SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
by

                 @pkrumins
               Peteris Krumins
             peter@catonmat.net
          http://www.catonmat.net
        good coders code, great reuse




          Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
Contents


Contents                                                                       i

Preface                                                                      vii

1 Introduction                                                                1
  1.1 Sed One-Liners . . . . . . . . . . . . . . . . . . . . . . . . .        1

2 Line Spacing                                                                7
  2.1 Double-space a file . . . . . . . . . . . . . . . . . . . . . . .        7
  2.2 Double-space a file which already has blank lines in it . . . .          7
  2.3 Triple-space a file . . . . . . . . . . . . . . . . . . . . . . . .      8
  2.4 Undo double-spacing . . . . . . . . . . . . . . . . . . . . . .         8
  2.5 Insert a blank line above every line that matches "regex" . .           9
  2.6 Insert a blank line below every line that matches "regex" . .          10
  2.7 Insert a blank line above and below every line that matches
      "regex" . . . . . . . . . . . . . . . . . . . . . . . . . . . . .      10
  2.8 Join all lines . . . . . . . . . . . . . . . . . . . . . . . . . . .   10
  2.9 Duplicate all lines . . . . . . . . . . . . . . . . . . . . . . . .    11

3 Line Numbering                                                       12
  3.1 Number each line of a file (named filename) . . . . . . . . . 12
  3.2 Number each line of a file (named filename) and right align
      the number . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
  3.3 Number each non-empty line of a file (called filename) . . . 14
  3.4 Count the number of lines in a file (emulates "wc -l") . . . . 15

