gawk: Very Simple
1
1 1.3 Some Simple Examples
1 ========================
1
1 The following command runs a simple 'awk' program that searches the
1 input file 'mail-list' for the character string 'li' (a grouping of
1 characters is usually called a "string"; the term "string" is based on
1 similar usage in English, such as "a string of pearls" or "a string of
1 cars in a train"):
1
1 awk '/li/ { print $0 }' mail-list
1
1 When lines containing 'li' are found, they are printed because
1 'print $0' means print the current line. (Just 'print' by itself means
1 the same thing, so we could have written that instead.)
1
1 You will notice that slashes ('/') surround the string 'li' in the
1 'awk' program. The slashes indicate that 'li' is the pattern to search
1 for. This type of pattern is called a "regular expression", which is
1 covered in more detail later (⇒Regexp). The pattern is allowed
1 to match parts of words. There are single quotes around the 'awk'
1 program so that the shell won't interpret any of it as special shell
1 characters.
1
1 Here is what this program prints:
1
1 $ awk '/li/ { print $0 }' mail-list
1 -| Amelia 555-5553 amelia.zodiacusque@gmail.com F
1 -| Broderick 555-0542 broderick.aliquotiens@yahoo.com R
1 -| Julie 555-6699 julie.perscrutabor@skeeve.com F
1 -| Samuel 555-3430 samuel.lanceolis@shu.edu A
1
1 In an 'awk' rule, either the pattern or the action can be omitted,
1 but not both. If the pattern is omitted, then the action is performed
1 for _every_ input line. If the action is omitted, the default action is
1 to print all lines that match the pattern.
1
1 Thus, we could leave out the action (the 'print' statement and the
1 braces) in the previous example and the result would be the same: 'awk'
1 prints all lines matching the pattern 'li'. By comparison, omitting the
1 'print' statement but retaining the braces makes an empty action that
1 does nothing (i.e., no lines are printed).
1
1 Many practical 'awk' programs are just a line or two long. Following
1 is a collection of useful, short programs to get you started. Some of
1 these programs contain constructs that haven't been covered yet. (The
1 description of the program will give you a good idea of what is going
1 on, but you'll need to read the rest of the Info file to become an 'awk'
1 expert!) Most of the examples use a data file named 'data'. This is
1 just a placeholder; if you use these programs yourself, substitute your
1 own file names for 'data'. For future reference, note that there is
1 often more than one way to do things in 'awk'. At some point, you may
1 want to look back at these examples and see if you can come up with
1 different ways to do the same things shown here:
1
1 * Print every line that is longer than 80 characters:
1
1 awk 'length($0) > 80' data
1
1 The sole rule has a relational expression as its pattern and has no
1 action--so it uses the default action, printing the record.
1
1 * Print the length of the longest input line:
1
1 awk '{ if (length($0) > max) max = length($0) }
1 END { print max }' data
1
1 The code associated with 'END' executes after all input has been
1 read; it's the other side of the coin to 'BEGIN'.
1
1 * Print the length of the longest line in 'data':
1
1 expand data | awk '{ if (x < length($0)) x = length($0) }
1 END { print "maximum line length is " x }'
1
1 This example differs slightly from the previous one: the input is
1 processed by the 'expand' utility to change TABs into spaces, so
1 the widths compared are actually the right-margin columns, as
1 opposed to the number of input characters on each line.
1
1 * Print every line that has at least one field:
1
1 awk 'NF > 0' data
1
1 This is an easy way to delete blank lines from a file (or rather,
1 to create a new file similar to the old file but from which the
1 blank lines have been removed).
1
1 * Print seven random numbers from 0 to 100, inclusive:
1
1 awk 'BEGIN { for (i = 1; i <= 7; i++)
1 print int(101 * rand()) }'
1
1 * Print the total number of bytes used by FILES:
1
1 ls -l FILES | awk '{ x += $5 }
1 END { print "total bytes: " x }'
1
1 * Print the total number of kilobytes used by FILES:
1
1 ls -l FILES | awk '{ x += $5 }
1 END { print "total K-bytes:", x / 1024 }'
1
1 * Print a sorted list of the login names of all users:
1
1 awk -F: '{ print $1 }' /etc/passwd | sort
1
1 * Count the lines in a file:
1
1 awk 'END { print NR }' data
1
1 * Print the even-numbered lines in the data file:
1
1 awk 'NR % 2 == 0' data
1
1 If you used the expression 'NR % 2 == 1' instead, the program would
1 print the odd-numbered lines.
1