The most useful and complete sed tutorial
Stream Editor, sed
pattern matching, delimiters, deletion, ranges, grouping, all explained!
http://www.grymoire.com/Unix/Sed.html
And here’s a stream of one liners.
|
1 |
http://sed.sourceforge.net/sed1line.txt |
File Spacing
# double space a file
|
1 |
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.
|
1 |
sed '/^$/d;G' |
# triple space a file
|
1 |
sed 'G;G' |
# undo double-spacing (assumes even-numbered lines are always blank)
|
1 |
sed 'n;d' |
# insert a blank line above every line which matches “regex”
|
1 |
sed '/regex/{x;p;x;}' |
# insert a blank line below every line which matches “regex”
|
1 |
sed '/regex/G' |
# insert a blank line above and below every line which matches “regex”
|
1 |
sed '/regex/{x;p;x;G;}' |
Text Conversion
# IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format. assumes all lines end in CR/LF. in bash, press Ctrl-V then Ctrl-M to make ^M
|
1 2 3 |
sed 's/.$//' sed 's/^M$//' sed 's/\x0D$//' |
# IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.
|
1 2 |
sed 's/$'"/`echo \\\r`/" sed 's/$/\r/' |
# IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.
|
1 2 |
sed "s/$//" sed -n p |
Other
# remove overstrikes (char, backspace) from man pages. echo may need -e switch if you use a bash shell.
|
1 2 |
sed "s/.`echo \\\b`//g" sed 's/.^H//g' |
# get Usenet/e-mail message header, delete everything after first blank line
|
1 |
sed '/^$/q' |
# get Usenet/e-mail message body, delete everything up to first blank line
|
1 |
sed '1,/^$/d' |
# get Subject header, but remove initial “Subject: ” portion
|
1 |
sed '/^Subject: */!d; s///;q' |
# get return address header
|
1 |
sed '/^Reply-To:/q; /^From:/h; /./d;g;q' |
# remove most HTML tags (accommodates multiple-line tags)
|
1 |
sed -e :a -e 's/<[^>]*>//g;/</N;//ba' |
