Linux in a Nutshell, Third Edition
by Ellen Siever, Stephen Spainhour, Jessica P. Hekman, Stephen Figgins
Command-Line Syntax
gawk’s syntax has two forms:
gawk [options] 'script' var=value file(s) gawk [options] -f scriptfile var=value file(s)
You can specify a script directly on the command line, or you can store a script in a scriptfile and specify it with -f. Multiple -f options are allowed; awk concatenates the files. This feature is useful for including libraries.
gawk operates on one or more input files. If none are specified (or if - is specified), gawk reads from the standard input.
Variables can be assigned a value on the command line. The value assigned to a variable can be a literal, a shell variable ($ name), or a command substitution (`cmd`), but the value is available only after a line of input is read (i.e., after the BEGIN statement).
For example, to print the first three (colon-separated) fields of the password file, use -F to set the field separator to a colon:
gawk -F: '{print $1; print $2; print $3}' /etc/passwdNumerous examples are shown later in Section 13.3.
Options
All options exist in both traditional POSIX (one-letter) format and GNU-style (long) format. Some recognized options are:
- --
Treat all subsequent text as commands or filenames, not options.
- -f scriptfile, --file= scriptfile
Read gawk commands from scriptfile instead of command line.
- -v var = value, --assign= var = value
Assign a value to variable var. This allows assignment before the script begins execution.
- -F c, --field-separator= c
Set the field ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access