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

This entry was posted on Monday, June 29th, 2009 at 00:16 and is filed under computers. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Tags: , , , ,

Comments are closed.