|
awk commandsLast revision August 6, 2004
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:
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:
$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
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:
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,
|