Sed by examples, Part 2

posted by Arkham on June 29th, 2009

sed_logo_white

sed (stream editor) is a Unix utility which (a) parses text files and (b) implements a programming language which can apply textual transformations to such files.
It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line.

Substitution

  1. Substitute, for every line, the first occurence of foo (if any) with spam:

    # test.txt
    foo bar spam foo
    spam foo bar egg
    sausage foo spam egg
    spam spam spam
    sed -e 's/foo/spam/' test.txt
  2. Substitute every occurence of foo with spam:

    sed -e 's/foo/spam/g' test.txt
  3. Substitute every occurence of foo with spam in the first two lines:

    sed -e '1,2s/foo/spam/g' test.txt
  4. Substitute every occurence of foo with spam in every line that starts with sausage and ends with egg:

    sed -e '/^sausage.*egg$/s/foo/spam/g' test.txt
  5. Substitute paths:

    sed -e 's|/usr/bin/|/usr/local/bin|g' script.sh
  6. Remove html tags:

    <html>
    <body>
    Hello World!
    </body>
    </html>
    sed -e 's/<[^>]*>//g' hello.html

Advanced substitution

  1. Append (that’s what she said) to every line ( & represents what matches):

    sed -e "s/^.*$/& (that's what she said)/g" test.txt
  2. Append lol, rofl and lmao to the first three words of every line:

    sed -e 's/\(^[^ ]*\) \([^ ]*\) \([^ ]*\)/\1lol \2rofl \3lmao/' test.txt

Multiple Commands

  1. Print a file alternating row numbers:

    sed -n -e '=;p' test.txt
  2. Print some information about your cpu first core:

    # script.sed
    1,/^$/{
        /model name/p
        /flags/p
        /bogomips/p
    }
    sed -n -f script.sed /proc/cpuinfo
  3. Add a line before/after each line or replace line:

    # script.sed
    i\
    This line will be inserted before each line
    # script.sed
    a\
    This line will be inserted after each line
    # script.sed
    c\
    This line will be inserted in each line
    sed -f script.sed test.txt
  4. Tags: , , , ,

Sed by examples

posted by Arkham on June 29th, 2009

Canon Tosh SED

sed (stream editor) is a Unix utility which (a) parses text files and (b) implements a programming language which can apply textual transformations to such files.
It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line.

Delete

  1. Delete the first line of a file:

    sed -e '1d' /etc/fstab
  2. Delete from the second to the tenth line:

    sed -e '2,10d' /etc/fstab
  3. Delete lines starting with #:

    sed -e '/^#/d' /etc/fstab

Print

  1. Print lines starting with #:

    sed -n -e '/^#/p' /etc/fstab
  2. Print blocks of text enclosed by BEGIN and END:

    sed -n -e '/BEGIN/,/END/p' script.awk
  3. Print Device section in xorg.conf:

    sed -n -e '/Section "Device"/,/EndSection/p' /etc/X11/xorg.conf
  4. Print main function in a C source file:

    sed -n -e '/main[[:space:]]*(/,/^}/p' source.c

Tags: , , , ,