sed

From RaySoft

sed (stream editor) is a Unix utility that parses text and implements a programming language which can apply transformations to such text. It reads input line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line.[1]

Documentation

Syntax

sed [PARAMETER ...] [SCRIPT] [FILE ...]

Parameters

-e SCRIPT, --expression=SCRIPT
Add the commands in SCRIPT to the set of commands to be run while processing the input.
-f FILE, --file=FILE
Add the commands contained in the FILE to the set of commands to be run while processing the input.
-i [SUFFIX], --in-place[=SUFFIX]
This option specifies that files are to be edited in-place. GNU sed does this by creating a temporary file and sending output to this file rather than to the standard output. This option implies -s.
-n, --quiet, --silent
By default, sed prints out the pattern space at the end of each cycle through the script. These options disable this automatic printing, and sed only produces output when explicitly told to via the p command.
-r, --regexp-extended
Use extended regular-expressions in the script.
-s, --separate
Consider files as separate rather than as a single continuous long stream.
-z, --null-data, --zero-terminated
Treat the input as a set of lines, each terminated by a zero byte instead of a newline. This option can be used with commands like sort -z and find -print0 to process arbitrary file names.

Commands

d
Delete the pattern space; immediately start next cycle.
g
Replace the contents of the pattern space with the contents of the hold space.
h
Replace the contents of the hold space with the contents of the pattern space.
p
Print the pattern space.
s/REGEXP/REPLACEMENT/[FLAGS]
Match the REGEXP against the content of the pattern space. If found, replace matched string with REPLACEMENT.

Flags

The s command can be followed by zero or more of the following FLAGS:

g
Apply the replacement to all matches to the regexp, not just the first.
I, i
The I modifier to regular-expression matching is a GNU extension which makes sed match regexp in a case-insensitive manner.
M, m
The M modifier to regular-expression matching is a GNU sed extension which directs GNU sed to match the regular-expression in multi-line mode.


Special sequence

The replacement section of the s command can include a special sequence.

\E
Stop case conversion started by \L or \U.
\l
Turn the next character to lowercase.
\L
Turn the replacement to lowercase until a \U or \E is found.
\u
Turn the next character to uppercase.
\U
Turn the replacement to uppercase until a \L or \E is found.

Examples

Print a specific line by its number from a file

Print line 23

sed --quiet '23p' 'myfile.txt'

Print line 23 to 64

sed --quiet '23,64p' 'myfile.txt'
Convert to uppercase
sed 's/.*/\U&/' <<<"${text}"
echo "${text^^}"               # Bash >= 4.0
Convert to lowercase
sed 's/.*/\L&/' <<<"${text}"
echo "${text,,}"               # Bash >= 4.0
Sort a comma separated string ignoring the first char
sed 's/\n//g; s/, */\n/g' <<<'.foo, #bar, -bla' \
| sort --key='1.2' \
| awk '{printf("%s%s", sep, $0); sep=", "} END {print ""}'

Output:

#bar, -bla, .foo

References