Sed by examples
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
-
Delete the first line of a file:
sed -e '1d' /etc/fstab
-
Delete from the second to the tenth line:
sed -e '2,10d' /etc/fstab
-
Delete lines starting with #:
sed -e '/^#/d' /etc/fstab
-
Print lines starting with #:
sed -n -e '/^#/p' /etc/fstab
-
Print blocks of text enclosed by BEGIN and END:
sed -n -e '/BEGIN/,/END/p' script.awk
-
Print Device section in xorg.conf:
sed -n -e '/Section "Device"/,/EndSection/p' /etc/X11/xorg.conf
-
Print main function in a C source file:
sed -n -e '/main[[:space:]]*(/,/^}/p' source.c
