summaryrefslogtreecommitdiff
path: root/usr/src/man/man1/awk.1
blob: 2b86e7e8b07911f898d35dee3f3f5ae1e6a143df (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Copyright 1992, X/Open Company Limited  All Rights Reserved
.\" Portions Copyright (c) 2005, 2006 Sun Microsystems, Inc. All Rights Reserved
.\" Copyright 2020 Joyent, Inc.
.\"
.TH AWK 1 "June 13, 2021"
.SH NAME
awk \- pattern scanning and processing language
.SH SYNOPSIS
.nf
\fB/usr/bin/awk\fR [\fB-F\fR \fIERE\fR] [\fB-v\fR \fIassignment\fR] \fI\&'program'\fR | \fB-f\fR \fIprogfile\fR...
     [\fIargument\fR]...
.fi

.LP
.nf
\fB/usr/bin/nawk\fR [\fB-F\fR \fIERE\fR] [\fB-v\fR \fIassignment\fR] \fI\&'program'\fR | \fB-f\fR \fIprogfile\fR...
     [\fIargument\fR]...
.fi

.LP
.nf
\fB/usr/xpg4/bin/awk\fR [\fB-F\fR \fIERE\fR] [\fB-v\fR \fIassignment\fR]... \fI\&'program'\fR | \fB-f\fR \fIprogfile\fR...
     [\fIargument\fR]...
.fi

.SH DESCRIPTION
NOTE: The \fBnawk\fR command is now the system default awk for illumos.
.LP
The \fB/usr/bin/awk\fR and \fB/usr/xpg4/bin/awk\fR utilities execute
\fIprogram\fRs written in the \fBawk\fR programming language, which is
specialized for textual data manipulation. A \fBawk\fR \fIprogram\fR is a
sequence of patterns and corresponding actions. The string specifying
\fIprogram\fR must be enclosed in single quotes (') to protect it from
interpretation by the shell. The sequence of pattern - action statements can be
specified in the command line as \fIprogram\fR or in one, or more, file(s)
specified by the \fB-f\fR\fIprogfile\fR option. When input is read that matches
a pattern, the action associated with the pattern is performed.
.sp
.LP
Input is interpreted as a sequence of records. By default, a record is a line,
but this can be changed by using the \fBRS\fR built-in variable. Each record of
input is matched to each pattern in the \fIprogram\fR. For each pattern
matched, the associated action is executed.
.sp
.LP
The \fBawk\fR utility interprets each input record as a sequence of fields
where, by default, a field is a string of non-blank characters. This default
white-space field delimiter (blanks and/or tabs) can be changed by using the
\fBFS\fR built-in variable or the \fB-F\fR\fIERE\fR option. The \fBawk\fR
utility denotes the first field in a record \fB$1\fR, the second \fB$2\fR, and
so forth. The symbol \fB$0\fR refers to the entire record; setting any other
field causes the reevaluation of \fB$0\fR. Assigning to \fB$0\fR resets the
values of all fields and the \fBNF\fR built-in variable.

.SH OPTIONS
The following options are supported:
.sp
.ne 2
.na
\fB\fB-F\fR \fIERE\fR\fR
.ad
.RS 17n
Define the input field separator to be the extended regular expression
\fIERE\fR, before any input is read (can be a character).
.RE

.sp
.ne 2
.na
\fB\fB-f\fR \fIprogfile\fR\fR
.ad
.RS 17n
Specifies the pathname of the file \fIprogfile\fR containing a \fBawk\fR
program. If multiple instances of this option are specified, the concatenation
of the files specified as \fIprogfile\fR in the order specified is the
\fBawk\fR program. The \fBawk\fR program can alternatively be specified in
the command line as a single argument.
.RE

.sp
.ne 2
.na
\fB\fB-v\fR \fIassignment\fR\fR
.ad
.RS 17n
The \fIassignment\fR argument must be in the same form as an \fIassignment\fR
operand. The assignment is of the form \fIvar=value\fR, where \fIvar\fR is the
name of one of the variables described below. The specified assignment occurs
before executing the \fBawk\fR program, including the actions associated with
\fBBEGIN\fR patterns (if any). Multiple occurrences of this option can be
specified.
.RE

.sp
.ne 2
.na
\fB\fB-safe\fR\fR
.ad
.RS 17n
When passed to \fBawk\fR, this flag will prevent the program from opening new
files or running child processes. The \fBENVIRON\fR array will also not be
initialized.
.RE

.SH OPERANDS
The following operands are supported:
.sp
.ne 2
.na
\fB\fIprogram\fR\fR
.ad
.RS 12n
If no \fB-f\fR option is specified, the first operand to \fBawk\fR is the text
of the \fBawk\fR program. The application supplies the \fIprogram\fR operand
as a single argument to \fBawk.\fR If the text does not end in a newline
character, \fBawk\fR interprets the text as if it did.
.RE

.sp
.ne 2
.na
\fB\fIargument\fR\fR
.ad
.RS 12n
Either of the following two types of \fIargument\fR can be intermixed:
.sp
.ne 2
.na
\fB\fIfile\fR\fR
.ad
.RS 14n
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 \fIfile\fR operands are
specified, or if a \fIfile\fR operand is \fB\(mi\fR, the standard input is
used.
.RE

.sp
.ne 2
.na
\fB\fIassignment\fR\fR
.ad
.RS 14n
An operand that begins with an underscore or alphabetic character from the
portable character set, followed by a sequence of underscores, digits and
alphabetics from the portable character set, followed by the \fB=\fR character
specifies a variable assignment rather than a pathname. The characters before
the \fB=\fR represent the name of a \fBawk\fR variable. If that name is a
\fBawk\fR reserved word, the behavior is undefined. The characters following
the equal sign is interpreted as if they appeared in the \fBawk\fR program
preceded and followed by a double-quote (\fB"\fR) character, as a \fBSTRING\fR
token, except that if the last character is an unescaped backslash, it is
interpreted as a literal backslash rather than as the first character of the
sequence \fB\e\fR\&.. The variable is assigned the value of that \fBSTRING\fR
token. If the value is considered a \fInumeric\fR string, the variable is
assigned its numeric value. Each such variable assignment is performed just
before the processing of the following \fIfile\fR, if any. Thus, an assignment
before the first \fBfile\fR argument is executed after the \fBBEGIN\fR actions
(if any), while an assignment after the last \fIfile\fR argument is executed
before the \fBEND\fR actions (if any).  If there are no \fIfile\fR arguments,
assignments are executed before processing the standard input.
.RE

.RE

.SH INPUT FILES
Input files to the \fBawk\fR program from any of the following sources:
.RS +4
.TP
.ie t \(bu
.el o
any \fIfile\fR operands or their equivalents, achieved by modifying the
\fBawk\fR variables \fBARGV\fR and \fBARGC\fR
.RE
.RS +4
.TP
.ie t \(bu
.el o
standard input in the absence of any \fIfile\fR operands
.RE
.RS +4
.TP
.ie t \(bu
.el o
arguments to the \fBgetline\fR function
.RE
.sp
.LP
must be text files. Whether the variable \fBRS\fR is set to a value other than
a newline character or not, for these files, implementations support records
terminated with the specified separator up to \fB{LINE_MAX}\fR bytes and can
support longer records.
.sp
.LP
If \fB-\fR\fBf\fR \fIprogfile\fR is specified, the files named by each of the
\fIprogfile\fR option-arguments must be text files containing an \fBawk\fR
program.
.sp
.LP
The standard input are used only if no \fIfile\fR operands are specified, or if
a \fIfile\fR operand is \fB\(mi\fR\&.

.SH EXTENDED DESCRIPTION
A \fBawk\fR program is composed of pairs of the form:
.sp
.in +2
.nf
pattern { \fIaction\fR }
.fi
.in -2

.sp
.LP
Either the pattern or the action (including the enclosing brace characters) can
be omitted. Pattern-action statements are separated by a semicolon or by a
newline.
.sp
.LP
A missing pattern matches any record of input, and a missing action is
equivalent to an action that writes the matched record of input to standard
output.
.sp
.LP
Execution of the \fBawk\fR program starts by first executing the actions
associated with all \fBBEGIN\fR patterns in the order they occur in the
program. Then each \fIfile\fR operand (or standard input if no files were
specified) is processed by reading data from the file until a record separator
is seen (a newline character by default), splitting the current record into
fields using the current value of \fBFS\fR, evaluating each pattern in the
program in the order of occurrence, and executing the action associated with
each pattern that matches the current record. The action for a matching pattern
is executed before evaluating subsequent patterns. Last, the actions associated
with all \fBEND\fR patterns is executed in the order they occur in the program.

.SS "Expressions in awk"
Expressions describe computations used in \fIpatterns\fR and \fIactions\fR. In
the following table, valid expression operations are given in groups from
highest precedence first to lowest precedence last, with equal-precedence
operators grouped between horizontal lines. In expression evaluation, where the
grammar is formally ambiguous, higher precedence operators are evaluated before
lower precedence operators.  In this table \fIexpr,\fR \fIexpr1,\fR
\fIexpr2,\fR and \fIexpr3\fR represent any expression, while \fIlvalue\fR
represents any entity that can be assigned to (that is, on the left side of an
assignment operator).
.sp

.sp
.TS
c c c c
l l l l .
\fBSyntax\fR	\fBName\fR	\fBType of Result\fR	\fBAssociativity\fR
_
( \fIexpr\fR )	Grouping	type of \fIexpr\fR	n/a
_
$\fIexpr\fR	Field reference	string	n/a
_
++ \fIlvalue\fR	Pre-increment	numeric	n/a
\(mi\(mi \fIlvalue\fR	Pre-decrement	numeric	n/a
\fIlvalue\fR ++	Post-increment	numeric	n/a
\fIlvalue\fR \(mi\(mi	Post-decrement	numeric	n/a
_
\fIexpr\fR ^ \fIexpr\fR	Exponentiation	numeric	right
_
! \fIexpr\fR	Logical not	numeric	n/a
+ \fIexpr\fR	Unary plus	numeric	n/a
\(mi \fIexpr\fR	Unary minus	numeric	n/a
_
\fIexpr\fR * \fIexpr\fR	Multiplication	numeric	left
\fIexpr\fR / \fIexpr\fR	Division	numeric	left
\fIexpr\fR % \fIexpr\fR	Modulus	numeric	left
_
\fIexpr\fR + \fIexpr\fR	Addition	numeric	left
\fIexpr\fR \(mi \fIexpr\fR	Subtraction	numeric	left
_
\fIexpr\fR \fIexpr\fR	String concatenation	string	left
_
\fIexpr\fR < \fIexpr\fR	Less than	numeric	none
\fIexpr\fR <= \fIexpr\fR	Less than or equal to	numeric	none
\fIexpr\fR != \fIexpr\fR	Not equal to	numeric	none
\fIexpr\fR == \fIexpr\fR	Equal to	numeric	none
\fIexpr\fR > \fIexpr\fR	Greater than	numeric	none
\fIexpr\fR >= \fIexpr\fR	Greater than or equal to	numeric	none
_
\fIexpr\fR ~ \fIexpr\fR	ERE match	numeric	none
\fIexpr\fR !~ \fIexpr\fR	ERE non-match	 numeric	none
_
\fIexpr\fR in array	Array membership	numeric	left
( \fIindex\fR ) in	Multi-dimension array	numeric	left
    \fIarray\fR	    membership
_
\fBexpr\fR && \fIexpr\fR	Logical AND	numeric	left
_
\fBexpr\fR |\|| \fIexpr\fR	Logical OR	numeric	left
_
\fIexpr1\fR ? \fIexpr2\fR	Conditional expression	type of selected	right
    : \fIexpr3\fR		   \fIexpr2\fR or \fIexpr3\fR
_
\fIlvalue\fR ^= \fIexpr\fR	Exponentiation	numeric	right
	assignment
\fIlvalue\fR %= \fIexpr\fR	Modulus assignment	numeric	right
\fIlvalue\fR *= \fIexpr\fR	Multiplication	numeric	right
	assignment
\fIlvalue\fR /= \fIexpr\fR	Division assignment	numeric	right
\fIlvalue\fR +=  \fIexpr\fR	Addition assignment	numeric	right
\fIlvalue\fR \(mi= \fIexpr\fR	Subtraction assignment	numeric	right
\fIlvalue\fR = \fIexpr\fR	Assignment	type of \fIexpr\fR	right
.TE

.sp
.LP
Each expression has either a string value, a numeric value or both. Except as
stated for specific contexts, the value of an expression is implicitly
converted to the type needed for the context in which it is used.  A string
value is converted to a numeric value by the equivalent of the following calls:
.sp
.in +2
.nf
setlocale(LC_NUMERIC, "");
\fInumeric_value\fR = atof(\fIstring_value\fR);
.fi
.in -2

.sp
.LP
A numeric value that is exactly equal to the value of an integer is converted
to a string by the equivalent of a call to the \fBsprintf\fR function with the
string \fB%d\fR as the \fBfmt\fR argument and the numeric value being converted
as the first and only \fIexpr\fR argument.  Any other numeric value is
converted to a string by the equivalent of a call to the \fBsprintf\fR function
with the value of the variable \fBCONVFMT\fR as the \fBfmt\fR argument and the
numeric value being converted as the first and only \fIexpr\fR argument.
.sp
.LP
A string value is considered to be a \fInumeric string\fR in the following
case:
.RS +4
.TP
1.
Any leading and trailing blank characters is ignored.
.RE
.RS +4
.TP
2.
If the first unignored character is a \fB+\fR or \fB\(mi\fR, it is ignored.
.RE
.RS +4
.TP
3.
If the remaining unignored characters would be lexically recognized as a
\fBNUMBER\fR token, the string is considered a \fInumeric string\fR.
.RE
.sp
.LP
If a \fB\(mi\fR character is ignored in the above steps, the numeric value of
the \fInumeric string\fR is the negation of the numeric value of the recognized
\fBNUMBER\fR token. Otherwise the numeric value of the \fInumeric string\fR is
the numeric value of the recognized \fBNUMBER\fR token. Whether or not a string
is a \fInumeric string\fR is relevant only in contexts where that term is used
in this section.
.sp
.LP
When an expression is used in a Boolean context, if it has a numeric value, a
value of zero is treated as false and any other value is treated as true.
Otherwise, a string value of the null string is treated as false and any other
value is treated as true. A Boolean context is one of the following:
.RS +4
.TP
.ie t \(bu
.el o
the first subexpression of a conditional expression.
.RE
.RS +4
.TP
.ie t \(bu
.el o
an expression operated on by logical NOT, logical \fBAND,\fR or logical OR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
the second expression of a \fBfor\fR statement.
.RE
.RS +4
.TP
.ie t \(bu
.el o
the expression of an \fBif\fR statement.
.RE
.RS +4
.TP
.ie t \(bu
.el o
the expression of the \fBwhile\fR clause in either a \fBwhile\fR or \fBdo\fR
\fB\&.\|.\|.\fR \fBwhile\fR statement.
.RE
.RS +4
.TP
.ie t \(bu
.el o
an expression used as a pattern (as in Overall Program Structure).
.RE
.sp
.LP
The \fBawk\fR language supplies arrays that are used for storing numbers or
strings. Arrays need not be declared. They are initially empty, and their sizes
changes dynamically. The subscripts, or element identifiers, are strings,
providing a type of associative array capability. An array name followed by a
subscript within square brackets can be used as an \fIlvalue\fR and as an
expression, as described in the grammar.  Unsubscripted array names are used in
only the following contexts:
.RS +4
.TP
.ie t \(bu
.el o
a parameter in a function definition or function call.
.RE
.RS +4
.TP
.ie t \(bu
.el o
the \fBNAME\fR token following any use of the keyword \fBin\fR.
.RE
.sp
.LP
A valid array \fIindex\fR consists of one or more comma-separated expressions,
similar to the way in which multi-dimensional arrays are indexed in some
programming languages. Because \fBawk\fR arrays are really one-dimensional,
such a comma-separated list is converted to a single string by concatenating
the string values of the separate expressions, each separated from the other by
the value of the \fBSUBSEP\fR variable.
.sp
.LP
Thus, the following two index operations are equivalent:
.sp
.in +2
.nf
var[expr1, expr2, ... exprn]
var[expr1 SUBSEP expr2 SUBSEP ... SUBSEP exprn]
.fi
.in -2

.sp
.LP
A multi-dimensioned \fIindex\fR used with the \fBin\fR operator must be put in
parentheses. The \fBin\fR operator, which tests for the existence of a
particular array element, does not create the element if it does not exist.
Any other reference to a non-existent array element automatically creates it.

.SS "Variables and Special Variables"
Variables can be used in an \fBawk\fR program by referencing them. With the
exception of function parameters, they are not explicitly declared.
Uninitialized scalar variables and array elements have both a numeric value of
zero and a string value of the empty string.
.sp
.LP
Field variables are designated by a \fB$\fR followed by a number or numerical
expression. The effect of the field number \fIexpression\fR 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 are created by assigning a value to them.
References to non-existent fields (that is, fields after \fB$NF\fR) produce the
null string. However, assigning to a non-existent field (for example,
\fB$(NF+2) = 5\fR) increases the value of \fBNF\fR, create any intervening
fields with the null string as their values and cause the value of \fB$0\fR to
be recomputed, with the fields being separated by the value of \fBOFS\fR. Each
field variable has a string value when created. If the string, with any
occurrence of the decimal-point character from the current locale changed to a
period character, is considered a \fInumeric string\fR (see \fBExpressions in
awk\fR above), the field variable also has the numeric value of the \fInumeric
string\fR.

.SS "/usr/bin/awk, /usr/xpg4/bin/awk"
\fBawk\fR sets the following special variables that are supported by both
\fB/usr/bin/awk\fR and \fB/usr/xpg4/bin/awk\fR:
.sp
.ne 2
.na
\fB\fBARGC\fR\fR
.ad
.RS 12n
The number of elements in the \fBARGV\fR array.
.RE

.sp
.ne 2
.na
\fB\fBARGV\fR\fR
.ad
.RS 12n
An array of command line arguments, excluding options and the \fIprogram\fR
argument, numbered from zero to \fBARGC\fR\(mi1.
.sp
The arguments in \fBARGV\fR can be modified or added to; \fBARGC\fR can be
altered.  As each input file ends, \fBawk\fR treats the next non-null element
of \fBARGV\fR, up to the current value of \fBARGC\fR\(mi1, inclusive, as the
name of the next input file.  Setting an element of \fBARGV\fR to null means
that it is not treated as an input file. The name \fB\(mi\fR indicates the
standard input. If an argument matches the format of an \fIassignment\fR
operand, this argument is treated as an assignment rather than a \fIfile\fR
argument.
.RE

.sp
.ne 2
.na
\fB\fBCONVFMT\fR\fR
.ad
.RS 12n
The \fBprintf\fR format for converting numbers to strings (except for output
statements, where \fBOFMT\fR is used). The default is \fB%.6g\fR.
.RE

.sp
.ne 2
.na
\fB\fBENVIRON\fR\fR
.ad
.RS 12n
The variable \fBENVIRON\fR is an array representing the value of the
environment. The indices of the array are strings consisting of the names of
the environment variables, and the value of each array element is a string
consisting of the value of that variable. If the value of an environment
variable is considered a \fInumeric string\fR, the array element also has its
numeric value.
.sp
In all cases where \fBawk\fR behavior is affected by environment variables
(including the environment of any commands that \fBawk\fR executes via the
\fBsystem\fR function or via pipeline redirections with the \fBprint\fR
statement, the \fBprintf\fR statement, or the \fBgetline\fR function), the
environment used is the environment at the time \fBawk\fR began executing.
.RE

.sp
.ne 2
.na
\fB\fBFILENAME\fR\fR
.ad
.RS 12n
A pathname of the current input file. Inside a \fBBEGIN\fR action the value is
undefined. Inside an \fBEND\fR action the value is the name of the last input
file processed.
.RE

.sp
.ne 2
.na
\fB\fBFNR\fR\fR
.ad
.RS 12n
The ordinal number of the current record in the current file. Inside a
\fBBEGIN\fR action the value is zero. Inside an \fBEND\fR action the value is
the number of the last record processed in the last file processed.
.RE

.sp
.ne 2
.na
\fB\fBFS\fR\fR
.ad
.RS 12n
Input field separator regular expression; a space character by default.
.RE

.sp
.ne 2
.na
\fB\fBNF\fR\fR
.ad
.RS 12n
The number of fields in the current record. Inside a \fBBEGIN\fR action, the
use of \fBNF\fR is undefined unless a \fBgetline\fR function without a
\fIvar\fR argument is executed previously. Inside an \fBEND\fR action, \fBNF\fR
retains the value it had for the last record read, unless a subsequent,
redirected, \fBgetline\fR function without a \fIvar\fR argument is performed
prior to entering the \fBEND\fR action.
.RE

.sp
.ne 2
.na
\fB\fBNR\fR\fR
.ad
.RS 12n
The ordinal number of the current record from the start of input. Inside a
\fBBEGIN\fR action the value is zero. Inside an \fBEND\fR action the value is
the number of the last record processed.
.RE

.sp
.ne 2
.na
\fB\fBOFMT\fR\fR
.ad
.RS 12n
The \fBprintf\fR format for converting numbers to strings in output statements
\fB"%.6g"\fR by default. The result of the conversion is unspecified if the
value of \fBOFMT\fR is not a floating-point format specification.
.RE

.sp
.ne 2
.na
\fB\fBOFS\fR\fR
.ad
.RS 12n
The \fBprint\fR statement output field separator; a space character by default.
.RE

.sp
.ne 2
.na
\fB\fBORS\fR\fR
.ad
.RS 12n
The \fBprint\fR output record separator; a newline character by default.
.RE

.sp
.ne 2
.na
\fB\fBRLENGTH\fR\fR
.ad
.RS 12n
The length of the string matched by the \fBmatch\fR function.
.RE

.sp
.ne 2
.na
\fB\fBRS\fR\fR
.ad
.RS 12n
The first character of the string value of \fBRS\fR is the input record
separator; a newline character by default. If \fBRS\fR contains more than one
character, the results are unspecified. If \fBRS\fR is null, then records are
separated by sequences of one or more blank lines. Leading or trailing blank
lines do not produce empty records at the beginning or end of input, and the
field separator is always newline, no matter what the value of \fBFS\fR.
.RE

.sp
.ne 2
.na
\fB\fBRSTART\fR\fR
.ad
.RS 12n
The starting position of the string matched by the \fBmatch\fR function,
numbering from 1. This is always equivalent to the return value of the
\fBmatch\fR function.
.RE

.sp
.ne 2
.na
\fB\fBSUBSEP\fR\fR
.ad
.RS 12n
The subscript separator string for multi-dimensional arrays. The default value
is \fB\e034\fR\&.
.RE

.SS "/usr/bin/awk"
The following variable is supported for \fB/usr/bin/awk\fR only:
.sp
.ne 2
.na
\fB\fBRT\fR\fR
.ad
.RS 12n
The record terminator for the most recent record read. For most records this
will be the same value as \fBRS\fR. At the end of a file with no trailing
separator value, though, this will be set to the empty string (\fB""\fR).
.RE

.SS "Regular Expressions"
The \fBawk\fR utility makes use of the extended regular expression notation
(see \fBregex\fR(5)) except that it allows the use of C-language conventions to
escape special characters within the EREs, namely \fB\e\e\fR, \fB\ea\fR,
\fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR, \fB\et\fR, \fB\ev\fR, and those
specified in the following table.  These escape sequences are 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 \fB\en\fR sequence is valid in \fBawk\fR EREs.  Using
a slash character within the regular expression requires escaping as shown in
the table below:
.sp

.sp
.TS
l l l
l l l .
\fBEscape Sequence\fR	\fBDescription\fR	\fBMeaning\fR
_
\fB\e"\fR	Backslash quotation-mark	Quotation-mark character
_
\fB\e/\fR	Backslash slash	Slash character
_
\fB\e\fR\fIddd\fR	T{
A backslash character followed by the longest sequence of one, two, or three octal-digit characters (01234567).  If all of the digits are 0, (that is, representation of the NULL character), the behavior is undefined.
T}	T{
The character encoded by the one-, two- or three-digit octal integer. Multi-byte characters require multiple, concatenated escape sequences, including the leading \e for each byte.
T}
_
\fB\e\fR\fIc\fR	T{
A backslash character followed by any character not described in this table or special characters (\fB\e\e\fR, \fB\ea\fR, \fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR, \fB\et\fR, \fB\ev\fR).
T}	Undefined
.TE

.sp
.LP
A regular expression can be matched against a specific field or string by using
one of the two regular expression matching operators, \fB~\fR and \fB!\|~\fR.
These operators 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 \fB~\fR expression evaluates to the value \fB1\fR, and the
\fB!\|~\fR expression evaluates to the value \fB0\fR. If the regular expression
does not match the string, the \fB~\fR expression evaluates to the value
\fB0\fR, and the \fB!\|~\fR expression evaluates to the value \fB1\fR. If the
right-hand operand is any expression other than the lexical token \fBERE\fR,
the string value of the expression is interpreted as an extended regular
expression, including the escape conventions described above. Notice that these
same escape conventions also are applied in the determining the value of a
string literal (the lexical token \fBSTRING\fR), and is applied a second time
when a string literal is used in this context.
.sp
.LP
When an \fBERE\fR token appears as an expression in any context other than as
the right-hand of the \fB~\fR or \fB!\|~\fR operator or as one of the built-in
function arguments described below, the value of the resulting expression is
the equivalent of:
.sp
.in +2
.nf
$0 ~ /\fIere\fR/
.fi
.in -2

.sp
.LP
The \fIere\fR argument to the \fBgsub,\fR \fBmatch,\fR \fBsub\fR functions, and
the \fIfs\fR argument to the \fBsplit\fR function (see \fBString Functions\fR)
is interpreted as extended regular expressions. These can be either \fBERE\fR
tokens or arbitrary expressions, and are interpreted in the same manner as the
right-hand side of the \fB~\fR or \fB!\|~\fR operator.
.sp
.LP
An extended regular expression can be used to separate fields by using the
\fB-F\fR \fIERE\fR option or by assigning a string containing the expression to
the built-in variable \fBFS\fR. The default value of the \fBFS\fR variable is a
single space character. The following describes \fBFS\fR behavior:
.RS +4
.TP
1.
If \fBFS\fR is a single character:
.RS +4
.TP
.ie t \(bu
.el o
If \fBFS\fR is the space character, skip leading and trailing blank characters;
fields are delimited by sets of one or more blank characters.
.RE
.RS +4
.TP
.ie t \(bu
.el o
Otherwise, if \fBFS\fR is any other character \fIc\fR, fields are delimited by
each single occurrence of \fIc\fR.
.RE
.RE
.RS +4
.TP
2.
Otherwise, the string value of \fBFS\fR is considered to be an extended
regular expression. Each occurrence of a sequence matching the extended regular
expression delimits fields.
.RE
.sp
.LP
Except in the \fBgsub\fR, \fBmatch\fR, \fBsplit\fR, and \fBsub\fR built-in
functions, regular expression matching is based on input records. That is,
record separator characters (the first character of the value of the variable
\fBRS\fR, a newline character by default) cannot be embedded in the expression,
and no expression matches the record separator character. If the record
separator is not a newline character, newline characters embedded in the
expression can be matched. In those four built-in functions, regular expression
matching are based on text strings. So, any character (including the newline
character and the record separator) can be embedded in the pattern and an
appropriate pattern matches any character. However, in all \fBawk\fR regular
expression matching, the use of one or more NULL characters in the pattern,
input record or text string produces undefined results.

.SS "Patterns"
A \fIpattern\fR is any valid \fIexpression,\fR a range specified by two
expressions separated by comma, or one of the two special patterns \fBBEGIN\fR
or \fBEND\fR.

.SS "Special Patterns"
The \fBawk\fR utility recognizes two special patterns, \fBBEGIN\fR and
\fBEND\fR. Each \fBBEGIN\fR pattern is matched once and its associated action
executed before the first record of input is read (except possibly by use of
the \fBgetline\fR function in a prior \fBBEGIN\fR action) and before command
line assignment is done. Each \fBEND\fR pattern is matched once and its
associated action executed after the last record of input has been read. These
two patterns have associated actions.
.sp
.LP
\fBBEGIN\fR and \fBEND\fR do not combine with other patterns.  Multiple
\fBBEGIN\fR and \fBEND\fR patterns are allowed. The actions associated with the
\fBBEGIN\fR patterns are executed in the order specified in the program, as are
the \fBEND\fR actions. An \fBEND\fR pattern can precede a \fBBEGIN\fR pattern
in a program.
.sp
.LP
If an \fBawk\fR program consists of only actions with the pattern \fBBEGIN\fR,
and the \fBBEGIN\fR action contains no \fBgetline\fR function, \fBawk\fR exits
without reading its input when the last statement in the last \fBBEGIN\fR
action is executed. If an \fBawk\fR program consists of only actions with the
pattern \fBEND\fR or only actions with the patterns \fBBEGIN\fR and \fBEND\fR,
the input is read before the statements in the \fBEND\fR actions are executed.

.SS "Expression Patterns"
An expression pattern is evaluated as if it were an expression in a Boolean
context. If the result is true, the pattern is considered to match, and the
associated action (if any) is executed. If the result is false, the action is
not executed.

.SS "Pattern Ranges"
A pattern range consists of two expressions separated by a comma. In this case,
the action is 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.

.SS "Actions"
An action is a sequence of statements. A statement can be one of the following:
.sp
.in +2
.nf
if ( \fIexpression\fR ) \fIstatement\fR [ else \fIstatement\fR ]
while ( \fIexpression\fR ) \fIstatement\fR
do \fIstatement\fR while ( \fIexpression\fR )
for ( \fIexpression\fR ; \fIexpression\fR ; \fIexpression\fR ) \fIstatement\fR
for ( \fIvar\fR in \fIarray\fR ) \fIstatement\fR
delete \fIarray\fR[\fIsubscript\fR] #delete an array element
delete \fIarray\fR #delete all elements within an array
break
continue
{ [ \fIstatement\fR ] .\|.\|. }
\fIexpression\fR        # commonly variable = expression
print [ \fIexpression-list\fR ] [ >\fIexpression\fR ]
printf format [ ,\fIexpression-list\fR ] [ >\fIexpression\fR ]
next              # skip remaining patterns on this input line
nextfile          # skip remaining patterns on this input file
exit [expr] # skip the rest of the input; exit status is expr
return [expr]
.fi
.in -2

.sp
.LP
Any single statement can be replaced by a statement list enclosed in braces.
The statements are terminated by newline characters or semicolons, and are
executed sequentially in the order that they appear.
.sp
.LP
The \fBnext\fR statement causes all further processing of the current input
record to be abandoned. The behavior is undefined if a \fBnext\fR statement
appears or is invoked in a \fBBEGIN\fR or \fBEND\fR action.
.sp
.LP
The \fBnextfile\fR statement is similar to \fBnext\fR, but also skips all other
records in the current file, and moves on to processing the next input file if
available (or exits the program if there are none). (Note that this keyword is
not supported by \fB/usr/xpg4/bin/awk\fR.)
.sp
.LP
The \fBexit\fR statement invokes all \fBEND\fR actions in the order in which
they occur in the program source and then terminate the program without reading
further input. An \fBexit\fR statement inside an \fBEND\fR action terminates
the program without further execution of \fBEND\fR actions.  If an expression
is specified in an \fBexit\fR statement, its numeric value is the exit status
of \fBawk\fR, unless subsequent errors are encountered or a subsequent
\fBexit\fR statement with an expression is executed.

.SS "Output Statements"
Both \fBprint\fR and \fBprintf\fR statements write to standard output by
default.  The output is written to the location specified by
\fIoutput_redirection\fR if one is supplied, as follows:
.sp
.in +2
.nf
\fB>\fR \fIexpression\fR\fB>>\fR \fIexpression\fR\fB|\fR \fIexpression\fR
.fi
.in -2

.sp
.LP
In all cases, the \fIexpression\fR is evaluated to produce a string that is
used as a full pathname to write into (for \fB>\fR or \fB>>\fR) or as a command
to be executed (for \fB|\fR). Using the first two forms, if the file of that
name is not currently open, it is opened, creating it if necessary and using
the first form, truncating the file. The output then is appended to the file.
As long as the file remains open, subsequent calls in which \fIexpression\fR
evaluates to the same string value simply appends output to the file. The file
remains open until the \fBclose\fR function, which is called with an expression
that evaluates to the same string value.
.sp
.LP
The third form writes output onto a stream piped to the input of a command. The
stream is created if no stream is currently open with the value of
\fIexpression\fR as its command name.  The stream created is equivalent to one
created by a call to the \fBpopen\fR(3C) function with the value of
\fIexpression\fR as the \fIcommand\fR argument and a value of \fBw\fR as the
\fImode\fR argument.  As long as the stream remains open, subsequent calls in
which \fIexpression\fR evaluates to the same string value writes output to the
existing stream. The stream remains open until the \fBclose\fR function is
called with an expression that evaluates to the same string value.  At that
time, the stream is closed as if by a call to the \fBpclose\fR function.
.sp
.LP
These output statements take a comma-separated list of \fIexpression\fR \fIs\fR
referred in the grammar by the non-terminal symbols \fBexpr_list,\fR
\fBprint_expr_list\fR or \fBprint_expr_list_opt.\fR This list is referred to
here as the \fIexpression list\fR, and each member is referred to as an
\fIexpression argument\fR.
.sp
.LP
The \fBprint\fR statement writes the value of each expression argument onto the
indicated output stream separated by the current output field separator (see
variable \fBOFS\fR above), and terminated by the output record separator (see
variable \fBORS\fR above). All expression arguments is taken as strings, being
converted if necessary; with the exception that the \fBprintf\fR format in
\fBOFMT\fR is used instead of the value in \fBCONVFMT\fR. An empty expression
list stands for the whole input record \fB(\fR$0\fB)\fR.
.sp
.LP
The \fBprintf\fR statement produces output based on a notation similar to the
File Format Notation used to describe file formats in this document Output is
produced as specified with the first expression argument as the string
\fBformat\fR and subsequent expression arguments as the strings \fBarg1\fR to
\fBargn,\fR inclusive, with the following exceptions:
.RS +4
.TP
1.
The \fIformat\fR is an actual character string rather than a graphical
representation. Therefore, it cannot contain empty character positions. The
space character in the \fIformat\fR string, in any context other than a
\fIflag\fR of a conversion specification, is treated as an ordinary character
that is copied to the output.
.RE
.RS +4
.TP
2.
If the character set contains a Delta character and that character appears
in the \fIformat\fR string, it is treated as an ordinary character that is
copied to the output.
.RE
.RS +4
.TP
3.
The \fIescape sequences\fR beginning with a backslash character is treated
as sequences of ordinary characters that are copied to the output. Note that
these same sequences is interpreted lexically by \fBawk\fR when they appear in
literal strings, but they is not treated specially by the \fBprintf\fR
statement.
.RE
.RS +4
.TP
4.
A \fIfield width\fR or \fIprecision\fR can be specified as the \fB*\fR
character instead of a digit string. In this case the next argument from the
expression list is fetched and its numeric value taken as the field width or
precision.
.RE
.RS +4
.TP
5.
The implementation does not precede or follow output from the \fBd\fR or
\fBu\fR conversion specifications with blank characters not specified by the
\fIformat\fR string.
.RE
.RS +4
.TP
6.
The implementation does not precede output from the \fBo\fR conversion
specification with leading zeros not specified by the \fIformat\fR string.
.RE
.RS +4
.TP
7.
For the \fBc\fR conversion specification: if the argument has a numeric
value, the character whose encoding is that value is output.  If the value is
zero or is not the encoding of any character in the character set, the behavior
is undefined.  If the argument does not have a numeric value, the first
character of the string value is output; if the string does not contain any
characters the behavior is undefined.
.RE
.RS +4
.TP
8.
For each conversion specification that consumes an argument, the next
expression argument is evaluated. With the exception of the \fBc\fR conversion,
the value is converted to the appropriate type for the conversion
specification.
.RE
.RS +4
.TP
9.
If there are insufficient expression arguments to satisfy all the conversion
specifications in the \fIformat\fR string, the behavior is undefined.
.RE
.RS +4
.TP
10.
If any character sequence in the \fIformat\fR string begins with a %
character, but does not form a valid conversion specification, the behavior is
unspecified.
.RE
.sp
.LP
Both \fBprint\fR and \fBprintf\fR can output at least \fB{LINE_MAX}\fR bytes.

.SS "Functions"
The \fBawk\fR language has a variety of built-in functions: arithmetic,
string, input/output and general.

.SS "Arithmetic Functions"
The arithmetic functions, except for \fBint\fR, are based on the \fBISO\fR
\fBC\fR standard. The behavior is undefined in cases where the \fBISO\fR
\fBC\fR standard specifies that an error be returned or that the behavior is
undefined. Although the 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 \fB[ ]\fR
brackets), such use is undefined.
.sp
.ne 2
.na
\fB\fBatan2(\fR\fIy\fR,\fIx\fR\fB)\fR\fR
.ad
.RS 17n
Return arctangent of \fIy\fR/\fIx\fR.
.RE

.sp
.ne 2
.na
\fB\fBcos\fR(\fIx\fR)\fR
.ad
.RS 17n
Return cosine of \fIx,\fR where \fIx\fR is in radians.
.RE

.sp
.ne 2
.na
\fB\fBsin\fR(\fIx\fR)\fR
.ad
.RS 17n
Return sine of \fIx,\fR where \fIx\fR is in radians.
.RE

.sp
.ne 2
.na
\fB\fBexp\fR(\fIx\fR)\fR
.ad
.RS 17n
Return the exponential function of \fIx\fR.
.RE

.sp
.ne 2
.na
\fB\fBlog\fR(\fIx\fR)\fR
.ad
.RS 17n
Return the natural logarithm of \fIx\fR.
.RE

.sp
.ne 2
.na
\fB\fBsqrt\fR(\fIx\fR)\fR
.ad
.RS 17n
Return the square root of \fIx\fR.
.RE

.sp
.ne 2
.na
\fB\fBint\fR(\fIx\fR)\fR
.ad
.RS 17n
Truncate its argument to an integer. It is truncated toward 0 when \fIx\fR > 0.
.RE

.sp
.ne 2
.na
\fB\fBrand()\fR\fR
.ad
.RS 17n
Return a random number \fIn\fR, such that 0 \(<= \fIn\fR < 1.
.RE

.sp
.ne 2
.na
\fB\fBsrand\fR([\fBexpr\fR])\fR
.ad
.RS 17n
Set the seed value for \fBrand\fR to \fIexpr\fR or use the time of day if
\fIexpr\fR is omitted. The previous seed value is returned.
.RE

.SS "String Functions"
The string functions in the following list shall be supported. Although the
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 \fB[ ]\fR brackets), such use is undefined.
.sp
.ne 2
.na
\fB\fBgsub\fR(\fIere\fR,\fIrepl\fR[,\|\fIin\fR])\fR
.ad
.sp .6
.RS 4n
Behave like \fBsub\fR (see below), except that it replaces all occurrences of
the regular expression (like the \fBed\fR utility global substitute) in
\fB$0\fR or in the \fIin\fR argument, when specified.
.RE

.sp
.ne 2
.na
\fB\fBindex\fR(\fIs\fR,\fIt\fR)\fR
.ad
.sp .6
.RS 4n
Return the position, in characters, numbering from 1, in string \fIs\fR where
string \fIt\fR first occurs, or zero if it does not occur at all.
.RE

.sp
.ne 2
.na
\fB\fBlength\fR[([\fIv\fR])]\fR
.ad
.sp .6
.RS 4n
Given no argument, this function returns the length of the whole record,
\fB$0\fR. If given an array as an argument (and using \fB/usr/bin/awk\fR),
then this returns the number of elements it contains. Otherwise, this function
interprets the argument as a string (performing any needed conversions) and
returns its length in characters.
.RE

.sp
.ne 2
.na
\fB\fBmatch\fR(\fIs\fR,\fIere\fR)\fR
.ad
.sp .6
.RS 4n
Return the position, in characters, numbering from 1, in string \fIs\fR where
the extended regular expression \fIere\fR occurs, or zero if it does not occur
at all. \fBRSTART\fR is set to the starting position (which is the same as the
returned value), zero if no match is found; \fBRLENGTH\fR is set to the length
of the matched string, \(mi1 if no match is found.
.RE

.sp
.ne 2
.na
\fB\fBsplit\fR(\fIs\fR,\fIa\fR[,\|\fIfs\fR])\fR
.ad
.sp .6
.RS 4n
Split the string \fIs\fR into array elements \fIa\fR[1], \fIa\fR[2],
\fB\&...,\fR \fIa\fR[\fIn\fR], and return \fIn\fR. The separation is done with
the extended regular expression \fIfs\fR or with the field separator \fBFS\fR
if \fIfs\fR is not given. Each array element has a string value when created.
If the string assigned to any array element, with any occurrence of the
decimal-point character from the current locale changed to a period character,
would be considered a \fInumeric string\fR; the array element also has the
numeric value of the \fInumeric string\fR. The effect of a null string as the
value of \fIfs\fR is unspecified.
.RE

.sp
.ne 2
.na
\fB\fBsprintf\fR(\fBfmt\fR,\fIexpr\fR,\fIexpr\fR,\fB\&...\fR)\fR
.ad
.sp .6
.RS 4n
Format the expressions according to the \fBprintf\fR format given by \fIfmt\fR
and return the resulting string.
.RE

.sp
.ne 2
.na
\fB\fBsub\fR(\fIere\fR,\fIrepl\fR[,\|\fIin\fR])\fR
.ad
.sp .6
.RS 4n
Substitute the string \fIrepl\fR in place of the first instance of the extended
regular expression \fBERE\fR in string in and return the number of
substitutions. An ampersand ( \fB&\fR ) appearing in the string \fIrepl\fR is
replaced by the string from in that matches the regular expression. An
ampersand preceded with a backslash ( \fB\e\fR ) is interpreted as the literal
ampersand character. An occurrence of two consecutive backslashes is
interpreted as just a single literal backslash character.  Any other occurrence
of a backslash (for example, preceding any other character) is treated as a
literal backslash character. If \fIrepl\fR is a string literal, the handling of
the ampersand character occurs after any lexical processing, including any
lexical backslash escape sequence processing. If \fBin\fR is specified and it
is not an \fBlvalue\fR the behavior is undefined. If in is omitted, \fBawk\fR
uses the current record (\fB$0\fR) in its place.
.RE

.sp
.ne 2
.na
\fB\fBsubstr\fR(\fIs\fR,\fIm\fR[,\|\fIn\fR])\fR
.ad
.sp .6
.RS 4n
Return the at most \fIn\fR-character substring of \fIs\fR that begins at
position \fIm,\fR numbering from 1. If \fIn\fR is missing, the length of the
substring is limited by the length of the string \fIs\fR.
.RE

.sp
.ne 2
.na
\fB\fBtolower\fR(\fIs\fR)\fR
.ad
.sp .6
.RS 4n
Return a string based on the string \fIs\fR. Each character in \fIs\fR that is
an upper-case letter specified to have a \fBtolower\fR mapping by the
\fBLC_CTYPE\fR category of the current locale is replaced in the returned
string by the lower-case letter specified by the mapping. Other characters in
\fIs\fR are unchanged in the returned string.
.RE

.sp
.ne 2
.na
\fB\fBtoupper\fR(\fIs\fR)\fR
.ad
.sp .6
.RS 4n
Return a string based on the string \fIs\fR. Each character in \fIs\fR that is
a lower-case letter specified to have a \fBtoupper\fR mapping by the
\fBLC_CTYPE\fR category of the current locale is replaced in the returned
string by the upper-case letter specified by the mapping. Other characters in
\fIs\fR are unchanged in the returned string.
.RE

.sp
.LP
All of the preceding functions that take \fIERE\fR as a parameter expect a
pattern or a string valued expression that is a regular expression as defined
below.

.SS "Input/Output and General Functions"
The input/output and general functions are:
.sp
.ne 2
.na
\fB\fBclose(\fR\fIexpression\fR)\fR
.ad
.RS 27n
Close the file or pipe opened by a \fBprint\fR or \fBprintf\fR statement or a
call to \fBgetline\fR with the same string-valued \fIexpression\fR. If the
close was successful, the function returns \fB0\fR; otherwise, it returns
non-zero.
.RE

.sp
.ne 2
.na
\fB\fBfflush(\fR\fIexpression\fR)\fR
.ad
.RS 27n
Flush any buffered output for the file or pipe opened by a \fBprint\fR or
\fBprintf\fR statement or a call to \fBgetline\fR with the same string-valued
\fIexpression\fR. If the flush was successful, the function returns \fB0\fR;
otherwise, it returns \fBEOF\fR. If no arguments or the empty string
(\fB""\fR) are given, then all open files will be flushed. (Note that
\fBfflush\fR is supported in \fB/usr/bin/awk\fR only.)
.RE

.sp
.ne 2
.na
\fB\fIexpression\fR|\fBgetline\fR[\fIvar\fR]\fR
.ad
.RS 27n
Read a record of input from a stream piped from the output of a command. The
stream is created if no stream is currently open with the value of
\fIexpression\fR as its command name. The stream created is equivalent to one
created by a call to the \fBpopen\fR function with the value of
\fIexpression\fR as the \fIcommand\fR argument and a value of \fBr\fR as the
\fImode\fR argument. As long as the stream remains open, subsequent calls in
which \fIexpression\fR evaluates to the same string value reads subsequent
records from the file. The stream remains open until the \fBclose\fR function
is called with an expression that evaluates to the same string value. At that
time, the stream is closed as if by a call to the \fBpclose\fR function. If
\fIvar\fR is missing, \fB$0\fR and \fBNF\fR is set. Otherwise, \fIvar\fR is
set.
.sp
The \fBgetline\fR operator can form ambiguous constructs when there are
operators that are not in parentheses (including concatenate) to the left of
the \fB|\fR (to the beginning of the expression containing \fBgetline\fR). In
the context of the \fB$\fR operator, \fB|\fR behaves as if it had a lower
precedence than \fB$\fR. The result of evaluating other operators is
unspecified, and all such uses of portable applications must be put in
parentheses properly.
.RE

.sp
.ne 2
.na
\fB\fBgetline\fR\fR
.ad
.RS 27n
Set \fB$0\fR to the next input record from the current input file. This form of
\fBgetline\fR sets the \fBNF\fR, \fBNR\fR, and \fBFNR\fR variables.
.RE

.sp
.ne 2
.na
\fB\fBgetline\fR \fIvar\fR\fR
.ad
.RS 27n
Set variable \fIvar\fR to the next input record from the current input file.
This form of \fBgetline\fR sets the \fBFNR\fR and \fBNR\fR variables.
.RE

.sp
.ne 2
.na
\fB\fBgetline\fR [\fIvar\fR] \fB<\fR \fIexpression\fR\fR
.ad
.RS 27n
Read the next record of input from a named file. The \fIexpression\fR is
evaluated to produce a string that is used as a full pathname. If the file of
that name is not currently open, it is opened. As long as the stream remains
open, subsequent calls in which \fIexpression\fR evaluates to the same string
value reads subsequent records from the file. The file remains open until the
\fBclose\fR function is called with an expression that evaluates to the same
string value. If \fIvar\fR is missing, \fB$0\fR and \fBNF\fR is set. Otherwise,
\fIvar\fR is set.
.sp
The \fBgetline\fR operator can form ambiguous constructs when there are binary
operators that are not in parentheses (including concatenate) to the right of
the \fB<\fR (up to the end of the expression containing the \fBgetline\fR). The
result of evaluating such a construct is unspecified, and all such uses of
portable applications must be put in parentheses properly.
.RE

.sp
.ne 2
.na
\fB\fBsystem\fR(\fIexpression\fR)\fR
.ad
.RS 27n
Execute the command given by \fIexpression\fR in a manner equivalent to the
\fBsystem\fR(3C) function and return the exit status of the command.
.RE

.sp
.LP
All forms of \fBgetline\fR return \fB1\fR for successful input, \fB0\fR for end
of file, and \fB\(mi1\fR for an error.
.sp
.LP
Where strings are used as the name of a file or pipeline, the strings must be
textually identical. The terminology ``same string value'' implies that
``equivalent strings'', even those that differ only by space characters,
represent different files.

.SS "User-defined Functions"
The \fBawk\fR language also provides user-defined functions. Such functions
can be defined as:
.sp
.in +2
.nf
\fBfunction\fR \fIname\fR(\fIargs\fR,\|.\|.\|.) { \fIstatements\fR }
.fi
.in -2

.sp
.LP
A function can be referred to anywhere in an \fBawk\fR program; in particular,
its use can precede its definition. The scope of a function is global.
.sp
.LP
Function arguments can be either scalars or arrays; the behavior is undefined
if an array name is passed as an argument that the function uses as a scalar,
or if a scalar expression is passed as an argument that the function uses as an
array. Function arguments are passed by value if scalar and by reference if
array name. Argument names are local to the function; all other variable names
are global. The same name is not used as both an argument name and as the name
of a function or a special \fBawk\fR variable. The same name must not be used
both as a variable name with global scope and as the name of a function. The
same name must not be used within the same scope both as a scalar variable and
as an array.
.sp
.LP
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 are initialized with a string value of the null string and a
numeric value of zero, and the extra parameters that are used in the function
body as arrays are initialized as empty arrays. If more arguments are supplied
in a function call than are in the function definition, the behavior is
undefined.
.sp
.LP
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 are
unchanged, except for array parameters passed by reference. The \fBreturn\fR
statement can be used to return a value. If a \fBreturn\fR statement appears
outside of a function definition, the behavior is undefined.
.sp
.LP
In the function definition, newline characters are optional before the opening
brace and after the closing brace. Function definitions can appear anywhere in
the program where a \fIpattern-action\fR pair is allowed.

.SH USAGE
The \fBindex\fR, \fBlength\fR, \fBmatch\fR, and \fBsubstr\fR functions should
not be confused with similar functions in the \fBISO C\fR standard; the
\fBawk\fR versions deal with characters, while the \fBISO C\fR standard deals
with bytes.
.sp
.LP
Because the concatenation operation is represented by adjacent expressions
rather than an explicit operator, it is often necessary to use parentheses to
enforce the proper evaluation precedence.
.sp
.LP
See \fBlargefile\fR(5) for the description of the behavior of \fBawk\fR when
encountering files greater than or equal to 2 Gbyte (2^31 bytes).

.SH EXAMPLES
The \fBawk\fR program specified in the command line is most easily specified
within single-quotes (for example, \fB\&'\fR\fIprogram\fR\fB\&'\fR) for
applications using \fBsh\fR, because \fBawk\fR programs commonly contain
characters that are special to the shell, including double-quotes. In the cases
where a \fBawk\fR program contains single-quote characters, it is usually
easiest to specify most of the program as strings within single-quotes
concatenated by the shell with quoted single-quote characters. For example:
.sp
.in +2
.nf
awk '/'\e''/ { print "quote:", $0 }'
.fi
.in -2

.sp
.LP
prints all lines from the standard input containing a single-quote character,
prefixed with \fBquote:\fR.
.sp
.LP
The following are examples of simple \fBawk\fR programs:
.LP
\fBExample 1 \fRWrite to the standard output all input lines for which field 3
is greater than 5:
.sp
.in +2
.nf
\fB$3 > 5\fR
.fi
.in -2
.sp

.LP
\fBExample 2 \fRWrite every tenth line:
.sp
.in +2
.nf
\fB(NR % 10) == 0\fR
.fi
.in -2
.sp

.LP
\fBExample 3 \fRWrite any line with a substring matching the regular
expression:
.sp
.in +2
.nf
\fB/(G|D)(2[0-9][[:alpha:]]*)/\fR
.fi
.in -2
.sp

.LP
\fBExample 4 \fRPrint any line with a substring containing a G or D, followed
by a sequence of digits and characters:
.sp
.LP
This example uses character classes \fBdigit\fR and \fBalpha\fR to match
language-independent digit and alphabetic characters, respectively.

.sp
.in +2
.nf
\fB/(G|D)([[:digit:][:alpha:]]*)/\fR
.fi
.in -2
.sp

.LP
\fBExample 5 \fRWrite any line in which the second field matches the regular
expression and the fourth field does not:
.sp
.in +2
.nf
\fB$2 ~ /xyz/ && $4 !~ /xyz/\fR
.fi
.in -2
.sp

.LP
\fBExample 6 \fRWrite any line in which the second field contains a backslash:
.sp
.in +2
.nf
\fB$2 ~ /\e\e/\fR
.fi
.in -2
.sp

.LP
\fBExample 7 \fRWrite any line in which the second field contains a backslash
(alternate method):
.sp
.LP
Notice that backslash escapes are interpreted twice, once in lexical processing
of the string and once in processing the regular expression.

.sp
.in +2
.nf
\fB$2 ~ "\e\e\e\e"\fR
.fi
.in -2
.sp

.LP
\fBExample 8 \fRWrite the second to the last and the last field in each line,
separating the fields by a colon:
.sp
.in +2
.nf
\fB{OFS=":";print $(NF-1), $NF}\fR
.fi
.in -2
.sp

.LP
\fBExample 9 \fRWrite the line number and number of fields in each line:
.sp
.LP
The three strings representing the line number, the colon and the number of
fields are concatenated and that string is written to standard output.

.sp
.in +2
.nf
\fB{print NR ":" NF}\fR
.fi
.in -2
.sp

.LP
\fBExample 10 \fRWrite lines longer than 72 characters:
.sp
.in +2
.nf
\fB{length($0) > 72}\fR
.fi
.in -2
.sp

.LP
\fBExample 11 \fRWrite first two fields in opposite order separated by the OFS:
.sp
.in +2
.nf
\fB{ print $2, $1 }\fR
.fi
.in -2
.sp

.LP
\fBExample 12 \fRSame, with input fields separated by comma or space and tab
characters, or both:
.sp
.in +2
.nf
\fBBEGIN { FS = ",[\et]*|[\et]+" }
      { print $2, $1 }\fR
.fi
.in -2
.sp

.LP
\fBExample 13 \fRAdd up first column, print sum and average:
.sp
.in +2
.nf
\fB{s += $1 }
END {print "sum is ", s, " average is", s/NR}\fR
.fi
.in -2
.sp

.LP
\fBExample 14 \fRWrite fields in reverse order, one per line (many lines out
for each line in):
.sp
.in +2
.nf
\fB{ for (i = NF; i > 0; --i) print $i }\fR
.fi
.in -2
.sp

.LP
\fBExample 15 \fRWrite all lines between occurrences of the strings "start" and
"stop":
.sp
.in +2
.nf
\fB/start/, /stop/\fR
.fi
.in -2
.sp

.LP
\fBExample 16 \fRWrite all lines whose first field is different from the
previous one:
.sp
.in +2
.nf
\fB$1 != prev { print; prev = $1 }\fR
.fi
.in -2
.sp

.LP
\fBExample 17 \fRSimulate the echo command:
.sp
.in +2
.nf
\fBBEGIN  {
       for (i = 1; i < ARGC; ++i)
             printf "%s%s", ARGV[i], i==ARGC-1?"\en":""
       }\fR
.fi
.in -2
.sp

.LP
\fBExample 18 \fRWrite the path prefixes contained in the PATH environment
variable, one per line:
.sp
.in +2
.nf
\fBBEGIN  {
       n = split (ENVIRON["PATH"], path, ":")
       for (i = 1; i <= n; ++i)
              print path[i]
       }\fR
.fi
.in -2
.sp

.LP
\fBExample 19 \fRPrint the file "input", filling in page numbers starting at 5:
.sp
.LP
If there is a file named \fBinput\fR containing page headers of the form

.sp
.in +2
.nf
Page#
.fi
.in -2

.sp
.LP
and a file named \fBprogram\fR that contains

.sp
.in +2
.nf
/Page/{ $2 = n++; }
{ print }
.fi
.in -2

.sp
.LP
then the command line

.sp
.in +2
.nf
\fBawk -f program n=5 input\fR
.fi
.in -2
.sp

.sp
.LP
prints the file \fBinput\fR, filling in page numbers starting at 5.

.SH ENVIRONMENT VARIABLES
See \fBenviron\fR(5) for descriptions of the following environment variables
that affect execution: \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and
\fBNLSPATH\fR.
.sp
.ne 2
.na
\fB\fBLC_NUMERIC\fR\fR
.ad
.RS 14n
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 character (the decimal-point character of the
POSIX locale) is the decimal-point character recognized in processing \fBawk\fR
programs (including assignments in command-line arguments).
.RE

.SH EXIT STATUS
The following exit values are returned:
.sp
.ne 2
.na
\fB\fB0\fR\fR
.ad
.RS 6n
All input files were processed successfully.
.RE

.sp
.ne 2
.na
\fB\fB>0\fR\fR
.ad
.RS 6n
An error occurred.
.RE

.sp
.LP
The exit status can be altered within the program by using an \fBexit\fR
expression.

.SH SEE ALSO
\fBed\fR(1), \fBegrep\fR(1), \fBgrep\fR(1), \fBlex\fR(1), \fBoawk\fR(1),
\fBsed\fR(1), \fBpopen\fR(3C), \fBprintf\fR(3C), \fBsystem\fR(3C),
\fBattributes\fR(5), \fBenviron\fR(5), \fBlargefile\fR(5), \fBregex\fR(5),
\fBXPG4\fR(5)
.sp
.LP
Aho, A. V., B. W. Kernighan, and P. J. Weinberger, \fIThe AWK Programming
Language\fR, Addison-Wesley, 1988.

.SH DIAGNOSTICS
If any \fIfile\fR operand is specified and the named file cannot be accessed,
\fBawk\fR writes a diagnostic message to standard error and terminate without
any further action.
.sp
.LP
If the program specified by either the \fIprogram\fR operand or a
\fIprogfile\fR operand is not a valid \fBawk\fR program (as specified in
\fBEXTENDED DESCRIPTION\fR), the behavior is undefined.

.SH NOTES
Input white space is not preserved on output if fields are involved.
.sp
.LP
There are no explicit conversions between numbers and strings. To force an
expression to be treated as a number add 0 to it; to force it to be treated as
a string concatenate the null string (\fB""\fR) to it.