SED

=Basics= generate_data | sed 's/x/y/g'
 * Using SED to make the small change of replacing x with y:

sed 's/regexp/replacement/g' inputFileName > outputFileName
 * SED on file:


 * Symbols:


 * Sed Pattern Flags:

=Examples= sed -e 's/([0-9][0-9][0-9])//g'
 * This will remove 2 numbers e.g: (123),(376) from input:

export PS1="C:\$( pwd | sed 's:/:\\\\\\:g' )\\> "
 * SED Joke to represent MSDOS Prompt:

Numbering
sed = filename | sed 'N;s/\n/\t/' sed = filename | sed 'N; s/^/    /; s/ *\(.\{6,\}\)\n/\1  /' sed '/./=' filename | sed '/./N; s/\n/ /'
 * Number each line of a file (simple left alignment). Using a tab (note on '\t' at end of file) instead of space will preserve margins.
 * Number each line of a file (number on left, right-aligned)
 * Number each line of file, but only print numbers if line is not blank

File Spacing
sed G
 * double space a file

sed '/^$/d;G'
 * double space a file which already has blank lines in it. Output file should contain no more than one blank line between lines of text.

sed 'G;G'
 * triple space a file

sed 'n;d'
 * undo double-spacing (assumes even-numbered lines are always blank)

sed '/regex/{x;p;x;}'
 * insert a blank line above every line which matches "regex"

sed '/regex/G'
 * insert a blank line below every line which matches "regex"

sed '/regex/{x;p;x;G;}'
 * insert a blank line above and below every line which matches "regex"

Text Conversion
sed 's/.$//'                        # assumes that all lines end with CR/LF
 * IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.

sed 's/$'"/`echo \\\r`/"            # command line under bash
 * IN UNIX ENVIRONMENT: Convert Unix newlines (LF) to DOS format.

sed "s/$//" sed -n p
 * IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.

sed 's/^[ \t]*//'                   # '\t' is a tab character (0x09),you should press the TAB key instead.
 * delete leading whitespace (spaces, tabs) from front of each line, aligns all text flush left

sed 's/[ \t]*$//'                   # '\t' is a tab character (0x09),you should press the TAB key instead.
 * delete trailing whitespace (spaces, tabs) from end of each line

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

sed 's/^/    /'
 * insert 5 blank spaces at beginning of each line (make page offset)

sed -e :a -e 's/^.\{1,78\}$/ &/;ta' # set at 78 plus 1 space
 * align all text flush right on a 79-column width

sed -e :a -e 's/^.\{1,77\}$/ & /;ta' sed -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/'
 * center all text in the middle of 79-column width.

sed 'n;n;n;n;G;'
 * add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)

sed 's/foo/bar/'            	      # replaces only 1st instance in a line sed 's/foo/bar/4'         	      # replaces only 4th instance in a line sed 's/foo/bar/g'         	      # replaces ALL instances in a line sed 's/\(.*\)foo\(.*foo\)/\1bar\2/'  # replace the next-to-last case sed 's/\(.*\)foo/\1bar/'             # replace only the last case
 * substitute (find and replace) "foo" with "bar" on each line

sed '/baz/s/foo/bar/g'
 * substitute "foo" with "bar" ONLY for lines which contain "baz"

sed '/baz/!s/foo/bar/g'
 * substitute "foo" with "bar" EXCEPT for lines which contain "baz"

sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'
 * change "scarlet" or "ruby" or "puce" to "red"

Printing
sed 10q
 * print first 10 lines of file (emulates behavior of "head")

sed q
 * print first line of file (emulates "head -1")

sed '/AAA/!d; /BBB/!d; /CCC/!d'
 * grep for AAA and BBB and CCC (in any order)

sed '/AAA.*BBB.*CCC/!d'
 * grep for AAA and BBB and CCC (in that order)

sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
 * grep for AAA or BBB or CCC (emulates "egrep")

Deletion
sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'
 * delete all lines except duplicate lines (emulates "uniq -d").

Special
sed '/^$/q'               # deletes everything after first blank line
 * get Usenet/e-mail message header

sed '1,/^$/d'             # deletes everything up to first blank line
 * get Usenet/e-mail message body

sed '/^Reply-To:/q; /^From:/h; /./d;g;q'
 * get return address header

= One-Liners =

Changing 1st parameter values sed 's/2M/200M/' php.ini.new > php.ini

= TEE =

Using tee to append to config file echo "net.ipv4.ip_forward = 1" | tee -a /etc/sysctl.conf