Contents | < Browse | Browse >
A Very Simple Example
=====================
The following command runs a simple `awk' program that searches the
input file `BBS-list' for the string of characters: `foo'. (A string
of characters is usually called, a "string". The term "string" is
perhaps based on similar usage in English, such as "a string of
pearls," or, "a string of cars in a train.")
awk '/foo/ { print $0 }' BBS-list
When lines containing `foo' are found, they are printed, because
`print $0' means print the current line. (Just `print' by itself means
the same thing, so we could have written that instead.)
You will notice that slashes, `/', surround the string `foo' in the
actual `awk' program. The slashes indicate that `foo' is a pattern to
search for. This type of pattern is called a "regular expression", and
is covered in more detail later (Regexp).
There are single-quotes around the `awk' program so that the
shell won't interpret any of it as special shell characters.
Here is what this program prints:
fooey 555-1234 2400/1200/300 B
foot 555-6699 1200/300 B
macfoo 555-6480 1200/300 A
sabafoo 555-2127 1200/300 C
In an `awk' rule, either the pattern or the action can be omitted,
but not both. If the pattern is omitted, then the action is performed
for *every* input line. If the action is omitted, the default action
is to print all lines that match the pattern.
Thus, we could leave out the action (the `print' statement and the
curly braces) in the above example, and the result would be the same:
all lines matching the pattern `foo' would be printed. By comparison,
omitting the `print' statement but retaining the curly braces makes an
empty action that does nothing; then no lines would be printed.