This article is part of the article series "Sed One-Liners Explained."
<- previous article next article ->

I love writing about programming and I am happy to announce my second e-book called "Sed One-Liners Explained". This book is based on my popular "Famous Sed One-Liners Explained" article series that has been read over 500,000 times.

I reviewed all the one-liners in the series, fixed various mistakes, greatly improved the explanations, added a bunch of new one-liners, bringing the total count to 100, and added three new chapters - an introduction to sed, a summary of sed addresses and ranges, and a chapter on debugging sed scripts with sed-sed.

Table of Contents

The e-book is 98 pages long and it explains exactly 100 one-liners. It's divided into the following chapters:

  • Preface.
  • 1. Introduction to sed.
  • 2. Line Spacing.
  • 3. Line Numbering.
  • 4. Text Conversion and Substitution.
  • 5. Selective Printing of Certain Lines.
  • 6. Selective Deletion of Certain Lines.
  • 7. Special sed Applications.
  • Appendix A. Summary of All sed Commands.
  • Appendix B. Addresses and Ranges.
  • Appendix C. Debugging sed Scripts with sed-sed.
  • Index.

What's sed?

Sed is the superman of UNIX stream editing. It's a small utility that's present on every UNIX system and it transforms one stream of text into another. Let's take a look at several practical examples that sed can carry out easily. All these examples and many more are explained in the e-book.

I have also made the first chapter of the book, Introduction to sed, freely available. Please download the e-book preview to read it. The introductory chapter explains the general principles of sed, introduces the four spaces of sed, addresses and ranges, and various command line flags.

Example 1: Replace "lamb" with "goat" on every line

sed 's/lamb/goat/'

This one-liner uses the famous s/.../.../ command. The s command substitutes the text in the first part of the command with the text in the second part. In this one-liner it replaces "lamb" with "goat".

A very detailed explanation of how sed reads the lines, how it executes the commands and how the printing happens is presented in the freely available introduction chapter. Please take a look.

Example 2: Replace only the second occurrence of "lamb" with "goat" on every line

sed 's/lamb/goat/2'

Sed is the only tool that I know that takes a numeric argument to the s command. The numeric argument, in this case 2, specifies which occurrence of the text to replace. In this example only the 2nd occurrence of "lamb" gets replaced with "goat".

Example 3: Number the lines in a file

sed = file | sed 'N; s/\n/: /'

This one-liner is actually two one-liners. The first one uses the = command that inserts a line containing the line number before every original line in the file. Then this output gets piped to the second sed command that joins two adjacent lines with the N command. When joining lines with the N command, a newline character \n is placed between them. Therefore it uses the s command to replace this newline \n with a colon followed by a space ": ".

So for example, if the file contains lines:

hello world
good job
sunny day

Then after running the one-liner, the result is going to be:

1: hello world
2: good job
3: sunny day

Example 4: Delete every 2nd line

sed 'n;d'

This one-liner uses the n command that prints the current line (actually the current pattern space, see the introduction chapter for in-depth explanation), deletes it, and reads the next line. Then sed executes the d command that deletes the current line without printing. This way the 1st line gets printed, the 2nd line gets deleted, then the 3rd line gets printed again, then the 4th gets deleted, etc.

Example 5: ROT 13 encode every line

sed '

Here the y/set1/set2/ command is used. The y command substitutes elements in the set1 with the corresponding elements in the set2. The first y command replaces all lowercase letters with their 13-char-shifted counterparts, and the second y command does the same for the uppercase letters. So for example, character a gets replaced by n, b gets replaced by o, character Z gets replaced by M, etc.

Sed is actually very powerful. It's as powerful as a Turing machine, meaning you can write any computer program in it. Check out these programs written in sed. Run them as sed -f file.sed:

After you read the e-book you'll be able to understand all these complex programs!

Book Preview

See the quality of my work before you buy the e-book. I have made the first chapter, Introduction to sed, freely available. The preview also includes the full table of contents, preface and the first page of chapter two.

Buy it now!

The price of the e-book is $9.95 and it can be purchased via PayPal:

PayPal - The safer, easier way to pay online!

After you have made the payment, my automated e-book processing system will send you the PDF e-book in a few minutes!

Tweet about my book!

Help me spread the word about my new book! I prepared a special link that you can use to tweet about it:

What's next?

I am not stopping here. I love writing about programming and my next book is going to be "Perl One-Liners Explained", based on my "Famous Perl One-Liners Explained" article series. Expect this book in a few months!


Enjoy the book and don't forget to leave comments about it!

Also if you're interested, take a look at my first e-book called "Awk One-Liners Explained". It's written in the same style as this e-book and it teaches practical Awk through many examples.

