- This will remove 2 numbers e.g: (123),(376) from input:
sed -e 's/([0-9][0-9][0-9])//g'
- SED Joke to represent MSDOS Prompt:
export PS1="C:\$( pwd | sed 's:/:\\\\\\:g' )\\> "
Numbering
- 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.
sed = filename | sed 'N;s/\n/\t/'
- Number each line of a file (number on left, right-aligned)
sed = filename | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /'
- Number each line of file, but only print numbers if line is not blank
sed '/./=' filename | sed '/./N; s/\n/ /'
File Spacing
sed 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 '/^$/d;G'
sed 'G;G'
- undo double-spacing (assumes even-numbered lines are always blank)
sed 'n;d'
- insert a blank line above every line which matches "regex"
sed '/regex/{x;p;x;}'
- insert a blank line below every line which matches "regex"
sed '/regex/G'
- insert a blank line above and below every line which matches "regex"
sed '/regex/{x;p;x;G;}'
Text Conversion
- IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
sed 's/.$//' # assumes that all lines end with CR/LF
- IN UNIX ENVIRONMENT: Convert Unix newlines (LF) to DOS format.
sed 's/$'"/`echo \\\r`/" # command line under bash
- IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.
sed "s/$//"
sed -n p
- 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]*$//' # '\t' is a tab character (0x09),you should press the TAB key instead.
- delete BOTH leading and trailing whitespace from each line
sed 's/^[ \t]*//;s/[ \t]*$//'
- insert 5 blank spaces at beginning of each line (make page offset)
sed 's/^/ /'
- align all text flush right on a 79-column width
sed -e :a -e 's/^.\{1,78\}$/ &/;ta' # set at 78 plus 1 space
- center all text in the middle of 79-column width.
sed -e :a -e 's/^.\{1,77\}$/ & /;ta'
sed -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/'
- add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
sed 'n;n;n;n;G;'
- substitute (find and replace) "foo" with "bar" on each line
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 "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 '/baz/!s/foo/bar/g'
- change "scarlet" or "ruby" or "puce" to "red"
sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'
Printing
- print first 10 lines of file (emulates behavior of "head")
sed 10q
- print first line of file (emulates "head -1")
sed q
- grep for AAA and BBB and CCC (in any order)
sed '/AAA/!d; /BBB/!d; /CCC/!d'
- grep for AAA and BBB and CCC (in that order)
sed '/AAA.*BBB.*CCC/!d'
- grep for AAA or BBB or CCC (emulates "egrep")
sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
Deletion
- delete all lines except duplicate lines (emulates "uniq -d").
sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'
Special
- get Usenet/e-mail message header
sed '/^$/q' # deletes everything after first blank line
- get Usenet/e-mail message body
sed '1,/^$/d' # deletes everything up to first blank line
- get return address header
sed '/^Reply-To:/q; /^From:/h; /./d;g;q'
{{#widget:DISQUS
|id=networkm
|uniqid=SED
|url=https://aman.awiki.org/wiki/SED
}}