awk commands
Last revision August 6, 2004
Table of Contents: |
You can supply commands for awk on the shell command line as arguments, generally enclosed in quotes (to protect against shell interpretation of special characters). But unless you have very simple commands, it is generally easier to put them into a "script" file. This script file is then referenced by the -f option on the command line.
The basic syntax for an
awk
command is:
pattern {action}
For each line in the input file that matches pattern, the action(s) within the braces are executed. The curly braces are required around the action(s). An input line may match more than one pattern, in which case the corresponding actions are executed in turn.
Either the pattern or the action may be omitted in a single command, but obviously not both.
If the pattern is omitted (command consists solely of an action in braces), then all lines match, and the action is applied to each line in turn.
If the action is omitted (command consists solely of a pattern), then the default action is to copy only the input lines that match the pattern to the output stream without any changes, discarding the lines that do not match.
If included directly on the shell command line, separate multiple awk commands with semi-colons (;). If using a script file, generally you would start each command on a new line, or you can put multiple commands on the same line separated by semi-colons.
If a single complicated command needs to extend to two or more lines in the script, put a backslash as the last character of each line that is continued. Example:
-
{print "First part of long string to print",$1 \
$2, "same print command continued", \
$3, "end of print command - no backslash."}
Comment lines may be included in a script file. These must start with the "hash mark" (#) comment character. They are not executed by awk. They are simply a way for the author to include additional information about what he/she is trying to do.
If you save your
awk
script in a file, it can be made into a "standalone" program.
That is, rather than running it with a command like
awk -f scriptfile
you can make it possible to run it directly, just like a shell
script, with the simple command
scriptfile
To do this, first add this line as the
very first
line of the script file:
#! /bin/awk -f
This line tells the Unix kernel that this file is a script that
should be executed by the
/bin/awk program.
If the
awk
program is not located in the
/bin
directory on your system, then subsitute the correct complete
pathname to the program in the first line of your script, for example,
#! /usr/bin/awk -f
Secondly, make sure that the execute permission is set for the
script file. For example, to make a script stored in the file
scriptfile
executable by any acccount, use this command:
chmod a+x scriptfile
Finally, the shell must be able to find your script file when you type its
name as a command. The C-shell looks only in a predefined set of directories,
called your "path", to find programs (or scripts) to run. On pangea, the current
directory is part of that path, so you can run programs that are located in the
current directory. Otherwise, you should specify the actual pathname of the script
to run it, for example,
~farrell/project/myscript