Finally, if you enjoy my blog, you can subscribe to my blog, follow me on Twitter or Google+.

This article is part of the article series "Sed One-Liners Explained."
<- previous article next article ->


WholesomeRobbieC Permalink
September 14, 2011, 12:11

Sounds great. Will definitely buy if you can release for Kindle.

September 14, 2011, 16:34

I will definitely release it for Kindle. Everyone who bought the PDF copy, will get Kindle version for free. I am almost done with the parser that converts LaTeX to Kindle. I will actually release all my books for Kindle.

Mark Tureny Permalink
September 27, 2011, 16:53

Any chance you might print a dead tree version?

September 30, 2011, 16:53

Yes, but not that soon.

September 14, 2011, 13:37

Congrats on releasing your second book!

Sadly it's a bit rushed release from typesetting PoV. To others: Peteris didn't show to me any WIP version before releasing it (just to make it clear, because by reading credits some of you could think the opposite), so I couldn't comment it earlier. But it is still better than previous one (that much I can say from comparing both samples, haven't seen full books) and hopefully the following ones will be even better rounded.

Keep the good work!

September 14, 2011, 18:11

Thanks Przemysław! This book is definitely much better typeset than the previous one. I agree that there are some typesetting issues but those are minor ones, like the titles for table of contents are pretty long. Otherwise the book itself looks great (see the book preview).

Just to let others know - Przemysław helped me to learn LaTeX and he has been giving a lot of tips on how to improve typesetting, and it's not the easiest thing to get right. Also taste for typesetting depends on the person, so what looks great to one, doesn't look that great to someone else.

He gave me a lot of tips on how to improve typesetting in this book (based on the preview) - smaller font and better spacing for the table of contents, better color for hyperlinks, better spacing for bullet points and other small improvements.

Kevin Permalink
September 15, 2011, 18:26

I just bought a copy and the typography in this book is really nice. Great work Peteris.

Aaron Permalink
September 14, 2011, 16:40

Peter, your work mesmerizes me. Your first eBook was brilliant and I just got your sed eBook. Keep up the good work and I am looking forward to your perl eBook!

September 14, 2011, 18:03

Awesome! I'll let you know when the Perl book is out!

September 15, 2011, 02:27

While sed hasn't been so useful for my projects as awk (I have used awk extensively for scientific data processing), it is still valuable to have it in your toolbox.

I found this to be a really lucid introduction by example; it certainly beats reading the man page ;-) (however man page is also recommended) Thank you!

September 16, 2011, 13:44

You're welcome!

PS. man page for sed is useless. ;)

Saint DanBert Permalink
September 24, 2011, 15:20

From the comments, it seems that you are doing the right things in the right way. Though you may stumble, you seem to be falling in the right direction (re improvements).

I have a passing interest in SED/AWK and friends, but please consider an eBook that documents your process and progress with creating eBooks. I keep hearing that it is easy to do, but my own experience has not seen those fruit. I have various nice manuscripts (mostly Libre/Open-office) that I've tried to package only to get junk when I view the eBook.

Continued Success,
~~~ 0;-Dan

September 27, 2011, 05:46

Thanks! Creating e-books is not easy at all. Really difficult thing to do.

kesselborn Permalink
October 07, 2011, 19:44

hey: would be super interested in a kindle edition ... could you put me on the list of people to contact once it's available?

October 08, 2011, 17:27

Definitely! I decided to publish Perl e-book first, and only after that release the Kindle edition of all 3 e-books.

Jakub Hrdina Permalink
October 28, 2011, 12:44

peter, how i can print all lines with WORD in them that appear twice?? please help

October 29, 2011, 05:34

Really easy, here is how:

sed -n '/WORD.*WORD/p'

The regular expression /WORD.*WORD/ matches two WORDs one after another on the current line. The p command prints such lines, and the -n command line option disables the printing of all other lines.

Techswatch Permalink
August 29, 2015, 09:00

had same question.. thanks for asking :P

Melchior Permalink
April 25, 2013, 23:35

Hello !

At the expense of sounding stupid: I was really interested in the sed sokoban/tetris and can't get those to work. Usage is sed -f sokoban.sed ? I'm running Fedora 17 if that is any help.


Simon Permalink
April 26, 2013, 16:10

In OS X (10.8) sed inline forces you to create backups by specifying a suffix for the backed up files after -i and before the argument. I read somewhere that this is BSD behaviour, from where OS X is based.

Took me a while to figure out why I was getting error messages because not many examples online do this.

Leave a new comment

(why do I need your e-mail?)

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

Type the word "unix_259": (just to make sure you're a human)

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