Appendix B. A Sed and Awk Micro-Primer


sed: a non-interactive text file editor

awk: a field-oriented pattern processing language with a C-like syntax

For all their differences, the two utilities share a similar invocation syntax, both use regular expressions , both read input by default from stdin, and both output to stdout. These are well-behaved UNIX tools, and they work together well. The output from one can be piped into the other, and their combined capabilities give shell scripts some of the power of Perl.


One important difference between the utilities is that while shell scripts can easily pass arguments to sed, it is more complicated for awk (see Example 34-3 and Example 9-22).

B.1. Sed


Sed determines which lines of its input that it will operate on from the address range passed to it. [1] Specify this address range either by line number or by a pattern to match. For example, 3d signals sed to delete line 3 of the input, and /windows/d tells sed that you want every line of the input containing a match to "windows" deleted.


Table B-1. Basic sed operators

[address-range]/pprintPrint [specified address range]
[address-range]/ddeleteDelete [specified address range]
gglobalOperate on every pattern match within each matched line of input



From the command line and in a shell script, a sed operation may require quoting and certain options.

   3 #  (If passing only a single instruction to "sed", the "-e" is optional.)
   7 #
   8 # Operates on the text contained in file $filename.


   1 filename=file1.txt
   2 pattern=BEGIN



   1 sed -n '/xzy/p' $filename
   3 # Otherwise all input lines would print.

Table B-2. Examples

8dDelete 8th line of input.
/^$/dDelete all blank lines.
/GUI/dDelete all lines containing "GUI".

results in
 The most important parts of any application are its  and sound effects

The backslash represents a newline as a substitution character. In this special case, the replacement expression continues on the next line.
   1 s/^  */\
   2 /g

   2 /^$/d
   3 }



For illustrative examples of sed within shell scripts, see:

  1. Example 34-1

  2. Example 34-2

  3. Example 12-2

  4. Example A-3

  5. Example 12-12

  6. Example 12-20

  7. Example A-13

  8. Example A-18

  9. Example 12-24

  10. Example 10-9

  11. Example 12-33

  12. Example A-2

  13. Example 12-10

  14. Example 12-8

  15. Example A-11

  16. Example 17-11



