1AWK(1)                      General Commands Manual                     AWK(1)
2
3
4

NAME

6       awk - pattern scanning and processing language
7

SYNOPSIS

9       awk [ -Fc ] [ prog ] [ file ] ...
10

DESCRIPTION

12       Awk scans each input file for lines that match any of a set of patterns
13       specified in prog.  With each pattern in prog there can be  an  associ‐
14       ated  action  that  will be performed when a line of a file matches the
15       pattern.  The set of patterns may appear literally as  prog,  or  in  a
16       file specified as -f file.
17
18       Files  are  read in order; if there are no files, the standard input is
19       read.  The file name `-'  means  the  standard  input.   Each  line  is
20       matched  against the pattern portion of every pattern-action statement;
21       the associated action is performed for each matched pattern.
22
23       An input line is made up of fields separated  by  white  space.   (This
24       default  can  be  changed  by  using  FS,  vide infra.)  The fields are
25       denoted $1, $2, ... ; $0 refers to the entire line.
26
27       A pattern-action statement has the form
28
29            pattern { action }
30
31       A missing { action } means print the line;  a  missing  pattern  always
32       matches.
33
34       An  action  is a sequence of statements.  A statement can be one of the
35       following:
36
37            if ( conditional ) statement [ else statement ]
38            while ( conditional ) statement
39            for ( expression ; conditional ; expression ) statement
40            break
41            continue
42            { [ statement ] ... }
43            variable = expression
44            print [ expression-list ] [ >expression ]
45            printf format [ , expression-list ] [ >expression ]
46            next # skip remaining patterns on this input line
47            exit # skip the rest of the input
48
49       Statements are terminated by semicolons, newlines or right braces.   An
50       empty  expression-list  stands for the whole line.  Expressions take on
51       string or numeric values as appropriate, and are built using the opera‐
52       tors  +,  -, *, /, %,  and concatenation (indicated by a blank).  The C
53       operators ++, --, +=, -=, *=, /=, and %= are also available in  expres‐
54       sions.   Variables  may  be  scalars,  array elements (denoted x[i]) or
55       fields.  Variables are initialized to  the  null  string.   Array  sub‐
56       scripts  may  be any string, not necessarily numeric; this allows for a
57       form of associative memory.  String constants are quoted "...".
58
59       The print statement prints its arguments on the standard output (or  on
60       a file if >file is present), separated by the current output field sep‐
61       arator, and terminated by the  output  record  separator.   The  printf
62       statement  formats  its  expression  list  according to the format (see
63       printf(3)).
64
65       The built-in function length returns the length of its  argument  taken
66       as  a  string,  or  of  the  whole line if no argument.  There are also
67       built-in functions exp, log, sqrt, and int.   The  last  truncates  its
68       argument  to  an integer.  substr(s, m, n) returns the n-character sub‐
69       string   of   s   that   begins   at   position   m.    The    function
70       sprintf(fmt, expr, expr, ...)  formats the expressions according to the
71       printf(3) format given by fmt and returns the resulting string.
72
73       Patterns are arbitrary Boolean combinations (!, ||, &&,  and  parenthe‐
74       ses)  of  regular  expressions  and  relational  expressions.   Regular
75       expressions must be surrounded by slashes and are as  in  egrep.   Iso‐
76       lated regular expressions in a pattern apply to the entire line.  Regu‐
77       lar expressions may also occur in relational expressions.
78
79       A pattern may consist of two patterns separated by  a  comma;  in  this
80       case,  the  action  is performed for all lines between an occurrence of
81       the first pattern and the next occurrence of the second.
82
83       A relational expression is one of the following:
84
85            expression matchop regular-expression
86            expression relop expression
87
88       where a relop is any of the  six  relational  operators  in  C,  and  a
89       matchop  is  either  ~  (for contains) or !~ (for does not contain).  A
90       conditional is an arithmetic expression, a relational expression, or  a
91       Boolean combination of these.
92
93       The  special  patterns  BEGIN  and  END  may be used to capture control
94       before the first input line is read and after the last.  BEGIN must  be
95       the first pattern, END the last.
96
97       A single character c may be used to separate the fields by starting the
98       program with
99
100            BEGIN { FS = "c" }
101
102       or by using the -Fc option.
103
104       Other variable names with special meanings include NF,  the  number  of
105       fields  in  the  current  record; NR, the ordinal number of the current
106       record; FILENAME, the name of the current input file; OFS,  the  output
107       field  separator  (default  blank);  ORS,  the  output record separator
108       (default newline); and OFMT, the output  format  for  numbers  (default
109       "%.6g").
110

EXAMPLES

112       Print lines longer than 72 characters:
113
114            length > 72
115
116       Print first two fields in opposite order:
117
118            { print $2, $1 }
119
120       Add up first column, print sum and average:
121
122                 { s += $1 }
123            END  { print "sum is", s, " average is", s/NR }
124
125       Print fields in reverse order:
126
127            { for (i = NF; i > 0; --i) print $i }
128
129       Print all lines between start/stop pairs:
130
131            /start/, /stop/
132
133       Print all lines whose first field is different from previous one:
134
135            $1 != prev { print; prev = $1 }
136

SEE ALSO

138       lex(1), sed(1)
139       A.  V. Aho, B. W. Kernighan, P. J. Weinberger, Awk - a pattern scanning
140       and processing language
141

BUGS

143       There are no explicit conversions  between  numbers  and  strings.   To
144       force  an expression to be treated as a number add 0 to it; to force it
145       to be treated as a string concatenate "" to it.
146
147
148
149                                                                        AWK(1)
Impressum