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

stream

And here’s a stream of one liners.

http://sed.sourceforge.net/sed1line.txt

File Spacing

# double space a file

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'

# triple space a file

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. assumes all lines end in CR/LF. in bash, press Ctrl-V then Ctrl-M to make ^M

sed 's/.$//'
sed 's/^M$//'
sed 's/\x0D$//'

# IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.

sed 's/$'"/`echo \\\r`/"
sed 's/$/\r/'

# IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.

sed "s/$//"
sed -n p

Other

# remove overstrikes (char, backspace) from man pages. echo may need -e switch if you use a bash shell.

sed "s/.`echo \\\b`//g"
sed 's/.^H//g'

# get Usenet/e-mail message header, delete everything after first blank line

sed '/^$/q'

# get Usenet/e-mail message body, delete everything up to first blank line

sed '1,/^$/d'

# get Subject header, but remove initial “Subject: ” portion

sed '/^Subject: */!d; s///;q'

# get return address header

sed '/^Reply-To:/q; /^From:/h; /./d;g;q'

# remove most HTML tags (accommodates multiple-line tags)

sed -e :a -e 's/<[^>]*>//g;/</N;//ba'

Posted by admica   @   9 August 2010

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Extreme predictions by Bill Gates and Nicholas Negroponte
Next Post
Create a password hash with a specific word in it »
Powered by Wordpress   |   Lunated designed by ZenVerse