Nothing delights me more than great science literature.
If you like my blog, I'd be thankful for a gift from my Amazon Wishlist.
sed as a superman, the unix stream editor

I present to you my cheat sheet of sed, the Superman of stream editing! It has come handy 101 times for me because sed is not what I use daily and after some time of not using the sed commands tend to fade away from my mind and I have to review them quickly. This cheat sheet is ideal for that!

You can't really be a good hacker if you don't know this tool. I always love to think of various situations where knowing many, many different tools makes you the ultimate guru.
Suppose you had a situation where you were on a box which had some problems and the only tool available to you was sed, and you had to fix a configuration file problem really quickly or bad, bad things would happen. If you never knew sed, you'd be in deep trouble because it can't be learned from the man page, because it only lists all the commands and you have no understanding how the commands work.

When I first learned sed, I remember the joy when I understood how it worked, that it operated on input stream, output stream, pattern space and hold buffer. Once you know this, the rest can be understood from the man page, but before that, I doubt it.

If you want to learn the sed editor I recommend this wonderful tutorial and these books.

This sed cheat sheet contains:

  • command line summary
  • command description, if they take single address or pattern, or a range of addresses, and what they modify (input stream, output stream, pattern space or hold buffer)
  • command line argument summary
  • extensions
  • short summary of adderss range format

If you notice any inaccuracies, mistakes or see that I have left something out, comment on this page, please!

Download Sed Stream Editor Cheat-Sheet

PDF:
Download link: sed stream editor cheat sheet (.pdf)
Downloaded: 44827 times

Plain Text (.txt):
Download link: sed stream editor cheat sheet (.txt)
Downloaded: 21336 times

Microsoft Word 2000 format (.doc):
Download link: sed stream editor cheat sheet (.doc)
Downloaded: 3774 times

Are you interested in sed and unix text editing power tools? Here are three great books on this topic from Amazon:

Comments

September 06, 2007, 19:14

Great job. I've added it to http://TechCheatSheets.com

September 06, 2007, 19:57

Hey, SR. Thanks so much for adding it to your site :)

February 01, 2008, 20:15

Nice post.

stephen Permalink
April 03, 2008, 23:14

i've got problem on sed, which is i hv to change the line with colon to comma, and also change some order of the line,but the line is rili long. its like 1:2:3:4:5:6:7. i hv to only left 1:6:7. and also hv to change the order to 1:7:6.
how can i do that and write into script.please gv me some hint.

stephen Permalink
April 03, 2008, 23:16

please help me.the last output is 1,7,6

April 06, 2008, 23:50

Stephen, not sure I understand your question...
You can try doing 'sed -e 's/:/,/g' to change colons into commas. It's harder to change the order.
AWK language seems more appropriate, as you can specify split and field seperators, etc.

July 05, 2008, 23:35

anyone have hack for cod4 1,7?
please send me on :
serbiaursus@hotmail.com

zorg Permalink
December 14, 2008, 23:35

On the second page, it says that you can specify a label with a text string followed by a colon. Is that really true? It seems like the opposite of specifying a label. Shouldn't it read "... text string following a colon ..." instead?

December 13, 2009, 14:11

I'm having to open like 8 pages on your site to get your sed tips.. It'd be much easier for people if you created a master sed jump off point.

I feel the same way about sed as you, modifying streams mid-stream is just freaking awesome.

Check this out:

G=$'\33[H\33[2J'
A=$'\a'

G will reset and clear the screen, A will cause a bell, but only if they are output to the tty.

Using sed I can tail a log file like a php_error.log file and when sed sees a special keyword appear in the output from tail it replaces it with G or A.

tail -f php_error.log | sed -u -e "s/AATPUTCLS/${G}/g"

And in my php file I might add

error_log('AATPUTCLS');

which would send that to the php_error.log and if it is being output via tail then sed resets the screen making it easy to focus in on code segments.

I hope you have some cool stuff on sed.. it's one tough cookie to figure out, but soo worth it. I use it alot in my various functions for my bash_profile.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type here:

Please preview the comment before submitting to make sure it's OK.