11.2. Command-Line Syntax

 < Day Day Up > 

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. POSIX awk 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 the 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. POSIX awk allows fs to be a regular expression. Each input line, or record, is divided into fields by white space (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

Many examples are shown later in the section "Simple Pattern-Action 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 that are 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 and their types and 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 later section "Internationalization," 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 the "Profiling" section 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.

     < Day Day Up > 


    Unix in a Nutshell
    Unix in a Nutshell, Fourth Edition
    ISBN: 0596100299
    EAN: 2147483647
    Year: 2005
    Pages: 201

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net