SED

SED = resources = Peteris Krumins' blog - Sed streaming Sed - An Introduction and Tutorial by Bruce Barnett Sed, a stream editor tldp.org

= oneliner - Insert =

Insert a blank line above every line that matches "regex"
sed '/regex/{x;p;x;}'

Insert a blank line below every line that matches "regex"
sed '/regex/G'

Insert a blank line above and below every line that matches "regex"
sed '/regex/{x;p;x;G;}'

Insert five blank spaces at the beginning of each line
sed 's/^/    /'

= Numbering =

Number each line of a file (named filename). Left align the number
sed = filename | sed 'N;s/\n/\t/'

Number each line of a file (named filename). Right align the number
sed = filename | sed 'N; s/^/    /; s/ *\(.\{6,\}\)\n/\1  /'

Number each non-empty line of a file (called filename)
sed '/./=' filename | sed '/./N; s/\n/ /'

= Delete =

Delete leading whitespace (tabs and spaces) from each line
sed 's/^[ \t]*//'

Delete trailing whitespace (tabs and spaces) from each line
sed 's/[ \t]*$//'

Delete both leading and trailing whitespace from each line
sed 's/^[ \t]*//;s/[ \t]*$//'

Remove the 3rd line:
sed '3d' fileName.txt

Remove first 3 lines:
sed '1,3d' fileName.txt

Removal of the line containing the string "awk":
sed '/awk/d' filename.txt

Remove the last line:
sed '$d' filename.txt

Remove all empty lines:
sed '/^$/d' filename.txt sed '/./!d' filename.txt

Remove line "matched" by a regular expression
by eliminating one containing digital characters (at least 1 digit) located at the end of the line.

sed '/[0-9/][0-9]*$/d' filename.txt

Removing the interval between lines 7 and 9.
sed '7,9d' filename.txt

Same operation but replacing the address by "parameters".
sed '/-Start/,/-End/d' filename.txt

The above examples are only change at the display of the file (stdout1= screen).

For permanent changes to the old versions (<4) use a temporary file for GNU sed use the "-i[suffix]":

sed -i".bak" '3d' filename.txt

=Substitute =

Substitute (find and replace) the first occurrence of "foo" with "bar" on each line
sed 's/foo/bar/'

Substitute (find and replace) the fourth occurrence of "foo" with "bar" on each line
sed 's/foo/bar/4'

Substitute (find and replace) all occurrence of "foo" with "bar" on each line
sed 's/foo/bar/g'

Substitute (find and replace) the first occurrence of a repeated occurrence of "foo" with "bar"
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/'

Substitute (find and replace) only the last occurrence of "foo" with "bar"
sed 's/\(.*\)foo/\1bar/'

Substitute all occurrences of "foo" with "bar" on all lines that contain "baz"
sed '/baz/s/foo/bar/g'

Substitute all occurrences of "foo" with "bar" on all lines that DO NOT contain "baz"
sed '/baz/!s/foo/bar/g'

Change text "scarlet", "ruby" or "puce" to "red"
sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g' gsed 's/scarlet\|ruby\|puce/red/g'

Reverse order of lines (emulate "tac" Unix command)
 sed '1!G;h;$!d' sed -n '1!G;h;$p'
 * 1) It silences the output with "-n" switch and forces the output with "p" command only at the last line.

= Line =

Reverse a line (emulates "rev" Unix command)
sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'

Join pairs of lines side-by-side (emulates "paste" Unix command)
sed '$!N;s/\n/ /'

Append a line to the next if it ends with a backslash "\"
sed -e :a -e '/\\$/N; s/\\\n//; ta'

Append a line to the previous if it starts with an equal sign "="
sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'

Digit group (commify) a numeric string
 sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta' $ echo "12345 1234 123" | sed 's/\B[0-9]\{3\}\>/,&/g'
 * 1) This one-liner turns a string of digits, such as "1234567" to "1,234,567". This is called commifying or digit grouping.

Add commas to numbers with decimal points and minus signs
gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta'

Add a blank line after every five lines
sed 'n;n;n;n;G;'