11.2. Command-Line Syntax The syntax for invoking awk has two forms: awk [options] 'script' var=value file(s) awk [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. gawk allows multiple -f scripts. Variables can be assigned a value on the command line. The value can be a string or numeric constant, a shell variable ($name), or a command substitution ('cmd'), but the value is available only after the BEGIN statement is executed. awk operates on one or more files. If none are specified (or if - is specified), awk reads from standard input. 11.2.1. Standard Options The standard options are:
-Ffs Set the field separator to fs. This is the same as setting the built-in variable FS. gawk allows fs to be a regular expression. Each input line, or record, is divided into fields by whitespace (spaces or tabs) or by some other user-definable field separator. Fields are referred to by the variables $1, $2,..., $n. $0 refers to the entire record.
-v var=value Assign a value to variable var. This allows assignment before the script begins execution. For example, to print the first three (colon-separated) fields of each record on separate lines: awk -F: '{ print $1; print $2; print $3 }' /etc/passwd Numerous examples are shown later in Simple Pattern-Procedure Examples." 11.2.2. Important gawk Options Besides the standard command line options, gawk has a large number of additional options. This section lists those of most value in day-to-day use. Any unique abbreviation of these options is acceptable.
--dump-variables[=file] When the program has finished running, print a sorted list of global variables, their types, and their final values to file. The default file is awkvars.out.
--gen-po Read the awk program and print all strings marked as translatable to standard output in the form of a GNU gettext Portable Object file. See the section Internationalization," later in this chapter, for more information.
--help Print a usage message to standard error and exit.
--lint[=fatal] Enable checking of nonportable or dubious constructs, both when the program is read and as it runs. With an argument of fatal, lint warnings become fatal errors.
--non-decimal-data Allow octal and hexadecimal data in the input to be recognized as such. This option is not recommended; use strtonum( ) in your program, instead.
--profile[=file] With gawk, put a "prettyprinted" version of the program in file. Default is awkprof.out. With pgawk (see Profiling," later in this chapter), put the profiled listing of the program in file.
--posix Turn on strict POSIX compatibility, in which all common and gawk-specific extensions are disabled.
--source='program text' Use program text as the awk source code. Use this option with -f to mix command-line programs with awk library files.
--traditional Disable all gawk-specific extensions, but allow common extensions (e.g., the ** operator for exponentiation).
--version Print the version of gawk on standard error and exit. |