-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathman_awk
1829 lines (1409 loc) · 120 KB
/
man_awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
AWK(1P) POSIX Programmer's Manual AWK(1P)
PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may dif‐
fer (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be
implemented on Linux.
NAME
awk — pattern scanning and processing language
SYNOPSIS
awk [−F sepstring] [−v assignment]... program [argument...]
awk [−F sepstring] −f progfile [−f progfile]... [−v assignment]...
[argument...]
DESCRIPTION
The awk utility shall execute programs written in the awk programming language, which is specialized for tex‐
tual data manipulation. An awk program is a sequence of patterns and corresponding actions. When input is read
that matches a pattern, the action associated with that pattern is carried out.
Input shall be interpreted as a sequence of records. By default, a record is a line, less its terminating <new‐
line>, but this can be changed by using the RS built-in variable. Each record of input shall be matched in turn
against each pattern in the program. For each pattern matched, the associated action shall be executed.
The awk utility shall interpret each input record as a sequence of fields where, by default, a field is a
string of non-<blank> non-<newline> characters. This default <blank> and <newline> field delimiter can be
changed by using the FS built-in variable or the −F sepstring option. The awk utility shall denote the first
field in a record $1, the second $2, and so on. The symbol $0 shall refer to the entire record; setting any
other field causes the re-evaluation of $0. Assigning to $0 shall reset the values of all other fields and the
NF built-in variable.
OPTIONS
The awk utility shall conform to the Base Definitions volume of POSIX.1‐2008, Section 12.2, Utility Syntax
Guidelines.
The following options shall be supported:
−F sepstring
Define the input field separator. This option shall be equivalent to:
-v FS=sepstring
except that if −F sepstring and −v FS=sepstring are both used, it is unspecified whether the FS
assignment resulting from −F sepstring is processed in command line order or is processed after the
last −v FS=sepstring. See the description of the FS built-in variable, and how it is used, in the
EXTENDED DESCRIPTION section.
−f progfile
Specify the pathname of the file progfile containing an awk program. A pathname of '−' shall denote
the standard input. If multiple instances of this option are specified, the concatenation of the
files specified as progfile in the order specified shall be the awk program. The awk program can
alternatively be specified in the command line as a single argument.
−v assignment
The application shall ensure that the assignment argument is in the same form as an assignment oper‐
and. The specified variable assignment shall occur prior to executing the awk program, including the
actions associated with BEGIN patterns (if any). Multiple occurrences of this option can be speci‐
fied.
OPERANDS
The following operands shall be supported:
program If no −f option is specified, the first operand to awk shall be the text of the awk program. The
application shall supply the program operand as a single argument to awk. If the text does not end
in a <newline>, awk shall interpret the text as if it did.
argument Either of the following two types of argument can be intermixed:
file A pathname of a file that contains the input to be read, which is matched against the set
of patterns in the program. If no file operands are specified, or if a file operand is '−',
the standard input shall be used.
assignment
An operand that begins with an <underscore> or alphabetic character from the portable char‐
acter set (see the table in the Base Definitions volume of POSIX.1‐2008, Section 6.1, Por‐
table Character Set), followed by a sequence of underscores, digits, and alphabetics from
the portable character set, followed by the '=' character, shall specify a variable assign‐
ment rather than a pathname. The characters before the '=' represent the name of an awk
variable; if that name is an awk reserved word (see Grammar) the behavior is undefined. The
characters following the <equals-sign> shall be interpreted as if they appeared in the awk
program preceded and followed by a double-quote ('"') character, as a STRING token (see
Grammar), except that if the last character is an unescaped <backslash>, it shall be inter‐
preted as a literal <backslash> rather than as the first character of the sequence "\"".
The variable shall be assigned the value of that STRING token and, if appropriate, shall be
considered a numeric string (see Expressions in awk), the variable shall also be assigned
its numeric value. Each such variable assignment shall occur just prior to the processing
of the following file, if any. Thus, an assignment before the first file argument shall be
executed after the BEGIN actions (if any), while an assignment after the last file argument
shall occur before the END actions (if any). If there are no file arguments, assignments
shall be executed before processing the standard input.
STDIN
The standard input shall be used only if no file operands are specified, or if a file operand is '−', or if a
progfile option-argument is '−'; see the INPUT FILES section. If the awk program contains no actions and no
patterns, but is otherwise a valid awk program, standard input and any file operands shall not be read and awk
shall exit with a return status of zero.
INPUT FILES
Input files to the awk program from any of the following sources shall be text files:
* Any file operands or their equivalents, achieved by modifying the awk variables ARGV and ARGC
* Standard input in the absence of any file operands
* Arguments to the getline function
Whether the variable RS is set to a value other than a <newline> or not, for these files, implementations shall
support records terminated with the specified separator up to {LINE_MAX} bytes and may support longer records.
If −f progfile is specified, the application shall ensure that the files named by each of the progfile option-
arguments are text files and their concatenation, in the same order as they appear in the arguments, is an awk
program.
ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of awk:
LANG Provide a default value for the internationalization variables that are unset or null. (See the Base
Definitions volume of POSIX.1‐2008, Section 8.2, Internationalization Variables for the precedence of
internationalization variables used to determine the values of locale categories.)
LC_ALL If set to a non-empty string value, override the values of all the other internationalization vari‐
ables.
LC_COLLATE
Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating
elements within regular expressions and in comparisons of string values.
LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for
example, single-byte as opposed to multi-byte characters in arguments and input files), the behavior
of character classes within regular expressions, the identification of characters as letters, and the
mapping of uppercase and lowercase characters for the toupper and tolower functions.
LC_MESSAGES
Determine the locale that should be used to affect the format and contents of diagnostic messages
written to standard error.
LC_NUMERIC
Determine the radix character used when interpreting numeric input, performing conversions between
numeric and string values, and formatting numeric output. Regardless of locale, the <period> charac‐
ter (the decimal-point character of the POSIX locale) is the decimal-point character recognized in
processing awk programs (including assignments in command line arguments).
NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES.
PATH Determine the search path when looking for commands executed by system(expr), or input and output
pipes; see the Base Definitions volume of POSIX.1‐2008, Chapter 8, Environment Variables.
In addition, all environment variables shall be visible via the awk variable ENVIRON.
ASYNCHRONOUS EVENTS
Default.
STDOUT
The nature of the output files depends on the awk program.
STDERR
The standard error shall be used only for diagnostic messages.
OUTPUT FILES
The nature of the output files depends on the awk program.
EXTENDED DESCRIPTION
Overall Program Structure
An awk program is composed of pairs of the form:
pattern { action }
Either the pattern or the action (including the enclosing brace characters) can be omitted.
A missing pattern shall match any record of input, and a missing action shall be equivalent to:
{ print }
Execution of the awk program shall start by first executing the actions associated with all BEGIN patterns in
the order they occur in the program. Then each file operand (or standard input if no files were specified)
shall be processed in turn by reading data from the file until a record separator is seen (<newline> by
default). Before the first reference to a field in the record is evaluated, the record shall be split into
fields, according to the rules in Regular Expressions, using the value of FS that was current at the time the
record was read. Each pattern in the program then shall be evaluated in the order of occurrence, and the action
associated with each pattern that matches the current record executed. The action for a matching pattern shall
be executed before evaluating subsequent patterns. Finally, the actions associated with all END patterns shall
be executed in the order they occur in the program.
Expressions in awk
Expressions describe computations used in patterns and actions. In the following table, valid expression oper‐
ations are given in groups from highest precedence first to lowest precedence last, with equal-precedence oper‐
ators grouped between horizontal lines. In expression evaluation, where the grammar is formally ambiguous,
higher precedence operators shall be evaluated before lower precedence operators. In this table expr, expr1,
expr2, and expr3 represent any expression, while lvalue represents any entity that can be assigned to (that is,
on the left side of an assignment operator). The precise syntax of expressions is given in Grammar.
Table 4-1: Expressions in Decreasing Precedence in awk
┌─────────────────────┬─────────────────────────┬────────────────┬──────────────┐
│ Syntax │ Name │ Type of Result │Associativity │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│( expr ) │Grouping │Type of expr │N/A │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│$expr │Field reference │String │N/A │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│lvalue ++ │Post-increment │Numeric │N/A │
│lvalue −− │Post-decrement │Numeric │N/A │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│++ lvalue │Pre-increment │Numeric │N/A │
│−− lvalue │Pre-decrement │Numeric │N/A │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│expr ^ expr │Exponentiation │Numeric │Right │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│! expr │Logical not │Numeric │N/A │
│+ expr │Unary plus │Numeric │N/A │
│− expr │Unary minus │Numeric │N/A │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│expr * expr │Multiplication │Numeric │Left │
│expr / expr │Division │Numeric │Left │
│expr % expr │Modulus │Numeric │Left │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│expr + expr │Addition │Numeric │Left │
│expr − expr │Subtraction │Numeric │Left │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│expr expr │String concatenation │String │Left │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│expr < expr │Less than │Numeric │None │
│expr <= expr │Less than or equal to │Numeric │None │
│expr != expr │Not equal to │Numeric │None │
│expr == expr │Equal to │Numeric │None │
│expr > expr │Greater than │Numeric │None │
│expr >= expr │Greater than or equal to │Numeric │None │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│expr ~ expr │ERE match │Numeric │None │
│expr !~ expr │ERE non-match │Numeric │None │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│expr in array │Array membership │Numeric │Left │
│( index ) in array │Multi-dimension array │Numeric │Left │
│ │membership │ │ │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│expr && expr │Logical AND │Numeric │Left │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│expr || expr │Logical OR │Numeric │Left │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│expr1 ? expr2 : expr3│Conditional expression │Type of selected│Right │
│ │ │expr2 or expr3 │ │
├─────────────────────┼─────────────────────────┼────────────────┼──────────────┤
│lvalue ^= expr │Exponentiation assignment│Numeric │Right │
│lvalue %= expr │Modulus assignment │Numeric │Right │
│lvalue *= expr │Multiplication assignment│Numeric │Right │
│lvalue /= expr │Division assignment │Numeric │Right │
│lvalue += expr │Addition assignment │Numeric │Right │
│lvalue −= expr │Subtraction assignment │Numeric │Right │
│lvalue = expr │Assignment │Type of expr │Right │
└─────────────────────┴─────────────────────────┴────────────────┴──────────────┘
Each expression shall have either a string value, a numeric value, or both. Except as stated for specific con‐
texts, the value of an expression shall be implicitly converted to the type needed for the context in which it
is used. A string value shall be converted to a numeric value either by the equivalent of the following calls
to functions defined by the ISO C standard:
setlocale(LC_NUMERIC, "");
numeric_value = atof(string_value);
or by converting the initial portion of the string to type double representation as follows:
The input string is decomposed into two parts: an initial, possibly empty, sequence of white-space char‐
acters (as specified by isspace()) and a subject sequence interpreted as a floating-point constant.
The expected form of the subject sequence is an optional '+' or '−' sign, then a non-empty sequence of
digits optionally containing a <period>, then an optional exponent part. An exponent part consists of
'e' or 'E', followed by an optional sign, followed by one or more decimal digits.
The sequence starting with the first digit or the <period> (whichever occurs first) is interpreted as a
floating constant of the C language, and if neither an exponent part nor a <period> appears, a <period>
is assumed to follow the last digit in the string. If the subject sequence begins with a minus-sign, the
value resulting from the conversion is negated.
A numeric value that is exactly equal to the value of an integer (see Section 1.1.2, Concepts Derived from the
ISO C Standard) shall be converted to a string by the equivalent of a call to the sprintf function (see String
Functions) with the string "%d" as the fmt argument and the numeric value being converted as the first and only
expr argument. Any other numeric value shall be converted to a string by the equivalent of a call to the
sprintf function with the value of the variable CONVFMT as the fmt argument and the numeric value being con‐
verted as the first and only expr argument. The result of the conversion is unspecified if the value of CONVFMT
is not a floating-point format specification. This volume of POSIX.1‐2008 specifies no explicit conversions
between numbers and strings. An application can force an expression to be treated as a number by adding zero to
it, or can force it to be treated as a string by concatenating the null string ("") to it.
A string value shall be considered a numeric string if it comes from one of the following:
1. Field variables
2. Input from the getline() function
3. FILENAME
4. ARGV array elements
5. ENVIRON array elements
6. Array elements created by the split() function
7. A command line variable assignment
8. Variable assignment from another numeric string variable
and an implementation-dependent condition corresponding to either case (a) or (b) below is met.
a. After the equivalent of the following calls to functions defined by the ISO C standard, string_value_end
would differ from string_value, and any characters before the terminating null character in
string_value_end would be <blank> characters:
char *string_value_end;
setlocale(LC_NUMERIC, "");
numeric_value = strtod (string_value, &string_value_end);
b. After all the following conversions have been applied, the resulting string would lexically be recognized
as a NUMBER token as described by the lexical conventions in Grammar:
-- All leading and trailing <blank> characters are discarded.
-- If the first non-<blank> is '+' or '−', it is discarded.
-- Each occurrence of the decimal point character from the current locale is changed to a <period>.
In case (a) the numeric value of the numeric string shall be the value that would be returned by the strtod()
call. In case (b) if the first non-<blank> is '−', the numeric value of the numeric string shall be the nega‐
tion of the numeric value of the recognized NUMBER token; otherwise, the numeric value of the numeric string
shall be the numeric value of the recognized NUMBER token. Whether or not a string is a numeric string shall be
relevant only in contexts where that term is used in this section.
When an expression is used in a Boolean context, if it has a numeric value, a value of zero shall be treated as
false and any other value shall be treated as true. Otherwise, a string value of the null string shall be
treated as false and any other value shall be treated as true. A Boolean context shall be one of the follow‐
ing:
* The first subexpression of a conditional expression
* An expression operated on by logical NOT, logical AND, or logical OR
* The second expression of a for statement
* The expression of an if statement
* The expression of the while clause in either a while or do...while statement
* An expression used as a pattern (as in Overall Program Structure)
All arithmetic shall follow the semantics of floating-point arithmetic as specified by the ISO C standard (see
Section 1.1.2, Concepts Derived from the ISO C Standard).
The value of the expression:
expr1 ^ expr2
shall be equivalent to the value returned by the ISO C standard function call:
pow(expr1, expr2)
The expression:
lvalue ^= expr
shall be equivalent to the ISO C standard expression:
lvalue = pow(lvalue, expr)
except that lvalue shall be evaluated only once. The value of the expression:
expr1 % expr2
shall be equivalent to the value returned by the ISO C standard function call:
fmod(expr1, expr2)
The expression:
lvalue %= expr
shall be equivalent to the ISO C standard expression:
lvalue = fmod(lvalue, expr)
except that lvalue shall be evaluated only once.
Variables and fields shall be set by the assignment statement:
lvalue = expression
and the type of expression shall determine the resulting variable type. The assignment includes the arithmetic
assignments ("+=", "−=", "*=", "/=", "%=", "^=", "++", "−−") all of which shall produce a numeric result. The
left-hand side of an assignment and the target of increment and decrement operators can be one of a variable,
an array with index, or a field selector.
The awk language supplies arrays that are used for storing numbers or strings. Arrays need not be declared.
They shall initially be empty, and their sizes shall change dynamically. The subscripts, or element identi‐
fiers, are strings, providing a type of associative array capability. An array name followed by a subscript
within square brackets can be used as an lvalue and thus as an expression, as described in the grammar; see
Grammar. Unsubscripted array names can be used in only the following contexts:
* A parameter in a function definition or function call
* The NAME token following any use of the keyword in as specified in the grammar (see Grammar); if the name
used in this context is not an array name, the behavior is undefined
A valid array index shall consist of one or more <comma>-separated expressions, similar to the way in which
multi-dimensional arrays are indexed in some programming languages. Because awk arrays are really one-dimen‐
sional, such a <comma>-separated list shall be converted to a single string by concatenating the string values
of the separate expressions, each separated from the other by the value of the SUBSEP variable. Thus, the fol‐
lowing two index operations shall be equivalent:
var[expr1, expr2, ... exprn]
var[expr1 SUBSEP expr2 SUBSEP ... SUBSEP exprn]
The application shall ensure that a multi-dimensioned index used with the in operator is parenthesized. The in
operator, which tests for the existence of a particular array element, shall not cause that element to exist.
Any other reference to a nonexistent array element shall automatically create it.
Comparisons (with the '<', "<=", "!=", "==", '>', and ">=" operators) shall be made numerically if both oper‐
ands are numeric, if one is numeric and the other has a string value that is a numeric string, or if one is
numeric and the other has the uninitialized value. Otherwise, operands shall be converted to strings as
required and a string comparison shall be made using the locale-specific collation sequence. The value of the
comparison expression shall be 1 if the relation is true, or 0 if the relation is false.
Variables and Special Variables
Variables can be used in an awk program by referencing them. With the exception of function parameters (see
User-Defined Functions), they are not explicitly declared. Function parameter names shall be local to the func‐
tion; all other variable names shall be global. The same name shall not be used as both a function parameter
name and as the name of a function or a special awk variable. The same name shall not be used both as a vari‐
able name with global scope and as the name of a function. The same name shall not be used within the same
scope both as a scalar variable and as an array. Uninitialized variables, including scalar variables, array
elements, and field variables, shall have an uninitialized value. An uninitialized value shall have both a
numeric value of zero and a string value of the empty string. Evaluation of variables with an uninitialized
value, to either string or numeric, shall be determined by the context in which they are used.
Field variables shall be designated by a '$' followed by a number or numerical expression. The effect of the
field number expression evaluating to anything other than a non-negative integer is unspecified; uninitialized
variables or string values need not be converted to numeric values in this context. New field variables can be
created by assigning a value to them. References to nonexistent fields (that is, fields after $NF), shall eval‐
uate to the uninitialized value. Such references shall not create new fields. However, assigning to a nonexis‐
tent field (for example, $(NF+2)=5) shall increase the value of NF; create any intervening fields with the
uninitialized value; and cause the value of $0 to be recomputed, with the fields being separated by the value
of OFS. Each field variable shall have a string value or an uninitialized value when created. Field variables
shall have the uninitialized value when created from $0 using FS and the variable does not contain any charac‐
ters. If appropriate, the field variable shall be considered a numeric string (see Expressions in awk).
Implementations shall support the following other special variables that are set by awk:
ARGC The number of elements in the ARGV array.
ARGV An array of command line arguments, excluding options and the program argument, numbered from zero to
ARGC−1.
The arguments in ARGV can be modified or added to; ARGC can be altered. As each input file ends, awk
shall treat the next non-null element of ARGV, up to the current value of ARGC−1, inclusive, as the
name of the next input file. Thus, setting an element of ARGV to null means that it shall not be
treated as an input file. The name '−' indicates the standard input. If an argument matches the for‐
mat of an assignment operand, this argument shall be treated as an assignment rather than a file
argument.
CONVFMT The printf format for converting numbers to strings (except for output statements, where OFMT is
used); "%.6g" by default.
ENVIRON An array representing the value of the environment, as described in the exec functions defined in the
System Interfaces volume of POSIX.1‐2008. The indices of the array shall be strings consisting of the
names of the environment variables, and the value of each array element shall be a string consisting
of the value of that variable. If appropriate, the environment variable shall be considered a numeric
string (see Expressions in awk); the array element shall also have its numeric value.
In all cases where the behavior of awk is affected by environment variables (including the environ‐
ment of any commands that awk executes via the system function or via pipeline redirections with the
print statement, the printf statement, or the getline function), the environment used shall be the
environment at the time awk began executing; it is implementation-defined whether any modification of
ENVIRON affects this environment.
FILENAME A pathname of the current input file. Inside a BEGIN action the value is undefined. Inside an END
action the value shall be the name of the last input file processed.
FNR The ordinal number of the current record in the current file. Inside a BEGIN action the value shall
be zero. Inside an END action the value shall be the number of the last record processed in the last
file processed.
FS Input field separator regular expression; a <space> by default.
NF The number of fields in the current record. Inside a BEGIN action, the use of NF is undefined unless
a getline function without a var argument is executed previously. Inside an END action, NF shall
retain the value it had for the last record read, unless a subsequent, redirected, getline function
without a var argument is performed prior to entering the END action.
NR The ordinal number of the current record from the start of input. Inside a BEGIN action the value
shall be zero. Inside an END action the value shall be the number of the last record processed.
OFMT The printf format for converting numbers to strings in output statements (see Output Statements);
"%.6g" by default. The result of the conversion is unspecified if the value of OFMT is not a float‐
ing-point format specification.
OFS The print statement output field separator; <space> by default.
ORS The print statement output record separator; a <newline> by default.
RLENGTH The length of the string matched by the match function.
RS The first character of the string value of RS shall be the input record separator; a <newline> by
default. If RS contains more than one character, the results are unspecified. If RS is null, then
records are separated by sequences consisting of a <newline> plus one or more blank lines, leading or
trailing blank lines shall not result in empty records at the beginning or end of the input, and a
<newline> shall always be a field separator, no matter what the value of FS is.
RSTART The starting position of the string matched by the match function, numbering from 1. This shall
always be equivalent to the return value of the match function.
SUBSEP The subscript separator string for multi-dimensional arrays; the default value is implementation-
defined.
Regular Expressions
The awk utility shall make use of the extended regular expression notation (see the Base Definitions volume of
POSIX.1‐2008, Section 9.4, Extended Regular Expressions) except that it shall allow the use of C-language con‐
ventions for escaping special characters within the EREs, as specified in the table in the Base Definitions
volume of POSIX.1‐2008, Chapter 5, File Format Notation ('\\', '\a', '\b', '\f', '\n', '\r', '\t', '\v') and
the following table; these escape sequences shall be recognized both inside and outside bracket expressions.
Note that records need not be separated by <newline> characters and string constants can contain <newline>
characters, so even the "\n" sequence is valid in awk EREs. Using a <slash> character within an ERE requires
the escaping shown in the following table.
Table 4-2: Escape Sequences in awk
┌─────────┬────────────────────────────────────┬────────────────────────────────────┐
│ Escape │ │ │
│Sequence │ Description │ Meaning │
├─────────┼────────────────────────────────────┼────────────────────────────────────┤
│\" │ <backslash> <quotation-mark> │ <quotation-mark> character │
├─────────┼────────────────────────────────────┼────────────────────────────────────┤
│\/ │ <backslash> <slash> │ <slash> character │
├─────────┼────────────────────────────────────┼────────────────────────────────────┤
│\ddd │ A <backslash> character followed │ The character whose encoding is │
│ │ by the longest sequence of one, │ represented by the one, two, or │
│ │ two, or three octal-digit charac‐ │ three-digit octal integer. Multi- │
│ │ ters (01234567). If all of the │ byte characters require multiple, │
│ │ digits are 0 (that is, representa‐ │ concatenated escape sequences of │
│ │ tion of the NUL character), the │ this type, including the leading │
│ │ behavior is undefined. │ <backslash> for each byte. │
├─────────┼────────────────────────────────────┼────────────────────────────────────┤
│\c │ A <backslash> character followed │ Undefined │
│ │ by any character not described in │ │
│ │ this table or in the table in the │ │
│ │ Base Definitions volume of │ │
│ │ POSIX.1‐2008, Chapter 5, File For‐ │ │
│ │ mat Notation ('\\', '\a', '\b', │ │
│ │ '\f', '\n', '\r', '\t', '\v'). │ │
└─────────┴────────────────────────────────────┴────────────────────────────────────┘
A regular expression can be matched against a specific field or string by using one of the two regular expres‐
sion matching operators, '~' and "!~". These operators shall interpret their right-hand operand as a regular
expression and their left-hand operand as a string. If the regular expression matches the string, the '~'
expression shall evaluate to a value of 1, and the "!~" expression shall evaluate to a value of 0. (The regular
expression matching operation is as defined by the term matched in the Base Definitions volume of POSIX.1‐2008,
Section 9.1, Regular Expression Definitions, where a match occurs on any part of the string unless the regular
expression is limited with the <circumflex> or <dollar-sign> special characters.) If the regular expression
does not match the string, the '~' expression shall evaluate to a value of 0, and the "!~" expression shall
evaluate to a value of 1. If the right-hand operand is any expression other than the lexical token ERE, the
string value of the expression shall be interpreted as an extended regular expression, including the escape
conventions described above. Note that these same escape conventions shall also be applied in determining the
value of a string literal (the lexical token STRING), and thus shall be applied a second time when a string
literal is used in this context.
When an ERE token appears as an expression in any context other than as the right-hand of the '~' or "!~" oper‐
ator or as one of the built-in function arguments described below, the value of the resulting expression shall
be the equivalent of:
$0 " " /ere/
The ere argument to the gsub, match, sub functions, and the fs argument to the split function (see String Func‐
tions) shall be interpreted as extended regular expressions. These can be either ERE tokens or arbitrary
expressions, and shall be interpreted in the same manner as the right-hand side of the '~' or "!~" operator.
An extended regular expression can be used to separate fields by assigning a string containing the expression
to the built-in variable FS, either directly or as a consequence of using the −F sepstring option. The default
value of the FS variable shall be a single <space>. The following describes FS behavior:
1. If FS is a null string, the behavior is unspecified.
2. If FS is a single character:
a. If FS is <space>, skip leading and trailing <blank> and <newline> characters; fields shall be delimited
by sets of one or more <blank> or <newline> characters.
b. Otherwise, if FS is any other character c, fields shall be delimited by each single occurrence of c.
3. Otherwise, the string value of FS shall be considered to be an extended regular expression. Each occurrence
of a sequence matching the extended regular expression shall delimit fields.
Except for the '~' and "!~" operators, and in the gsub, match, split, and sub built-in functions, ERE matching
shall be based on input records; that is, record separator characters (the first character of the value of the
variable RS, <newline> by default) cannot be embedded in the expression, and no expression shall match the
record separator character. If the record separator is not <newline>, <newline> characters embedded in the
expression can be matched. For the '~' and "!~" operators, and in those four built-in functions, ERE matching
shall be based on text strings; that is, any character (including <newline> and the record separator) can be
embedded in the pattern, and an appropriate pattern shall match any character. However, in all awk ERE match‐
ing, the use of one or more NUL characters in the pattern, input record, or text string produces undefined
results.
Patterns
A pattern is any valid expression, a range specified by two expressions separated by a comma, or one of the two
special patterns BEGIN or END.
Special Patterns
The awk utility shall recognize two special patterns, BEGIN and END. Each BEGIN pattern shall be matched once
and its associated action executed before the first record of input is read—except possibly by use of the get‐
line function (see Input/Output and General Functions) in a prior BEGIN action—and before command line assign‐
ment is done. Each END pattern shall be matched once and its associated action executed after the last record
of input has been read. These two patterns shall have associated actions.
BEGIN and END shall not combine with other patterns. Multiple BEGIN and END patterns shall be allowed. The
actions associated with the BEGIN patterns shall be executed in the order specified in the program, as are the
END actions. An END pattern can precede a BEGIN pattern in a program.
If an awk program consists of only actions with the pattern BEGIN, and the BEGIN action contains no getline
function, awk shall exit without reading its input when the last statement in the last BEGIN action is exe‐
cuted. If an awk program consists of only actions with the pattern END or only actions with the patterns BEGIN
and END, the input shall be read before the statements in the END actions are executed.
Expression Patterns
An expression pattern shall be evaluated as if it were an expression in a Boolean context. If the result is
true, the pattern shall be considered to match, and the associated action (if any) shall be executed. If the
result is false, the action shall not be executed.
Pattern Ranges
A pattern range consists of two expressions separated by a comma; in this case, the action shall be performed
for all records between a match of the first expression and the following match of the second expression,
inclusive. At this point, the pattern range can be repeated starting at input records subsequent to the end of
the matched range.
Actions
An action is a sequence of statements as shown in the grammar in Grammar. Any single statement can be replaced
by a statement list enclosed in curly braces. The application shall ensure that statements in a statement list
are separated by <newline> or <semicolon> characters. Statements in a statement list shall be executed sequen‐
tially in the order that they appear.
The expression acting as the conditional in an if statement shall be evaluated and if it is non-zero or non-
null, the following statement shall be executed; otherwise, if else is present, the statement following the
else shall be executed.
The if, while, do...while, for, break, and continue statements are based on the ISO C standard (see Section
1.1.2, Concepts Derived from the ISO C Standard), except that the Boolean expressions shall be treated as
described in Expressions in awk, and except in the case of:
for (variable in array)
which shall iterate, assigning each index of array to variable in an unspecified order. The results of adding
new elements to array within such a for loop are undefined. If a break or continue statement occurs outside of
a loop, the behavior is undefined.
The delete statement shall remove an individual array element. Thus, the following code deletes an entire
array:
for (index in array)
delete array[index]
The next statement shall cause all further processing of the current input record to be abandoned. The behavior
is undefined if a next statement appears or is invoked in a BEGIN or END action.
The exit statement shall invoke all END actions in the order in which they occur in the program source and then
terminate the program without reading further input. An exit statement inside an END action shall terminate the
program without further execution of END actions. If an expression is specified in an exit statement, its
numeric value shall be the exit status of awk, unless subsequent errors are encountered or a subsequent exit
statement with an expression is executed.
Output Statements
Both print and printf statements shall write to standard output by default. The output shall be written to the
location specified by output_redirection if one is supplied, as follows:
> expression
>> expression
| expression
In all cases, the expression shall be evaluated to produce a string that is used as a pathname into which to
write (for '>' or ">>") or as a command to be executed (for '|'). Using the first two forms, if the file of
that name is not currently open, it shall be opened, creating it if necessary and using the first form, trun‐
cating the file. The output then shall be appended to the file. As long as the file remains open, subsequent
calls in which expression evaluates to the same string value shall simply append output to the file. The file
remains open until the close function (see Input/Output and General Functions) is called with an expression
that evaluates to the same string value.
The third form shall write output onto a stream piped to the input of a command. The stream shall be created if
no stream is currently open with the value of expression as its command name. The stream created shall be
equivalent to one created by a call to the popen() function defined in the System Interfaces volume of
POSIX.1‐2008 with the value of expression as the command argument and a value of w as the mode argument. As
long as the stream remains open, subsequent calls in which expression evaluates to the same string value shall
write output to the existing stream. The stream shall remain open until the close function (see Input/Output
and General Functions) is called with an expression that evaluates to the same string value. At that time, the
stream shall be closed as if by a call to the pclose() function defined in the System Interfaces volume of
POSIX.1‐2008.
As described in detail by the grammar in Grammar, these output statements shall take a <comma>-separated list
of expressions referred to in the grammar by the non-terminal symbols expr_list, print_expr_list, or
print_expr_list_opt. This list is referred to here as the expression list, and each member is referred to as
an expression argument.
The print statement shall write the value of each expression argument onto the indicated output stream sepa‐
rated by the current output field separator (see variable OFS above), and terminated by the output record sepa‐
rator (see variable ORS above). All expression arguments shall be taken as strings, being converted if neces‐
sary; this conversion shall be as described in Expressions in awk, with the exception that the printf format in
OFMT shall be used instead of the value in CONVFMT. An empty expression list shall stand for the whole input
record ($0).
The printf statement shall produce output based on a notation similar to the File Format Notation used to
describe file formats in this volume of POSIX.1‐2008 (see the Base Definitions volume of POSIX.1‐2008, Chapter
5, File Format Notation). Output shall be produced as specified with the first expression argument as the
string format and subsequent expression arguments as the strings arg1 to argn, inclusive, with the following
exceptions:
1. The format shall be an actual character string rather than a graphical representation. Therefore, it cannot
contain empty character positions. The <space> in the format string, in any context other than a flag of a
conversion specification, shall be treated as an ordinary character that is copied to the output.
2. If the character set contains a '' character and that character appears in the format string, it shall be
treated as an ordinary character that is copied to the output.
3. The escape sequences beginning with a <backslash> character shall be treated as sequences of ordinary char‐
acters that are copied to the output. Note that these same sequences shall be interpreted lexically by awk
when they appear in literal strings, but they shall not be treated specially by the printf statement.
4. A field width or precision can be specified as the '*' character instead of a digit string. In this case
the next argument from the expression list shall be fetched and its numeric value taken as the field width
or precision.
5. The implementation shall not precede or follow output from the d or u conversion specifier characters with
<blank> characters not specified by the format string.
6. The implementation shall not precede output from the o conversion specifier character with leading zeros
not specified by the format string.
7. For the c conversion specifier character: if the argument has a numeric value, the character whose encoding
is that value shall be output. If the value is zero or is not the encoding of any character in the charac‐
ter set, the behavior is undefined. If the argument does not have a numeric value, the first character of
the string value shall be output; if the string does not contain any characters, the behavior is undefined.
8. For each conversion specification that consumes an argument, the next expression argument shall be evalu‐
ated. With the exception of the c conversion specifier character, the value shall be converted (according
to the rules specified in Expressions in awk) to the appropriate type for the conversion specification.
9. If there are insufficient expression arguments to satisfy all the conversion specifications in the format
string, the behavior is undefined.
10. If any character sequence in the format string begins with a '%' character, but does not form a valid con‐
version specification, the behavior is unspecified.
Both print and printf can output at least {LINE_MAX} bytes.
Functions
The awk language has a variety of built-in functions: arithmetic, string, input/output, and general.
Arithmetic Functions
The arithmetic functions, except for int, shall be based on the ISO C standard (see Section 1.1.2, Concepts
Derived from the ISO C Standard). The behavior is undefined in cases where the ISO C standard specifies that
an error be returned or that the behavior is undefined. Although the grammar (see Grammar) permits built-in
functions to appear with no arguments or parentheses, unless the argument or parentheses are indicated as
optional in the following list (by displaying them within the "[]" brackets), such use is undefined.
atan2(y,x)
Return arctangent of y/x in radians in the range [−π,π].
cos(x) Return cosine of x, where x is in radians.
sin(x) Return sine of x, where x is in radians.
exp(x) Return the exponential function of x.
log(x) Return the natural logarithm of x.
sqrt(x) Return the square root of x.
int(x) Return the argument truncated to an integer. Truncation shall be toward 0 when x>0.
rand() Return a random number n, such that 0≤n<1.
srand([expr])
Set the seed value for rand to expr or use the time of day if expr is omitted. The previous seed
value shall be returned.
String Functions
The string functions in the following list shall be supported. Although the grammar (see Grammar) permits
built-in functions to appear with no arguments or parentheses, unless the argument or parentheses are indicated
as optional in the following list (by displaying them within the "[]" brackets), such use is undefined.
gsub(ere, repl[, in])
Behave like sub (see below), except that it shall replace all occurrences of the regular expression
(like the ed utility global substitute) in $0 or in the in argument, when specified.
index(s, t)
Return the position, in characters, numbering from 1, in string s where string t first occurs, or
zero if it does not occur at all.
length[([s])]
Return the length, in characters, of its argument taken as a string, or of the whole record, $0, if
there is no argument.
match(s, ere)
Return the position, in characters, numbering from 1, in string s where the extended regular expres‐
sion ere occurs, or zero if it does not occur at all. RSTART shall be set to the starting position
(which is the same as the returned value), zero if no match is found; RLENGTH shall be set to the
length of the matched string, −1 if no match is found.
split(s, a[, fs ])
Split the string s into array elements a[1], a[2], ..., a[n], and return n. All elements of the
array shall be deleted before the split is performed. The separation shall be done with the ERE fs or
with the field separator FS if fs is not given. Each array element shall have a string value when
created and, if appropriate, the array element shall be considered a numeric string (see Expressions
in awk). The effect of a null string as the value of fs is unspecified.
sprintf(fmt, expr, expr, ...)
Format the expressions according to the printf format given by fmt and return the resulting string.
sub(ere, repl[, in ])
Substitute the string repl in place of the first instance of the extended regular expression ERE in
string in and return the number of substitutions. An <ampersand> ('&') appearing in the string repl
shall be replaced by the string from in that matches the ERE. An <ampersand> preceded with a <back‐
slash> shall be interpreted as the literal <ampersand> character. An occurrence of two consecutive
<backslash> characters shall be interpreted as just a single literal <backslash> character. Any other
occurrence of a <backslash> (for example, preceding any other character) shall be treated as a lit‐
eral <backslash> character. Note that if repl is a string literal (the lexical token STRING; see
Grammar), the handling of the <ampersand> character occurs after any lexical processing, including
any lexical <backslash>-escape sequence processing. If in is specified and it is not an lvalue (see
Expressions in awk), the behavior is undefined. If in is omitted, awk shall use the current record
($0) in its place.
substr(s, m[, n ])
Return the at most n-character substring of s that begins at position m, numbering from 1. If n is
omitted, or if n specifies more characters than are left in the string, the length of the substring
shall be limited by the length of the string s.
tolower(s)
Return a string based on the string s. Each character in s that is an uppercase letter specified to
have a tolower mapping by the LC_CTYPE category of the current locale shall be replaced in the
returned string by the lowercase letter specified by the mapping. Other characters in s shall be
unchanged in the returned string.
toupper(s)
Return a string based on the string s. Each character in s that is a lowercase letter specified to
have a toupper mapping by the LC_CTYPE category of the current locale is replaced in the returned
string by the uppercase letter specified by the mapping. Other characters in s are unchanged in the
returned string.
All of the preceding functions that take ERE as a parameter expect a pattern or a string valued expression that
is a regular expression as defined in Regular Expressions.
Input/Output and General Functions
The input/output and general functions are:
close(expression)
Close the file or pipe opened by a print or printf statement or a call to getline with the same
string-valued expression. The limit on the number of open expression arguments is implementation-
defined. If the close was successful, the function shall return zero; otherwise, it shall return non-
zero.
expression | getline [var]
Read a record of input from a stream piped from the output of a command. The stream shall be created
if no stream is currently open with the value of expression as its command name. The stream created
shall be equivalent to one created by a call to the popen() function with the value of expression as
the command argument and a value of r as the mode argument. As long as the stream remains open, sub‐
sequent calls in which expression evaluates to the same string value shall read subsequent records
from the stream. The stream shall remain open until the close function is called with an expression
that evaluates to the same string value. At that time, the stream shall be closed as if by a call to
the pclose() function. If var is omitted, $0 and NF shall be set; otherwise, var shall be set and, if
appropriate, it shall be considered a numeric string (see Expressions in awk).
The getline operator can form ambiguous constructs when there are unparenthesized operators (includ‐
ing concatenate) to the left of the '|' (to the beginning of the expression containing getline). In
the context of the '$' operator, '|' shall behave as if it had a lower precedence than '$'. The
result of evaluating other operators is unspecified, and conforming applications shall parenthesize
properly all such usages.
getline Set $0 to the next input record from the current input file. This form of getline shall set the NF,
NR, and FNR variables.
getline var
Set variable var to the next input record from the current input file and, if appropriate, var shall
be considered a numeric string (see Expressions in awk). This form of getline shall set the FNR and
NR variables.
getline [var] < expression
Read the next record of input from a named file. The expression shall be evaluated to produce a
string that is used as a pathname. If the file of that name is not currently open, it shall be
opened. As long as the stream remains open, subsequent calls in which expression evaluates to the
same string value shall read subsequent records from the file. The file shall remain open until the
close function is called with an expression that evaluates to the same string value. If var is omit‐
ted, $0 and NF shall be set; otherwise, var shall be set and, if appropriate, it shall be considered
a numeric string (see Expressions in awk).
The getline operator can form ambiguous constructs when there are unparenthesized binary operators
(including concatenate) to the right of the '<' (up to the end of the expression containing the get‐
line). The result of evaluating such a construct is unspecified, and conforming applications shall
parenthesize properly all such usages.
system(expression)
Execute the command given by expression in a manner equivalent to the system() function defined in
the System Interfaces volume of POSIX.1‐2008 and return the exit status of the command.
All forms of getline shall return 1 for successful input, zero for end-of-file, and −1 for an error.
Where strings are used as the name of a file or pipeline, the application shall ensure that the strings are
textually identical. The terminology ``same string value'' implies that ``equivalent strings'', even those that
differ only by <space> characters, represent different files.
User-Defined Functions
The awk language also provides user-defined functions. Such functions can be defined as:
function name([parameter, ...]) { statements }
A function can be referred to anywhere in an awk program; in particular, its use can precede its definition.
The scope of a function is global.
Function parameters, if present, can be either scalars or arrays; the behavior is undefined if an array name is
passed as a parameter that the function uses as a scalar, or if a scalar expression is passed as a parameter
that the function uses as an array. Function parameters shall be passed by value if scalar and by reference if
array name.
The number of parameters in the function definition need not match the number of parameters in the function
call. Excess formal parameters can be used as local variables. If fewer arguments are supplied in a function
call than are in the function definition, the extra parameters that are used in the function body as scalars
shall evaluate to the uninitialized value until they are otherwise initialized, and the extra parameters that
are used in the function body as arrays shall be treated as uninitialized arrays where each element evaluates
to the uninitialized value until otherwise initialized.
When invoking a function, no white space can be placed between the function name and the opening parenthesis.
Function calls can be nested and recursive calls can be made upon functions. Upon return from any nested or
recursive function call, the values of all of the calling function's parameters shall be unchanged, except for
array parameters passed by reference. The return statement can be used to return a value. If a return statement
appears outside of a function definition, the behavior is undefined.
In the function definition, <newline> characters shall be optional before the opening brace and after the clos‐
ing brace. Function definitions can appear anywhere in the program where a pattern-action pair is allowed.
Grammar
The grammar in this section and the lexical conventions in the following section shall together describe the
syntax for awk programs. The general conventions for this style of grammar are described in Section 1.3, Gram‐
mar Conventions. A valid program can be represented as the non-terminal symbol program in the grammar. This
formal syntax shall take precedence over the preceding text syntax description.
%token NAME NUMBER STRING ERE
%token FUNC_NAME /* Name followed by '(' without white space. */
/* Keywords */
%token Begin End
/* 'BEGIN' 'END' */
%token Break Continue Delete Do Else
/* 'break' 'continue' 'delete' 'do' 'else' */
%token Exit For Function If In
/* 'exit' 'for' 'function' 'if' 'in' */
%token Next Print Printf Return While
/* 'next' 'print' 'printf' 'return' 'while' */
/* Reserved function names */
%token BUILTIN_FUNC_NAME
/* One token for the following:
* atan2 cos sin exp log sqrt int rand srand
* gsub index length match split sprintf sub
* substr tolower toupper close system
*/
%token GETLINE
/* Syntactically different from other built-ins. */
/* Two-character tokens. */
%token ADD_ASSIGN SUB_ASSIGN MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN POW_ASSIGN
/* '+=' '−=' '*=' '/=' '%=' '^=' */
%token OR AND NO_MATCH EQ LE GE NE INCR DECR APPEND
/* '||' '&&' '!~' '==' '<=' '>=' '!=' '++' '−−' '>>' */
/* One-character tokens. */
%token '{' '}' '(' ')' '[' ']' ',' ';' NEWLINE
%token '+' '−' '*' '%' '^' '!' '>' '<' '|' '?' ':' ' " " ' '$' '='
%start program
%%
program : item_list
| actionless_item_list
;
item_list : newline_opt
| actionless_item_list item terminator
| item_list item terminator
| item_list action terminator
;
actionless_item_list : item_list pattern terminator
| actionless_item_list pattern terminator
;
item : pattern action
| Function NAME '(' param_list_opt ')'
newline_opt action
| Function FUNC_NAME '(' param_list_opt ')'
newline_opt action
;
param_list_opt : /* empty */
| param_list
;
param_list : NAME
| param_list ',' NAME