4 Text Conversion and Substitution                                       16
  4.1 Convert DOS/Windows newlines (CRLF) to Unix newlines
      (LF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16


                                       i



                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
CONTENTS                                                                    ii


  4.2    Another way to convert DOS/Windows newlines to Unix
         newlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
  4.3    Yet another way to convert DOS/Windows newlines to Unix
         newlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
  4.4    Convert Unix newlines (LF) to DOS/Windows newlines (CRLF) 17
  4.5    Another way to convert Unix newlines to DOS/Windows
         newlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
  4.6    Convert Unix newlines to DOS/Windows newlines from DOS/Win-
         dows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
  4.7    Another way to convert Unix newlines to DOS/Windows
         newlines from DOS/Windows . . . . . . . . . . . . . . . . . 18
  4.8    Delete leading whitespace (tabs and spaces) from each line . 19
  4.9    Delete trailing whitespace (tabs and spaces) from each line . 19
  4.10   Delete both leading and trailing whitespace from each line
         (trim) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
  4.11   Insert five blank spaces at the beginning of each line . . . . . 20
  4.12   Align lines right on a 79-column width . . . . . . . . . . . . 20
  4.13   Center all text in the middle of 79-column width . . . . . . . 21
  4.14   Substitute (find and replace) the 4th occurrence of "foo" with
         "bar" on each line . . . . . . . . . . . . . . . . . . . . . . . . 22
  4.15   Substitute (find and replace) all occurrence of "foo" with
         "bar" on each line . . . . . . . . . . . . . . . . . . . . . . . . 22
  4.16   Substitute (find and replace) the first occurrence of a re-
         peated occurrence of "foo" with "bar" . . . . . . . . . . . . 22
  4.17   Substitute (find and replace) only the last occurrence of "foo"
         with "bar" . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
  4.18   Substitute all occurrences of "foo" with "bar" on all lines
         that contain "baz" . . . . . . . . . . . . . . . . . . . . . . . 24
  4.19   Substitute all occurrences of "foo" with "bar" on all lines
         that do not contain "baz" . . . . . . . . . . . . . . . . . . . 24
  4.20   Change text "scarlet", "ruby" or "puce" to "red" . . . . . . 25
  4.21   Reverse order of lines (emulate "tac" Unix command) . . . . 25
  4.22   Reverse a line (emulates "rev" Unix command) . . . . . . . 27
  4.23   Join pairs of lines side-by-side (emulates "paste" Unix com-
         mand) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
  4.24   Append a line to the next if it ends with a backslash . . . . 28
  4.25   Append a line to the previous if it starts with an equal sign . 29
  4.26   Digit group (commify) a numeric string . . . . . . . . . . . . 30




                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
CONTENTS                                                                      iii


   4.27   Add commas to numbers with decimal points and minus signs           31
   4.28   Add a blank line after every five lines . . . . . . . . . . . . .    32
   4.29   Uppercase all letters of every line . . . . . . . . . . . . . . .   33
   4.30   Uppercase the first word of every line . . . . . . . . . . . . .     33
   4.31   Lowercase all letters of every line . . . . . . . . . . . . . . .   33
   4.32   Lowercase the first letter of every line . . . . . . . . . . . . .   33
   4.33   Duplicate every word on every line . . . . . . . . . . . . . .      34
   4.34   Another way to duplicate every word on every line . . . . . .       34
   4.35   Remove all punctuation from every line . . . . . . . . . . . .      35
   4.36   ROT13 encode every line . . . . . . . . . . . . . . . . . . . .     36

5 Selective Printing of Certain Lines                                      37
  5.1 Print the first 10 lines of a file (emulates "head -10") . . . . 37
  5.2 Print the first line of a file (emulates "head -1") . . . . . . . 37
  5.3 Print the last 10 lines of a file (emulates "tail -10") . . . . . 38
  5.4 Print the last 2 lines of a file (emulates "tail -2") . . . . . . 38
  5.5 Print the last line of a file (emulates "tail -1") . . . . . . . . 39
  5.6 Print next-to-the-last line of a file . . . . . . . . . . . . . . . 39
  5.7 Print only the lines that match a regular expression (emulates
       "grep") . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
  5.8 Print only the lines that do not match a regular expression
       (emulates "grep -v") . . . . . . . . . . . . . . . . . . . . . . 41
  5.9 Print the line before regexp, but not the line containing the
       regexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
  5.10 Print the line after regexp, but not the line containing the
       regexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
  5.11 Print one line before and after regexp. Also print the line
       matching regexp and its line number. (emulates "grep -A1
       -B1") . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
  5.12 Grep for "AAA" and "BBB" and "CCC" in any order . . . . 44
  5.13 Grep for "AAA" or "BBB", or "CCC" in any order . . . . . 44
  5.14 Grep for "AAA" and "BBB" and "CCC" in that order . . . 45
  5.15 Print the paragraph that contains "AAA" . . . . . . . . . . 46
  5.16 Print a paragraph if it contains "AAA" and "BBB" and
       "CCC" in any order . . . . . . . . . . . . . . . . . . . . . . 46
  5.17 Print a paragraph if it contains "AAA" or "BBB" or "CCC" 47
  5.18 Print only the lines that are 65 characters in length or more 47
  5.19 Print only the lines that are less than 65 chars . . . . . . . . 48




                 Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
CONTENTS                                                                      iv


   5.20 Print section of a file from a regex to end of file . . . . . . .       48
   5.21 Print lines 8-12 (inclusive) of a file . . . . . . . . . . . . . .     48
   5.22 Print line number 52 . . . . . . . . . . . . . . . . . . . . . .      49
   5.23 Beginning at line 3, print every 7th line . . . . . . . . . . . .     49
   5.24 Print section of lines between two regular expressions (inclu-
        sive) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   50
   5.25 Print lines that contain only printable ASCII characters . . .        50

6 Selective Deletion of Certain Lines                                         51
  6.1 Delete all lines in the file between two regular expressions . .         51
  6.2 Delete duplicate, consecutive lines from a file (emulates "uniq")        52
  6.3 Delete duplicate, nonconsecutive lines from a file . . . . . . .         54
  6.4 Delete all lines except duplicate consecutive lines (emulates
       "uniq -d") . . . . . . . . . . . . . . . . . . . . . . . . . . . .     56
  6.5 Delete the first 10 lines of a file . . . . . . . . . . . . . . . .       57
  6.6 Delete the last line of a file . . . . . . . . . . . . . . . . . . .     58
  6.7 Delete the last 2 lines of a file . . . . . . . . . . . . . . . . .      58
  6.8 Delete the last 10 lines of a file . . . . . . . . . . . . . . . .       58
  6.9 Delete every 8th line . . . . . . . . . . . . . . . . . . . . . .       59
  6.10 Delete lines that match a regular expression pattern . . . . .         60
  6.11 Delete all blank lines in a file (emulates "grep ’.’") . . . . . .      60
  6.12 Delete all consecutive blank lines from a file (emulates "cat
       -s") . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   61
  6.13 Delete all consecutive blank lines from a file except the first
       two . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .    61
  6.14 Delete all leading blank lines at the top of a file . . . . . . .       62
  6.15 Delete all trailing blank lines at the end of a file . . . . . . .      62
  6.16 Delete the last line of each paragraph . . . . . . . . . . . . .       63

7 Special Sed Applications                                             64
  7.1 Print Usenet/HTTP/Email message header . . . . . . . . . 64
  7.2 Print Usenet/HTTP/Email message body . . . . . . . . . . 65
  7.3 Extract subject from an email message . . . . . . . . . . . . 65
  7.4 Extract sender information from an email message . . . . . . 65
  7.5 Extract email address from a string . . . . . . . . . . . . . . 66
  7.6 Add a leading angle bracket and space to each line (quote an
      email message) . . . . . . . . . . . . . . . . . . . . . . . . . 66




                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
CONTENTS                                                                                                                   v


   7.7  Delete leading angle bracket from each line (unquote an email
        message) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                66
   7.8 Strip all HTML tags . . . . . . . . . . . . . . . . . . . . . .                                                    67
   7.9 Remove nroff overstrikes . . . . . . . . . . . . . . . . . . . .                                                    67
   7.10 Extract the IP address from ifconfig output . . . . . . . . .                                                      68

A Sed Commands                                                                                                            69
  A.1 The "=" command         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   69
  A.2 The "a" command         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   70
  A.3 The "b" command         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   70
  A.4 The "c" command         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   71
  A.5 The "d" command         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   71
  A.6 The "D" command         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   72
  A.7 The "g" command         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   72
  A.8 The "G" command         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   72
  A.9 The "h" command         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   72
  A.10 The "H" command        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   73
  A.11 The "i" command .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   73
  A.12 The "n" command        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   73
  A.13 The "N" command        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   74
  A.14 The "p" command        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   74
  A.15 The "P" command        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   74
  A.16 The "q" command        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   75
  A.17 The "r" command .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   75
  A.18 The "s" command .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   75
  A.19 The "t" command .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   75
  A.20 The "w" command        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   76
  A.21 The "x" command        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   76
  A.22 The "y" command        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   76
  A.23 The ":" command .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   77

B Addresses and Ranges                                                                                                    78
  B.1 Single Numeric Address .                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   78
  B.2 Single Regex Address . . .              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   78
  B.3 Pair of Numeric Addresses               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   79
  B.4 Pair of Regex Addresses .               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   79
  B.5 Special Address $ . . . . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   79
  B.6 Stepping Address . . . . .              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   80




                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
CONTENTS                                                                vi


  B.7 Match N Lines After the Address . . . . . . . . . . . . . . .     80
  B.8 Combining Addresses . . . . . . . . . . . . . . . . . . . . . .   80

C Debugging sed Scripts with sed-sed                                    81

Index                                                                   88




              Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
Preface

Thanks!
Thank you for purchasing my "Sed One-Liners Explained" e-book! This is
my second e-book and I based it on the "Famous Sed One-Liners Explained"
article series that I wrote on my www.catonmat.net blog. I went through all
the one-liners in the articles, improved them, added 8 new ones (bringing
the total count to 100), fixed a lot of mistakes, added an Introduction
to sed one-liners and three new chapters. The three new chapters are Sed
commands that summarizes all the sed commands, Sed addresses and ranges
that summarizes the sed ranges, and Debugging sed scripts with sed-sed
that shows how useful the sed-sed utility is.
    You might wonder why I called the article series "famous"? Well, be-
cause I based the articles on the famous sed1line.txt file by Eric Pement.
This file has been circulating around Unix newsgroups and forums for years
and it’s very popular among Unix programmers. That’s how I actually re-
ally learned sed myself. I went through all the one-liners in this file, tried
them out and endeavored to understand exactly how they work. Then I
decided that it would be a good idea to explain them on my blog, which I
did, and after that I thought, why not turn it into a book? That’s how I
ended up writing this book.
    As I mentioned, this is my second e-book. My first e-book is "Awk
One-Liners Explained" which I based on Eric Pement’s awk1line.txt file. I
really learned Awk from this file, too.
    My third e-book is now in development. It’s "Perl One-Liners Ex-
plained". The Perl book will be based on my "Famous Perl One-Liners
Explained" article series. I was so inspired by sed1line.txt and awk1line.txt
files that I decided to create my own perl1line.txt file. I am still working
on it but I am one chapter away from finishing it. If you’re interested,
subscribe to my blog, follow me on Twitter or follow me on Google+. That

                                     vii



                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
PREFACE                                                                 viii


way you’ll be the first to know when I publish perl1line.txt and my next
book!


Credits
I’d like to thank Eric Pement who made the famous sed1line.txt file that
I learned Sed from and that I based this book on. I’d also like to thank
waldner and DJ Mills from #sed channel on FreeNode IRC network for
always helping me with sed. Also thanks goes to Madars Virza for the book
review, Przemysław Pawełczyk for teaching me LaTeX, Abraham Alhashmy
for advice on how to improve the design of the book, Blake Sitney for proof-
reading the book, everyone who commented on my blog while I was writing
the sed one-liners article series, and everyone else who helped me with sed
and this book.




                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
One

                                                    Introduction

1.1     Sed One-Liners
Mastering sed can be reduced to understanding and manipulating the four
spaces of sed. These four spaces are:

   • Input Stream

   • Pattern Space

   • Hold Buffer

   • Output Stream

    Think about the spaces this way – sed reads the input stream and pro-
duces the output stream. Internally it has the pattern space and the hold
buffer. Sed reads data from the input stream until it finds the newline
character n. Then it places the data read so far, without the newline, into
the pattern space. Most of the sed commands operate on the data in the
pattern space. The hold buffer is there for your convenience. Think about
it as temporary buffer. You can copy or exchange data between the pattern
space and the hold buffer. Once sed has executed all the commands, it
outputs the pattern space and adds a n at the end.
    It’s possible to modify the behavior of sed with the -n command line
switch. When -n is specified, sed doesn’t output the pattern space and you
have to explicitly print it with either p or P commands.
    Let’s look at several examples to understand the four spaces and sed.
You’ll learn much more about the sed commands and spaces as you work
through the chapters of the book. These are just examples to illustrate
what sed looks like and what it’s all about.
    Here is the simplest possible sed program:


                                     1



                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
CHAPTER 1. INTRODUCTION                                                     2


  sed 's/foo/bar/'


    This program replaces text "foo" with "bar" on every line. Here is how
it works. Suppose you have a file with these lines:

  abc
  foo
  123-foo-456


    Sed opens the file as the input stream and starts reading the data. After
reading "abc" it finds a newline n. It places the text "abc" in the pattern
space and now it applies the s/foo/bar/ command. Since we have "abc"
in the pattern space and there is no "foo" anywhere, sed does nothing to
the pattern space. At this moment sed has executed all the commands (in
this case just one). The default action when all the commands have been
executed is to print the pattern space, followed by newline. So the output
from the first line is "abcn".
    Now sed reads in the second line "foo" and executes s/foo/bar/. This
replaces "foo" with "bar". The pattern space now contains just "bar". The
end of the script has been reached and sed prints out the pattern space,
followed by newline. The output from the second line is "barn".
    Now the 3rd line is read in. The pattern space is now "123-foo-456".
Since there is "foo" in the text, the s/foo/bar/ is successful and the pattern
space is now "123-bar-456". The end is reached and sed prints the pattern
space. The output is "123-bar-456n".
    All the lines of the input have been read at this moment and sed exits.
The output from running the script on our example file is:

  abc
  bar
  123-bar-456


    In this example we never used the hold buffer because there was no need
for temporary storage.
    Before we look at an example with temporary storage, let’s take a look
at three command line switches – -n, -e and -i. First -n.
    If you specify -n to sed, like this:




                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
CHAPTER 1. INTRODUCTION                                                     3


  sed -n 's/foo/bar/'


    Then sed will no longer print the pattern space when it reaches the end
of the script. So if you run this program on our sample file above, there
will be no output. You must use sed’s p command to force sed to print the
line:

  sed -n 's/foo/bar/; p'


   As you can see, sed commands are separated by the ; character. You
can also use -e switch to separate the commands:

  sed -n -e 's/foo/bar/' -e 'p'


    It’s the same as if you used ;. Next, let’s take a look at the -i command
line argument. This one forces sed to do in-place editing of the file, meaning
it reads the contents of the file, executes the commands, and places the new
contents back in the file.
    Here is an example. Suppose you have a file called "users", with the
following content:

  pkrumins:hacker
  esr:guru
  rms:geek


   And you wish to replace the ":" symbol with ";" in the whole file. Then
you can do it as easily as:

  sed -i 's/:/;/' users


    It will silently execute the s/:/;/ command on all lines in the file and
do all substitutions. Be very careful when using -i as it’s destructive
and it’s not reversible! It’s always safer to run sed without -i, and then
replace the file yourself.
    Actually, before we look at the hold buffer, let’s take a look at addresses
and ranges. Addresses allow you to restrict sed commands to certain lines,
or ranges of lines.




                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
CHAPTER 1. INTRODUCTION                                                        4


   The simplest address is a single number that limits sed commands to
the given line number:

  sed '5s/foo/bar/'


   This limits the s/foo/bar/ only to the 5th line of file or input stream.
So if there is a "foo" on the 5th line, it will be replaced with "bar". No
other lines will be touched.
   The addresses can be also inverted with the ! after the address. To
match all lines that are not the 5th line (lines 1-4, plus lines 6-...), do this:

  sed '5!s/foo/bar/'


   The inversion can be applied to any address.
   Next, you can also limit sed commands to a range of lines by specifying
two numbers, separated by a comma:

  sed '5,10s/foo/bar/'


    In this one-liner the s/foo/bar/ is executed only on lines 5 – 10, in-
clusive. Here is a quick, useful one-liner. Suppose you want to print lines
5 – 10 in the file. You can first disable implicit line printing with the -n
command line switch, and then use the p command on lines 5 – 10:

  sed -n '5,10p'


    This will execute the p command only on lines 5 – 10. No other lines
will be output. Pretty neat, isn’t it?
    There is a special address $ that matches the last line of the file. Here
is an example that prints the last line of the file:

  sed -n '$p'


    As you can see, the p command has been limited to $, which is the last
line of input.
    Next, there is also a single regular expression address match like this
/regex/. If you specify a regex before a command, then the command will
only get executed on lines that match the regex. Check this out:




                 Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
CHAPTER 1. INTRODUCTION                                                   5


 sed -n '/a+b+/p'


    Here the p command will get called only on lines that match a+b+ reg-
ular expression, which means one or more letters "a" followed by one or
more letters "b". For example, it prints lines like "ab", "aab", "aaabbbbb",
"foo-123-ab", etc.
    There is also an expression to match a range between two regexes. Here
is an example,

 sed '/foo/,/bar/d'


    This one-liner matches all lines between the first line that matches
"/foo/" regex and the first line that matches "/bar/" regex, inclusive. It
applies the d command that stands for delete. In other words, it deletes a
range of lines between the first line that matches "/foo/" and the first line
after "/foo/" that matches "/bar/", inclusive.
    Now let’s take a look at the hold buffer. Suppose you have a problem
where you want to print the line before the line that matches a regular
expression. How do you do this? If sed didn’t have a hold buffer, things
would be tough, but with hold buffer we can always save the current line
to the hold buffer, and then let sed read in the next line. Now if the next
line matches the regex, we would just print the hold buffer, which holds the
previous line. Easy, right?
    The command for copying the current pattern space to the hold buffer
is h. The command for copying the hold buffer back to the pattern space
is g. The command for exchanging the hold buffer and the pattern space is
x. We just have to choose the right commands to solve this problem. Here
is the solution:

 sed -n '/regex/{x;p;x}; h'


   It works this way – every line gets copied to the hold buffer with the h
command at the end of the script. However, for every line that matches the
/regex/, we exchange the hold buffer with the pattern space by using the
x command, print it with the p command, and then exchange the buffers
back, so that if the next line matches the /regex/ again, we could print the
current line.




                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
CHAPTER 1. INTRODUCTION                                                   6


    Also notice the command grouping. Several commands can be grouped
and executed only for a specific address or range. In this one-liner the
command group is {x;p;x} and it gets executed only if the current line
matches /regex/.
    Note that this one-liner doesn’t work if it’s the first line of the input
matches /regex/. To fix this, we can limit the p command to all lines that
are not the first line with the 1! inverted address match:

 sed -n '/regex/{x;1!p;x}; h'


   Notice the 1!p. This says – call the p command on all the lines that are
not the 1st line. This prevents anything to be printed in case the first line
matches /regex/.
   I think that this is a great introduction to sed one-liners that covers
the most important concepts in sed. Overall this book contains exactly 100
well-explained one-liners. Once you work through them, you’ll have rewired
your brain to "think in sed". In other words, you’ll have learned how to
manipulate the pattern space, the hold buffer and you’ll know when to print
the data to get the results that you need. Enjoy this book!




                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
Two

                                                     Line Spacing

2.1       Double-space a file

  sed G


    This sed one-liner uses the G command. The G command appends a
newline n followed by the contents of the hold buffer to the pattern space.
In this one-liner the hold buffer is empty all the time (only three commands
- h, H and x modify the hold buffer), so we end up simply appending a
newline to the pattern space for every line. Once all the commands have
been processed (in this case just the G command), sed sends the contents of
pattern space to the output stream followed by a newline. So there we have
it, each line now ends with two newlines – one added by the G command
and the other implicitly by sed’s printing mechanism. File has been double
spaced.


2.2       Double-space a file which already has
          blank lines in it

  sed '/^$/d;G'


    Sed allows the commands to be restricted only to certain lines. This
one-liner operates only on lines that match the regular expression /^$/.
But which are those? Those are the empty lines, because only the empty
lines have a property that the beginning of the line ^ is the same as the end
of the line $. Remember that before doing the regular expression match sed
pushes the input line to pattern space. When doing it, sed strips the trailing

                                      7



                Preview copy (first 16 pages)
Get full e-book at www.catonmat.net/blog/sed-book/
This is only the e-book preview.


           Get the full e-book at:



http://www.catonmat.net/blog/sed-book/

Contenu connexe

En vedette

Testingtechniques And Strategy
Testingtechniques And StrategyTestingtechniques And Strategy
Testingtechniques And Strategynazeer pasha
 
Presentation of awk
Presentation of awkPresentation of awk
Presentation of awkyogesh4589
 
unix crontab basics
unix crontab basicsunix crontab basics
unix crontab basicssaratsandhya
 
Linux fundamental - Chap 15 Job Scheduling
Linux fundamental - Chap 15 Job SchedulingLinux fundamental - Chap 15 Job Scheduling
Linux fundamental - Chap 15 Job SchedulingKenny (netman)
 
Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)Qiangning Hong
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrepTri Truong
 
Oracle 12c Multi Process Multi Threaded
Oracle 12c Multi Process Multi ThreadedOracle 12c Multi Process Multi Threaded
Oracle 12c Multi Process Multi ThreadedMarkus Flechtner
 
Practical unix utilities for text processing
Practical unix utilities for text processingPractical unix utilities for text processing
Practical unix utilities for text processingAnton Arhipov
 
sed -- A programmer's perspective
sed -- A programmer's perspectivesed -- A programmer's perspective
sed -- A programmer's perspectiveLi Ding
 
Python在豆瓣的应用
Python在豆瓣的应用Python在豆瓣的应用
Python在豆瓣的应用Qiangning Hong
 

En vedette (20)

Unix day4 v1.3
Unix day4 v1.3Unix day4 v1.3
Unix day4 v1.3
 
What Linux is what you should also have on your computer.
What Linux is what you should also have on your computer.What Linux is what you should also have on your computer.
What Linux is what you should also have on your computer.
 
Testingtechniques And Strategy
Testingtechniques And StrategyTestingtechniques And Strategy
Testingtechniques And Strategy
 
Unix
UnixUnix
Unix
 
Awk essentials
Awk essentialsAwk essentials
Awk essentials
 
Presentation of awk
Presentation of awkPresentation of awk
Presentation of awk
 
unix crontab basics
unix crontab basicsunix crontab basics
unix crontab basics
 
UNIX - Class6 - sed - Detail
UNIX - Class6 - sed - DetailUNIX - Class6 - sed - Detail
UNIX - Class6 - sed - Detail
 
Shell script-sec
Shell script-secShell script-sec
Shell script-sec
 
Chap06
Chap06Chap06
Chap06
 
Linux fundamental - Chap 15 Job Scheduling
Linux fundamental - Chap 15 Job SchedulingLinux fundamental - Chap 15 Job Scheduling
Linux fundamental - Chap 15 Job Scheduling
 
Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
Oracle 12c Multi Process Multi Threaded
Oracle 12c Multi Process Multi ThreadedOracle 12c Multi Process Multi Threaded
Oracle 12c Multi Process Multi Threaded
 
Awk programming
Awk programming Awk programming
Awk programming
 
Czzawk
CzzawkCzzawk
Czzawk
 
DAE
DAEDAE
DAE
 
Practical unix utilities for text processing
Practical unix utilities for text processingPractical unix utilities for text processing
Practical unix utilities for text processing
 
sed -- A programmer's perspective
sed -- A programmer's perspectivesed -- A programmer's perspective
sed -- A programmer's perspective
 
Python在豆瓣的应用
Python在豆瓣的应用Python在豆瓣的应用
Python在豆瓣的应用
 

Similaire à Sed Unix Utility Explained

Assembly Codes in C Programmes - A Short Notes by Arun Umrao
Assembly Codes in C Programmes - A Short Notes by Arun UmraoAssembly Codes in C Programmes - A Short Notes by Arun Umrao
Assembly Codes in C Programmes - A Short Notes by Arun Umraossuserd6b1fd
 
Time seriesplot
Time seriesplotTime seriesplot
Time seriesplotHélio
 
Programming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiProgramming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiPriya Chauhan
 
Programming in Ansi C
Programming in Ansi CProgramming in Ansi C
Programming in Ansi CPriya Chauhan
 
Elements of Programming Interviews.pdf
Elements of Programming Interviews.pdfElements of Programming Interviews.pdf
Elements of Programming Interviews.pdfSudhir Biswal
 
Algorithms for programmers ideas and source code
Algorithms for programmers ideas and source code Algorithms for programmers ideas and source code
Algorithms for programmers ideas and source code Duy Phan
 
Algorithmic Problem Solving with Python.pdf
Algorithmic Problem Solving with Python.pdfAlgorithmic Problem Solving with Python.pdf
Algorithmic Problem Solving with Python.pdfEmily Smith
 
Fortran 90 overview
Fortran 90 overviewFortran 90 overview
Fortran 90 overviewthang7788
 
Ibm db2 10.5 for linux, unix, and windows x query reference
Ibm db2 10.5 for linux, unix, and windows   x query referenceIbm db2 10.5 for linux, unix, and windows   x query reference
Ibm db2 10.5 for linux, unix, and windows x query referencebupbechanhgmail
 
Tinyos programming
Tinyos programmingTinyos programming
Tinyos programmingssuserf04f61
 

Similaire à Sed Unix Utility Explained (20)

Assembly Codes in C Programmes - A Short Notes by Arun Umrao
Assembly Codes in C Programmes - A Short Notes by Arun UmraoAssembly Codes in C Programmes - A Short Notes by Arun Umrao
Assembly Codes in C Programmes - A Short Notes by Arun Umrao
 
Time seriesplot
Time seriesplotTime seriesplot
Time seriesplot
 
Programming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiProgramming in ansi C by Balaguruswami
Programming in ansi C by Balaguruswami
 
Programming in Ansi C
Programming in Ansi CProgramming in Ansi C
Programming in Ansi C
 
Elements of Programming Interviews.pdf
Elements of Programming Interviews.pdfElements of Programming Interviews.pdf
Elements of Programming Interviews.pdf
 
Manual Cobol
Manual CobolManual Cobol
Manual Cobol
 
Algorithms for programmers ideas and source code
Algorithms for programmers ideas and source code Algorithms for programmers ideas and source code
Algorithms for programmers ideas and source code
 
Algoritmicx
AlgoritmicxAlgoritmicx
Algoritmicx
 
Tutorial111
Tutorial111Tutorial111
Tutorial111
 
Bsdconv
BsdconvBsdconv
Bsdconv
 
Algorithmic Problem Solving with Python.pdf
Algorithmic Problem Solving with Python.pdfAlgorithmic Problem Solving with Python.pdf
Algorithmic Problem Solving with Python.pdf
 
Fortran 90 overview
Fortran 90 overviewFortran 90 overview
Fortran 90 overview
 
Ibm db2 10.5 for linux, unix, and windows x query reference
Ibm db2 10.5 for linux, unix, and windows   x query referenceIbm db2 10.5 for linux, unix, and windows   x query reference
Ibm db2 10.5 for linux, unix, and windows x query reference
 
Tinyos programming
Tinyos programmingTinyos programming
Tinyos programming
 
Bogstad 2015
Bogstad 2015Bogstad 2015
Bogstad 2015
 
Pattern Finding.pdf
Pattern Finding.pdfPattern Finding.pdf
Pattern Finding.pdf
 
OPCDE Crackme Solution
OPCDE Crackme SolutionOPCDE Crackme Solution
OPCDE Crackme Solution
 
Typeset equations
Typeset equationsTypeset equations
Typeset equations
 
Manual de KmPlot
Manual de KmPlotManual de KmPlot
Manual de KmPlot
 
Effective-java EMERSON EDUARDO RODRIGUES
Effective-java EMERSON EDUARDO RODRIGUESEffective-java EMERSON EDUARDO RODRIGUES
Effective-java EMERSON EDUARDO RODRIGUES
 

Dernier

Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 

Dernier (20)

Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 

Sed Unix Utility Explained

  • 1. by @pkrumins Peteris Krumins peter@catonmat.net http://www.catonmat.net good coders code, great reuse Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 2. Contents Contents i Preface vii 1 Introduction 1 1.1 Sed One-Liners . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 Line Spacing 7 2.1 Double-space a file . . . . . . . . . . . . . . . . . . . . . . . 7 2.2 Double-space a file which already has blank lines in it . . . . 7 2.3 Triple-space a file . . . . . . . . . . . . . . . . . . . . . . . . 8 2.4 Undo double-spacing . . . . . . . . . . . . . . . . . . . . . . 8 2.5 Insert a blank line above every line that matches "regex" . . 9 2.6 Insert a blank line below every line that matches "regex" . . 10 2.7 Insert a blank line above and below every line that matches "regex" . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.8 Join all lines . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.9 Duplicate all lines . . . . . . . . . . . . . . . . . . . . . . . . 11 3 Line Numbering 12 3.1 Number each line of a file (named filename) . . . . . . . . . 12 3.2 Number each line of a file (named filename) and right align the number . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.3 Number each non-empty line of a file (called filename) . . . 14 3.4 Count the number of lines in a file (emulates "wc -l") . . . . 15 4 Text Conversion and Substitution 16 4.1 Convert DOS/Windows newlines (CRLF) to Unix newlines (LF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 i Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 3. CONTENTS ii 4.2 Another way to convert DOS/Windows newlines to Unix newlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 4.3 Yet another way to convert DOS/Windows newlines to Unix newlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 4.4 Convert Unix newlines (LF) to DOS/Windows newlines (CRLF) 17 4.5 Another way to convert Unix newlines to DOS/Windows newlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 4.6 Convert Unix newlines to DOS/Windows newlines from DOS/Win- dows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 4.7 Another way to convert Unix newlines to DOS/Windows newlines from DOS/Windows . . . . . . . . . . . . . . . . . 18 4.8 Delete leading whitespace (tabs and spaces) from each line . 19 4.9 Delete trailing whitespace (tabs and spaces) from each line . 19 4.10 Delete both leading and trailing whitespace from each line (trim) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 4.11 Insert five blank spaces at the beginning of each line . . . . . 20 4.12 Align lines right on a 79-column width . . . . . . . . . . . . 20 4.13 Center all text in the middle of 79-column width . . . . . . . 21 4.14 Substitute (find and replace) the 4th occurrence of "foo" with "bar" on each line . . . . . . . . . . . . . . . . . . . . . . . . 22 4.15 Substitute (find and replace) all occurrence of "foo" with "bar" on each line . . . . . . . . . . . . . . . . . . . . . . . . 22 4.16 Substitute (find and replace) the first occurrence of a re- peated occurrence of "foo" with "bar" . . . . . . . . . . . . 22 4.17 Substitute (find and replace) only the last occurrence of "foo" with "bar" . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 4.18 Substitute all occurrences of "foo" with "bar" on all lines that contain "baz" . . . . . . . . . . . . . . . . . . . . . . . 24 4.19 Substitute all occurrences of "foo" with "bar" on all lines that do not contain "baz" . . . . . . . . . . . . . . . . . . . 24 4.20 Change text "scarlet", "ruby" or "puce" to "red" . . . . . . 25 4.21 Reverse order of lines (emulate "tac" Unix command) . . . . 25 4.22 Reverse a line (emulates "rev" Unix command) . . . . . . . 27 4.23 Join pairs of lines side-by-side (emulates "paste" Unix com- mand) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 4.24 Append a line to the next if it ends with a backslash . . . . 28 4.25 Append a line to the previous if it starts with an equal sign . 29 4.26 Digit group (commify) a numeric string . . . . . . . . . . . . 30 Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 4. CONTENTS iii 4.27 Add commas to numbers with decimal points and minus signs 31 4.28 Add a blank line after every five lines . . . . . . . . . . . . . 32 4.29 Uppercase all letters of every line . . . . . . . . . . . . . . . 33 4.30 Uppercase the first word of every line . . . . . . . . . . . . . 33 4.31 Lowercase all letters of every line . . . . . . . . . . . . . . . 33 4.32 Lowercase the first letter of every line . . . . . . . . . . . . . 33 4.33 Duplicate every word on every line . . . . . . . . . . . . . . 34 4.34 Another way to duplicate every word on every line . . . . . . 34 4.35 Remove all punctuation from every line . . . . . . . . . . . . 35 4.36 ROT13 encode every line . . . . . . . . . . . . . . . . . . . . 36 5 Selective Printing of Certain Lines 37 5.1 Print the first 10 lines of a file (emulates "head -10") . . . . 37 5.2 Print the first line of a file (emulates "head -1") . . . . . . . 37 5.3 Print the last 10 lines of a file (emulates "tail -10") . . . . . 38 5.4 Print the last 2 lines of a file (emulates "tail -2") . . . . . . 38 5.5 Print the last line of a file (emulates "tail -1") . . . . . . . . 39 5.6 Print next-to-the-last line of a file . . . . . . . . . . . . . . . 39 5.7 Print only the lines that match a regular expression (emulates "grep") . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 5.8 Print only the lines that do not match a regular expression (emulates "grep -v") . . . . . . . . . . . . . . . . . . . . . . 41 5.9 Print the line before regexp, but not the line containing the regexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 5.10 Print the line after regexp, but not the line containing the regexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 5.11 Print one line before and after regexp. Also print the line matching regexp and its line number. (emulates "grep -A1 -B1") . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 5.12 Grep for "AAA" and "BBB" and "CCC" in any order . . . . 44 5.13 Grep for "AAA" or "BBB", or "CCC" in any order . . . . . 44 5.14 Grep for "AAA" and "BBB" and "CCC" in that order . . . 45 5.15 Print the paragraph that contains "AAA" . . . . . . . . . . 46 5.16 Print a paragraph if it contains "AAA" and "BBB" and "CCC" in any order . . . . . . . . . . . . . . . . . . . . . . 46 5.17 Print a paragraph if it contains "AAA" or "BBB" or "CCC" 47 5.18 Print only the lines that are 65 characters in length or more 47 5.19 Print only the lines that are less than 65 chars . . . . . . . . 48 Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 5. CONTENTS iv 5.20 Print section of a file from a regex to end of file . . . . . . . 48 5.21 Print lines 8-12 (inclusive) of a file . . . . . . . . . . . . . . 48 5.22 Print line number 52 . . . . . . . . . . . . . . . . . . . . . . 49 5.23 Beginning at line 3, print every 7th line . . . . . . . . . . . . 49 5.24 Print section of lines between two regular expressions (inclu- sive) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 5.25 Print lines that contain only printable ASCII characters . . . 50 6 Selective Deletion of Certain Lines 51 6.1 Delete all lines in the file between two regular expressions . . 51 6.2 Delete duplicate, consecutive lines from a file (emulates "uniq") 52 6.3 Delete duplicate, nonconsecutive lines from a file . . . . . . . 54 6.4 Delete all lines except duplicate consecutive lines (emulates "uniq -d") . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 6.5 Delete the first 10 lines of a file . . . . . . . . . . . . . . . . 57 6.6 Delete the last line of a file . . . . . . . . . . . . . . . . . . . 58 6.7 Delete the last 2 lines of a file . . . . . . . . . . . . . . . . . 58 6.8 Delete the last 10 lines of a file . . . . . . . . . . . . . . . . 58 6.9 Delete every 8th line . . . . . . . . . . . . . . . . . . . . . . 59 6.10 Delete lines that match a regular expression pattern . . . . . 60 6.11 Delete all blank lines in a file (emulates "grep ’.’") . . . . . . 60 6.12 Delete all consecutive blank lines from a file (emulates "cat -s") . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 6.13 Delete all consecutive blank lines from a file except the first two . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 6.14 Delete all leading blank lines at the top of a file . . . . . . . 62 6.15 Delete all trailing blank lines at the end of a file . . . . . . . 62 6.16 Delete the last line of each paragraph . . . . . . . . . . . . . 63 7 Special Sed Applications 64 7.1 Print Usenet/HTTP/Email message header . . . . . . . . . 64 7.2 Print Usenet/HTTP/Email message body . . . . . . . . . . 65 7.3 Extract subject from an email message . . . . . . . . . . . . 65 7.4 Extract sender information from an email message . . . . . . 65 7.5 Extract email address from a string . . . . . . . . . . . . . . 66 7.6 Add a leading angle bracket and space to each line (quote an email message) . . . . . . . . . . . . . . . . . . . . . . . . . 66 Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 6. CONTENTS v 7.7 Delete leading angle bracket from each line (unquote an email message) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 7.8 Strip all HTML tags . . . . . . . . . . . . . . . . . . . . . . 67 7.9 Remove nroff overstrikes . . . . . . . . . . . . . . . . . . . . 67 7.10 Extract the IP address from ifconfig output . . . . . . . . . 68 A Sed Commands 69 A.1 The "=" command . . . . . . . . . . . . . . . . . . . . . . . 69 A.2 The "a" command . . . . . . . . . . . . . . . . . . . . . . . 70 A.3 The "b" command . . . . . . . . . . . . . . . . . . . . . . . 70 A.4 The "c" command . . . . . . . . . . . . . . . . . . . . . . . 71 A.5 The "d" command . . . . . . . . . . . . . . . . . . . . . . . 71 A.6 The "D" command . . . . . . . . . . . . . . . . . . . . . . . 72 A.7 The "g" command . . . . . . . . . . . . . . . . . . . . . . . 72 A.8 The "G" command . . . . . . . . . . . . . . . . . . . . . . . 72 A.9 The "h" command . . . . . . . . . . . . . . . . . . . . . . . 72 A.10 The "H" command . . . . . . . . . . . . . . . . . . . . . . . 73 A.11 The "i" command . . . . . . . . . . . . . . . . . . . . . . . . 73 A.12 The "n" command . . . . . . . . . . . . . . . . . . . . . . . 73 A.13 The "N" command . . . . . . . . . . . . . . . . . . . . . . . 74 A.14 The "p" command . . . . . . . . . . . . . . . . . . . . . . . 74 A.15 The "P" command . . . . . . . . . . . . . . . . . . . . . . . 74 A.16 The "q" command . . . . . . . . . . . . . . . . . . . . . . . 75 A.17 The "r" command . . . . . . . . . . . . . . . . . . . . . . . . 75 A.18 The "s" command . . . . . . . . . . . . . . . . . . . . . . . . 75 A.19 The "t" command . . . . . . . . . . . . . . . . . . . . . . . . 75 A.20 The "w" command . . . . . . . . . . . . . . . . . . . . . . . 76 A.21 The "x" command . . . . . . . . . . . . . . . . . . . . . . . 76 A.22 The "y" command . . . . . . . . . . . . . . . . . . . . . . . 76 A.23 The ":" command . . . . . . . . . . . . . . . . . . . . . . . . 77 B Addresses and Ranges 78 B.1 Single Numeric Address . . . . . . . . . . . . . . . . . . . . 78 B.2 Single Regex Address . . . . . . . . . . . . . . . . . . . . . . 78 B.3 Pair of Numeric Addresses . . . . . . . . . . . . . . . . . . . 79 B.4 Pair of Regex Addresses . . . . . . . . . . . . . . . . . . . . 79 B.5 Special Address $ . . . . . . . . . . . . . . . . . . . . . . . . 79 B.6 Stepping Address . . . . . . . . . . . . . . . . . . . . . . . . 80 Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 7. CONTENTS vi B.7 Match N Lines After the Address . . . . . . . . . . . . . . . 80 B.8 Combining Addresses . . . . . . . . . . . . . . . . . . . . . . 80 C Debugging sed Scripts with sed-sed 81 Index 88 Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 8. Preface Thanks! Thank you for purchasing my "Sed One-Liners Explained" e-book! This is my second e-book and I based it on the "Famous Sed One-Liners Explained" article series that I wrote on my www.catonmat.net blog. I went through all the one-liners in the articles, improved them, added 8 new ones (bringing the total count to 100), fixed a lot of mistakes, added an Introduction to sed one-liners and three new chapters. The three new chapters are Sed commands that summarizes all the sed commands, Sed addresses and ranges that summarizes the sed ranges, and Debugging sed scripts with sed-sed that shows how useful the sed-sed utility is. You might wonder why I called the article series "famous"? Well, be- cause I based the articles on the famous sed1line.txt file by Eric Pement. This file has been circulating around Unix newsgroups and forums for years and it’s very popular among Unix programmers. That’s how I actually re- ally learned sed myself. I went through all the one-liners in this file, tried them out and endeavored to understand exactly how they work. Then I decided that it would be a good idea to explain them on my blog, which I did, and after that I thought, why not turn it into a book? That’s how I ended up writing this book. As I mentioned, this is my second e-book. My first e-book is "Awk One-Liners Explained" which I based on Eric Pement’s awk1line.txt file. I really learned Awk from this file, too. My third e-book is now in development. It’s "Perl One-Liners Ex- plained". The Perl book will be based on my "Famous Perl One-Liners Explained" article series. I was so inspired by sed1line.txt and awk1line.txt files that I decided to create my own perl1line.txt file. I am still working on it but I am one chapter away from finishing it. If you’re interested, subscribe to my blog, follow me on Twitter or follow me on Google+. That vii Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 9. PREFACE viii way you’ll be the first to know when I publish perl1line.txt and my next book! Credits I’d like to thank Eric Pement who made the famous sed1line.txt file that I learned Sed from and that I based this book on. I’d also like to thank waldner and DJ Mills from #sed channel on FreeNode IRC network for always helping me with sed. Also thanks goes to Madars Virza for the book review, Przemysław Pawełczyk for teaching me LaTeX, Abraham Alhashmy for advice on how to improve the design of the book, Blake Sitney for proof- reading the book, everyone who commented on my blog while I was writing the sed one-liners article series, and everyone else who helped me with sed and this book. Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 10. One Introduction 1.1 Sed One-Liners Mastering sed can be reduced to understanding and manipulating the four spaces of sed. These four spaces are: • Input Stream • Pattern Space • Hold Buffer • Output Stream Think about the spaces this way – sed reads the input stream and pro- duces the output stream. Internally it has the pattern space and the hold buffer. Sed reads data from the input stream until it finds the newline character n. Then it places the data read so far, without the newline, into the pattern space. Most of the sed commands operate on the data in the pattern space. The hold buffer is there for your convenience. Think about it as temporary buffer. You can copy or exchange data between the pattern space and the hold buffer. Once sed has executed all the commands, it outputs the pattern space and adds a n at the end. It’s possible to modify the behavior of sed with the -n command line switch. When -n is specified, sed doesn’t output the pattern space and you have to explicitly print it with either p or P commands. Let’s look at several examples to understand the four spaces and sed. You’ll learn much more about the sed commands and spaces as you work through the chapters of the book. These are just examples to illustrate what sed looks like and what it’s all about. Here is the simplest possible sed program: 1 Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 11. CHAPTER 1. INTRODUCTION 2 sed 's/foo/bar/' This program replaces text "foo" with "bar" on every line. Here is how it works. Suppose you have a file with these lines: abc foo 123-foo-456 Sed opens the file as the input stream and starts reading the data. After reading "abc" it finds a newline n. It places the text "abc" in the pattern space and now it applies the s/foo/bar/ command. Since we have "abc" in the pattern space and there is no "foo" anywhere, sed does nothing to the pattern space. At this moment sed has executed all the commands (in this case just one). The default action when all the commands have been executed is to print the pattern space, followed by newline. So the output from the first line is "abcn". Now sed reads in the second line "foo" and executes s/foo/bar/. This replaces "foo" with "bar". The pattern space now contains just "bar". The end of the script has been reached and sed prints out the pattern space, followed by newline. The output from the second line is "barn". Now the 3rd line is read in. The pattern space is now "123-foo-456". Since there is "foo" in the text, the s/foo/bar/ is successful and the pattern space is now "123-bar-456". The end is reached and sed prints the pattern space. The output is "123-bar-456n". All the lines of the input have been read at this moment and sed exits. The output from running the script on our example file is: abc bar 123-bar-456 In this example we never used the hold buffer because there was no need for temporary storage. Before we look at an example with temporary storage, let’s take a look at three command line switches – -n, -e and -i. First -n. If you specify -n to sed, like this: Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 12. CHAPTER 1. INTRODUCTION 3 sed -n 's/foo/bar/' Then sed will no longer print the pattern space when it reaches the end of the script. So if you run this program on our sample file above, there will be no output. You must use sed’s p command to force sed to print the line: sed -n 's/foo/bar/; p' As you can see, sed commands are separated by the ; character. You can also use -e switch to separate the commands: sed -n -e 's/foo/bar/' -e 'p' It’s the same as if you used ;. Next, let’s take a look at the -i command line argument. This one forces sed to do in-place editing of the file, meaning it reads the contents of the file, executes the commands, and places the new contents back in the file. Here is an example. Suppose you have a file called "users", with the following content: pkrumins:hacker esr:guru rms:geek And you wish to replace the ":" symbol with ";" in the whole file. Then you can do it as easily as: sed -i 's/:/;/' users It will silently execute the s/:/;/ command on all lines in the file and do all substitutions. Be very careful when using -i as it’s destructive and it’s not reversible! It’s always safer to run sed without -i, and then replace the file yourself. Actually, before we look at the hold buffer, let’s take a look at addresses and ranges. Addresses allow you to restrict sed commands to certain lines, or ranges of lines. Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 13. CHAPTER 1. INTRODUCTION 4 The simplest address is a single number that limits sed commands to the given line number: sed '5s/foo/bar/' This limits the s/foo/bar/ only to the 5th line of file or input stream. So if there is a "foo" on the 5th line, it will be replaced with "bar". No other lines will be touched. The addresses can be also inverted with the ! after the address. To match all lines that are not the 5th line (lines 1-4, plus lines 6-...), do this: sed '5!s/foo/bar/' The inversion can be applied to any address. Next, you can also limit sed commands to a range of lines by specifying two numbers, separated by a comma: sed '5,10s/foo/bar/' In this one-liner the s/foo/bar/ is executed only on lines 5 – 10, in- clusive. Here is a quick, useful one-liner. Suppose you want to print lines 5 – 10 in the file. You can first disable implicit line printing with the -n command line switch, and then use the p command on lines 5 – 10: sed -n '5,10p' This will execute the p command only on lines 5 – 10. No other lines will be output. Pretty neat, isn’t it? There is a special address $ that matches the last line of the file. Here is an example that prints the last line of the file: sed -n '$p' As you can see, the p command has been limited to $, which is the last line of input. Next, there is also a single regular expression address match like this /regex/. If you specify a regex before a command, then the command will only get executed on lines that match the regex. Check this out: Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 14. CHAPTER 1. INTRODUCTION 5 sed -n '/a+b+/p' Here the p command will get called only on lines that match a+b+ reg- ular expression, which means one or more letters "a" followed by one or more letters "b". For example, it prints lines like "ab", "aab", "aaabbbbb", "foo-123-ab", etc. There is also an expression to match a range between two regexes. Here is an example, sed '/foo/,/bar/d' This one-liner matches all lines between the first line that matches "/foo/" regex and the first line that matches "/bar/" regex, inclusive. It applies the d command that stands for delete. In other words, it deletes a range of lines between the first line that matches "/foo/" and the first line after "/foo/" that matches "/bar/", inclusive. Now let’s take a look at the hold buffer. Suppose you have a problem where you want to print the line before the line that matches a regular expression. How do you do this? If sed didn’t have a hold buffer, things would be tough, but with hold buffer we can always save the current line to the hold buffer, and then let sed read in the next line. Now if the next line matches the regex, we would just print the hold buffer, which holds the previous line. Easy, right? The command for copying the current pattern space to the hold buffer is h. The command for copying the hold buffer back to the pattern space is g. The command for exchanging the hold buffer and the pattern space is x. We just have to choose the right commands to solve this problem. Here is the solution: sed -n '/regex/{x;p;x}; h' It works this way – every line gets copied to the hold buffer with the h command at the end of the script. However, for every line that matches the /regex/, we exchange the hold buffer with the pattern space by using the x command, print it with the p command, and then exchange the buffers back, so that if the next line matches the /regex/ again, we could print the current line. Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 15. CHAPTER 1. INTRODUCTION 6 Also notice the command grouping. Several commands can be grouped and executed only for a specific address or range. In this one-liner the command group is {x;p;x} and it gets executed only if the current line matches /regex/. Note that this one-liner doesn’t work if it’s the first line of the input matches /regex/. To fix this, we can limit the p command to all lines that are not the first line with the 1! inverted address match: sed -n '/regex/{x;1!p;x}; h' Notice the 1!p. This says – call the p command on all the lines that are not the 1st line. This prevents anything to be printed in case the first line matches /regex/. I think that this is a great introduction to sed one-liners that covers the most important concepts in sed. Overall this book contains exactly 100 well-explained one-liners. Once you work through them, you’ll have rewired your brain to "think in sed". In other words, you’ll have learned how to manipulate the pattern space, the hold buffer and you’ll know when to print the data to get the results that you need. Enjoy this book! Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 16. Two Line Spacing 2.1 Double-space a file sed G This sed one-liner uses the G command. The G command appends a newline n followed by the contents of the hold buffer to the pattern space. In this one-liner the hold buffer is empty all the time (only three commands - h, H and x modify the hold buffer), so we end up simply appending a newline to the pattern space for every line. Once all the commands have been processed (in this case just the G command), sed sends the contents of pattern space to the output stream followed by a newline. So there we have it, each line now ends with two newlines – one added by the G command and the other implicitly by sed’s printing mechanism. File has been double spaced. 2.2 Double-space a file which already has blank lines in it sed '/^$/d;G' Sed allows the commands to be restricted only to certain lines. This one-liner operates only on lines that match the regular expression /^$/. But which are those? Those are the empty lines, because only the empty lines have a property that the beginning of the line ^ is the same as the end of the line $. Remember that before doing the regular expression match sed pushes the input line to pattern space. When doing it, sed strips the trailing 7 Preview copy (first 16 pages) Get full e-book at www.catonmat.net/blog/sed-book/
  • 17. This is only the e-book preview. Get the full e-book at: http://www.catonmat.net/blog/sed-book/