Book HomeLinux in a NutshellSearch this book

13.2. 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/passwd

Numerous examples are shown later in Section 13.3, "Patterns and Procedures".

13.2.1. 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.

-Fc, --field-separator=c

Set the field separator to character c. This is the same as setting the variable FS. c may be a regular expression. Each input line, or record, is divided into fields by whitespace (blanks or tabs) or by some other user-definable record separator. Fields are referred to by the variables $1, $2,..., $n. $0 refers to the entire record.

-W option

All -W options are specific to gawk, as opposed to awk. An alternate syntax is --option (i.e., --compat). option may be one of:

compat

Same as traditional.

copyleft

Print copyleft notice and exit.

copyright

Same as copyleft.

help

Print syntax and list of options, then exit.

lint

Warn about commands that might not port to other versions of awk or that gawk considers problematic.

lint-old

Like lint but compares to an older version of awk.

posix

Expect exact compatibility with POSIX; additionally, ignore \x escape sequences, **, and **=.

re-interval

Allow use of {n,m} intervals in regular expressions.

source=script

Treat script as gawk commands. Like the 'script' argument but lets you mix commands from files (using -f options) with commands on the gawk command line.

traditional

Behave exactly like traditional (non-GNU) awk.

usage

Same as help.

version

Print version information and exit.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.

This HTML Help has been published using the chm2web software.