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: 129808 times

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

Microsoft Word 2000 format (.doc):
Download link: sed stream editor cheat sheet (.doc)
Downloaded: 7096 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.

sadarat Permalink
October 20, 2010, 06:38

Hey if any on has these charts on these commmands like

SED, AWK, GREP etc please farward hem on my mail
getnayab@gmail.com
thank you

November 16, 2010, 15:54

Nice layout. Asciidoc is nice to maintain the various output formats. It support tables that look descent in text, pdf, html output.

Just noticed in your sed cheat sheet, there is no -V only --version, like --help.

http://www.gnu.org/software/sed/manual/sed.html

surya Permalink
December 05, 2012, 09:05

Your plain text documnet has a very nice layout.
How did you create those nice tables in text? I am very curious to know that.

January 02, 2013, 17:39

It's just ascii art.

         
              .------.
.------.      | This |
| Just +------+.-----'
`------+ Like  |
       `-------'
samantha Permalink
March 01, 2013, 00:21

Well, I am considered a quite good hacker. But I frankly consider having to learn a bunch of different languages to do my work with little or no commonality a waste of brainpower. There is a sweet spot between a tool/process in a pipeline on series of commands and another language. I don't think our profession will advance until there is one core language and everything else expressible in that. But maybe I got warped playing with a Symbolics machine in my formative years. :)

That you even have to dig out a cheat sheet for X may be a sign that X just isn't good enough and perhaps shouldn't live as a separate thing.

Drew Marold Permalink
July 18, 2013, 13:56

Until that one grand unified thing comes along though you've got to work with what's out there. I'm a release engineer, so I have to know a wide variety of tools because you never know what you're going to encounter. I can write scripts in shell, sed, awk, perl, python, expect, dabble in C, and read & understand a couple more languages. That breadth of knowledge makes me very in demand. But I still keep cheat sheets & O'Reilly books on hand because I don't use them all every day, and sometimes it's hard to remember the exact specifics. It's kind of the opposite of the old saying "When all you have is a hammer, everything looks like a nail." I have a well stocked toolbox that lets me solve problems with a tool that matches the problem space.

April 16, 2014, 13:50

Ok really good news

April 16, 2014, 13:50

Ok really good news

Leave a new comment

(why do I need your e-mail?)

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

Type the first letter of your name: (just to make sure you're a human)

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

Advertisements