summaryrefslogtreecommitdiff
path: root/usr.bin/awk/awk.1
blob: e256d37178f40719c38ed71ac6d6b069e1dcf079 (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
.\"	$OpenBSD: awk.1,v 1.68 2023/09/21 18:16:12 jmc Exp $
.\"
.\" Copyright (C) Lucent Technologies 1997
.\" All Rights Reserved
.\"
.\" Permission to use, copy, modify, and distribute this software and
.\" its documentation for any purpose and without fee is hereby
.\" granted, provided that the above copyright notice appear in all
.\" copies and that both that the copyright notice and this
.\" permission notice and warranty disclaimer appear in supporting
.\" documentation, and that the name Lucent Technologies or any of
.\" its entities not be used in advertising or publicity pertaining
.\" to distribution of the software without specific, written prior
.\" permission.
.\"
.\" LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
.\" INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
.\" IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
.\" SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
.\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
.\" ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
.\" THIS SOFTWARE.
.\"
.Dd $Mdocdate: September 21 2023 $
.Dt AWK 1
.Os
.Sh NAME
.Nm awk
.Nd pattern-directed scanning and processing language
.Sh SYNOPSIS
.Nm awk
.Op Fl safe
.Op Fl V
.Op Fl d Ns Op Ar n
.Op Fl F Ar fs | Fl -csv
.Op Fl v Ar var Ns = Ns Ar value
.Op Ar prog | Fl f Ar progfile
.Ar
.Sh DESCRIPTION
.Nm
scans each input
.Ar file
for lines that match any of a set of patterns specified literally in
.Ar prog
or in one or more files specified as
.Fl f Ar progfile .
With each pattern there can be an associated action that will be performed
when a line of a
.Ar file
matches the pattern.
Each line is matched against the
pattern portion of every pattern-action statement;
the associated action is performed for each matched pattern.
The file name
.Sq -
means the standard input.
Any
.Ar file
of the form
.Ar var Ns = Ns Ar value
is treated as an assignment, not a filename,
and is executed at the time it would have been opened if it were a filename.
.Pp
The options are as follows:
.Bl -tag -width "-safe "
.It Fl -csv
Process records using the (more or less) standard comma-separated values
.Pq CSV
format instead of the input field separator.
When the
.Fl -csv
option is specified, attempts to change the input field separator
or record separator are ignored.
.It Fl d Ns Op Ar n
Debug mode.
Set debug level to
.Ar n ,
or 1 if
.Ar n
is not specified.
A value greater than 1 causes
.Nm
to dump core on fatal errors.
.It Fl F Ar fs
Define the input field separator to be the regular expression
.Ar fs .
.It Fl f Ar progfile
Read program code from the specified file
.Ar progfile
instead of from the command line.
.It Fl safe
Disable file output
.Pf ( Ic print No > ,
.Ic print No >> ) ,
process creation
.Po
.Ar cmd | Ic getline ,
.Ic print | ,
.Ic system
.Pc
and access to the environment
.Pf ( Va ENVIRON ;
see the section on variables below).
This is a first
.Pq and not very reliable
approximation to a
.Dq safe
version of
.Nm .
.It Fl V
Print the version number of
.Nm
to standard output and exit.
.It Fl v Ar var Ns = Ns Ar value
Assign
.Ar value
to variable
.Ar var
before
.Ar prog
is executed;
any number of
.Fl v
options may be present.
.El
.Pp
The input is normally made up of input lines
.Pq records
separated by newlines, or by the value of
.Va RS .
If
.Va RS
is null, then any number of blank lines are used as the record separator,
and newlines are used as field separators
(in addition to the value of
.Va FS ) .
This is convenient when working with multi-line records.
.Pp
An input line is normally made up of fields separated by whitespace,
or by the value of the field separator
.Va FS
at the time the line is read.
The fields are denoted
.Va $1 , $2 , ... ,
while
.Va $0
refers to the entire line.
.Va FS
may be set to either a single character or a regular expression.
As a special case, if
.Va FS
is a single space
.Pq the default ,
fields will be split by one or more whitespace characters.
If
.Va FS
is null, the input line is split into one field per character.
.Pp
Normally, any number of blanks separate fields.
In order to set the field separator to a single blank, use the
.Fl F
option with a value of
.Sq [\ \&] .
If a field separator of
.Sq t
is specified,
.Nm
treats it as if
.Sq \et
had been specified and uses
.Aq TAB
as the field separator.
In order to use a literal
.Sq t
as the field separator, use the
.Fl F
option with a value of
.Sq [t] .
The field separator is usually set via the
.Fl F
option or from inside a
.Ic BEGIN
block so that it takes effect before the input is read.
.Pp
A pattern-action statement has the form:
.Pp
.D1 Ar pattern Ic \&{ Ar action Ic \&}
.Pp
A missing
.Ic \&{ Ar action Ic \&}
means print the line;
a missing pattern always matches.
Pattern-action statements are separated by newlines or semicolons.
.Pp
Newlines are permitted after a terminating statement or following a comma
.Pq Sq ,\& ,
an open brace
.Pq Sq { ,
a logical AND
.Pq Sq && ,
a logical OR
.Pq Sq || ,
after the
.Sq do
or
.Sq else
keywords,
or after the closing parenthesis of an
.Sq if ,
.Sq for ,
or
.Sq while
statement.
Additionally, a backslash
.Pq Sq \e
can be used to escape a newline between tokens.
.Pp
An action is a sequence of statements.
A statement can be one of the following:
.Pp
.Bl -tag -width Ds -offset indent -compact
.It Ic if Ar ( expression ) Ar statement Op Ic else Ar statement
.It Ic while Ar ( expression ) Ar statement
.It Ic for Ar ( expression ; expression ; expression ) statement
.It Ic for Ar ( var Ic in Ar array ) statement
.It Ic do Ar statement Ic while Ar ( expression )
.It Ic break
.It Ic continue
.It Xo Ic {
.Op Ar statement ...
.Ic }
.Xc
.It Xo Ar expression
.No # commonly
.Ar var No = Ar expression
.Xc
.It Xo Ic print
.Op Ar expression-list
.Op > Ns Ar expression
.Xc
.It Xo Ic printf Ar format
.Op Ar ... , expression-list
.Op > Ns Ar expression
.Xc
.It Ic return Op Ar expression
.It Xo Ic next
.No # skip remaining patterns on this input line
.Xc
.It Xo Ic nextfile
.No # skip rest of this file, open next, start at top
.Xc
.It Xo Ic delete
.Sm off
.Ar array Ic \&[ Ar expression Ic \&]
.Sm on
.No # delete an array element
.Xc
.It Xo Ic delete Ar array
.No # delete all elements of array
.Xc
.It Xo Ic exit
.Op Ar expression
.No # exit processing, and perform
.Ic END
processing; status is
.Ar expression
.Xc
.El
.Pp
Statements are terminated by
semicolons, newlines or right braces.
An empty
.Ar expression-list
stands for
.Ar $0 .
String constants are quoted
.Li \&"" ,
with the usual C escapes recognized within
(see
.Xr printf 1
for a complete list of these).
Expressions take on string or numeric values as appropriate,
and are built using the operators
.Ic + \- * / % ^
.Pq exponentiation ,
and concatenation
.Pq indicated by whitespace .
The operators
.Ic \&! ++ \-\- += \-= *= /= %= ^=
.Ic > >= < <= == != ?\&:
are also available in expressions.
Variables may be scalars, array elements
(denoted
.Li x[i] )
or fields.
Variables are initialized to the null string.
Array subscripts may be any string,
not necessarily numeric;
this allows for a form of associative memory.
Multiple subscripts such as
.Li [i,j,k]
are permitted; the constituents are concatenated,
separated by the value of
.Va SUBSEP
.Pq see the section on variables below .
.Pp
The
.Ic print
statement prints its arguments on the standard output
(or on a file if
.Pf >\ \& Ar file
or
.Pf >>\ \& Ar file
is present or on a pipe if
.Pf |\ \& Ar cmd
is present), separated by the current output field separator,
and terminated by the output record separator.
.Ar file
and
.Ar cmd
may be literal names or parenthesized expressions;
identical string values in different statements denote
the same open file.
The
.Ic printf
statement formats its expression list according to the
.Ar format
(see
.Xr printf 1 ) .
.Pp
Patterns are arbitrary Boolean combinations
(with
.Ic "\&! || &&" )
of regular expressions and
relational expressions.
.Nm
supports extended regular expressions
.Pq EREs .
See
.Xr re_format 7
for more information on regular expressions.
Isolated regular expressions
in a pattern apply to the entire line.
Regular expressions may also occur in
relational expressions, using the operators
.Ic ~
and
.Ic !~ .
.Pf / Ar re Ns /
is a constant regular expression;
any string (constant or variable) may be used
as a regular expression,
except in the position of an isolated regular expression in a pattern.
.Pp
A pattern may consist of two patterns separated by a comma;
in this case, the action is performed for all lines
from an occurrence of the first pattern
through an occurrence of the second.
.Pp
A relational expression is one of the following:
.Pp
.Bl -tag -width Ds -offset indent -compact
.It Ar expression matchop regular-expression
.It Ar expression relop expression
.It Ar expression Ic in Ar array-name
.It Xo Ic \&( Ns
.Ar expr , expr , \&... Ns Ic \&) in
.Ar array-name
.Xc
.El
.Pp
where a
.Ar relop
is any of the six relational operators in C, and a
.Ar matchop
is either
.Ic ~
(matches)
or
.Ic !~
(does not match).
A conditional is an arithmetic expression,
a relational expression,
or a Boolean combination
of these.
.Pp
The special pattern
.Ic BEGIN
may be used to capture control before the first input line is read.
The special pattern
.Ic END
may be used to capture control after processing is finished.
.Ic BEGIN
and
.Ic END
do not combine with other patterns.
They may appear multiple times in a program and execute
in the order they are read by
.Nm .
.Pp
Variable names with special meanings:
.Pp
.Bl -tag -width "FILENAME " -compact
.It Va ARGC
Argument count, assignable.
.It Va ARGV
Argument array, assignable;
non-null members are taken as filenames.
.It Va CONVFMT
Conversion format when converting numbers
(default
.Qq Li %.6g ) .
.It Va ENVIRON
Array of environment variables; subscripts are names.
.It Va FILENAME
The name of the current input file.
.It Va FNR
Ordinal number of the current record in the current file.
.It Va FS
Regular expression used to separate fields (default whitespace);
also settable by option
.Fl F Ar fs .
.It Va NF
Number of fields in the current record.
.Va $NF
can be used to obtain the value of the last field in the current record.
.It Va NR
Ordinal number of the current record.
.It Va OFMT
Output format for numbers (default
.Qq Li %.6g ) .
.It Va OFS
Output field separator (default blank).
.It Va ORS
Output record separator (default newline).
.It Va RLENGTH
The length of the string matched by the
.Fn match
function.
.It Va RS
Input record separator (default newline).
If empty, blank lines separate records.
If more than one character long,
.Va RS
is treated as a regular expression, and records are
separated by text matching the expression.
.It Va RSTART
The starting position of the string matched by the
.Fn match
function.
.It Va SUBSEP
Separates multiple subscripts (default 034).
.El
.Sh FUNCTIONS
The awk language has a variety of built-in functions:
arithmetic, string, input/output, general, and bit-operation.
.Pp
Functions may be defined (at the position of a pattern-action statement)
thusly:
.Pp
.Dl function foo(a, b, c) { ...; return x }
.Pp
Parameters are passed by value if scalar, and by reference if array name;
functions may be called recursively.
Parameters are local to the function; all other variables are global.
Thus local variables may be created by providing excess parameters in
the function definition.
.Ss Arithmetic Functions
.Bl -tag -width "atan2(y, x)"
.It Fn atan2 y x
Return the arctangent of
.Fa y Ns / Ns Fa x
in radians.
.It Fn cos x
Return the cosine of
.Fa x ,
where
.Fa x
is in radians.
.It Fn exp x
Return the exponential of
.Fa x .
.It Fn int x
Return
.Fa x
truncated to an integer value.
.It Fn log x
Return the natural logarithm of
.Fa x .
.It Fn rand
Return a random number,
.Fa n ,
such that
.Sm off
.Pf 0 \*(Le Fa n No \*(Lt 1 .
.Sm on
Random numbers are non-deterministic unless a seed is explicitly set with
.Fn srand .
.It Fn sin x
Return the sine of
.Fa x ,
where
.Fa x
is in radians.
.It Fn sqrt x
Return the square root of
.Fa x .
.It Fn srand expr
Sets seed for
.Fn rand
to
.Fa expr
and returns the previous seed.
If
.Fa expr
is omitted,
.Fn rand
will return non-deterministic random numbers.
.El
.Ss String Functions
.Bl -tag -width "split(s, a, fs)"
.It Fn gensub r s h [t]
Search the target string
.Ar t
for matches of the regular expression
.Ar r .
If
.Ar h
is a string beginning with
.Ic g
or
.Ic G ,
then replace all matches of
.Ar r
with
.Ar s .
Otherwise,
.Ar h
is a number indicating which match of
.Ar r
to replace.
If no
.Ar t
is supplied,
.Va $0
is used instead.
.\"Within the replacement text
.\".Ar s ,
.\"the sequence
.\".Ar \en ,
.\"where
.\".Ar n
.\"is a digit from 1 to 9, may be used to indicate just the text that
.\"matched the
.\".Ar n Ap th
.\"parenthesized subexpression.
.\"The sequence
.\".Ic \e0
.\"represents the entire text, as does the character
.\".Ic & .
Unlike
.Fn sub
and
.Fn gsub ,
the modified string is returned as the result of the function,
and the original target is
.Em not
changed.
Note that
.Ar \en
sequences within the replacement string
.Ar s ,
as supported by GNU
.Nm ,
are
.Em not
supported at this time.
.It Fn gsub r t s
The same as
.Fn sub
except that all occurrences of the regular expression are replaced.
.Fn gsub
returns the number of replacements.
.It Fn index s t
The position in
.Fa s
where the string
.Fa t
occurs, or 0 if it does not.
.It Fn length s
The length of
.Fa s
taken as a string,
number of elements in an array for an array argument,
or length of
.Va $0
if no argument is given.
.It Fn match s r
The position in
.Fa s
where the regular expression
.Fa r
occurs, or 0 if it does not.
The variable
.Va RSTART
is set to the starting position of the matched string
.Pq which is the same as the returned value
or zero if no match is found.
The variable
.Va RLENGTH
is set to the length of the matched string,
or \-1 if no match is found.
.It Fn split s a fs
Splits the string
.Fa s
into array elements
.Va a[1] , a[2] , ... , a[n]
and returns
.Va n .
The separation is done with the regular expression
.Ar fs
or with the field separator
.Va FS
if
.Ar fs
is not given.
An empty string as field separator splits the string
into one array element per character.
.It Fn sprintf fmt expr ...
The string resulting from formatting
.Fa expr , ...
according to the
.Xr printf 1
format
.Fa fmt .
.It Fn sub r t s
Substitutes
.Fa t
for the first occurrence of the regular expression
.Fa r
in the string
.Fa s .
If
.Fa s
is not given,
.Va $0
is used.
An ampersand
.Pq Sq &
in
.Fa t
is replaced in string
.Fa s
with regular expression
.Fa r .
A literal ampersand can be specified by preceding it with two backslashes
.Pq Sq \e\e .
A literal backslash can be specified by preceding it with another backslash
.Pq Sq \e\e .
.Fn sub
returns the number of replacements.
.It Fn substr s m n
Return at most the
.Fa n Ns -character
substring of
.Fa s
that begins at position
.Fa m
counted from 1.
If
.Fa n
is omitted, or if
.Fa n
specifies more characters than are left in the string,
the length of the substring is limited by the length of
.Fa s .
.It Fn tolower str
Returns a copy of
.Fa str
with all upper-case characters translated to their
corresponding lower-case equivalents.
.It Fn toupper str
Returns a copy of
.Fa str
with all lower-case characters translated to their
corresponding upper-case equivalents.
.El
.Ss Time Functions
This version of
.Nm
provides the following functions for obtaining and formatting time
stamps.
.Bl -tag -width indent
.It Fn mktime datespec
Converts
.Fa datespec
into a timestamp in the same form as a value returned by
.Fn systime .
The
.Fa datespec
is a string composed of six or seven numbers separated by whitespace:
.Bd -literal -offset indent
YYYY MM DD HH MM SS [DST]
.Ed
.Pp
The fields in
.Fa datespec
are as follows:
.Bl -tag -width "YYYY"
.It YYYY
Year: a four-digit year, including the century.
.It MM
Month: a number from 1 to 12.
.It DD
Day: a number from 1 to 31.
.It HH
Hour: a number from 0 to 23.
.It MM
Minute: a number from 0 to 59.
.It SS
Second: a number from 0 to 60 (permitting a leap second).
.It DST
Daylight Saving Time: a positive or zero value indicates that
DST is or is not in effect.
If DST is not specified, or is negative,
.Fn mktime
will attempt to determine the correct value.
.El
.It Fn strftime "[format [, timestamp]]"
Formats
.Ar timestamp
according to the string
.Ar format .
The format string may contain any of the conversion specifications described
in the
.Xr strftime 3
manual page, as well as any arbitrary text.
The
.Ar timestamp
must be in the same form as a value returned by
.Fn mktime
and
.Fn systime .
If
.Ar timestamp
is not specified, the current time is used.
If
.Ar format
is not specified, a default format equivalent to the output of
.Xr date 1
is used.
.It Fn systime
Returns the value of time in seconds since 0 hours, 0 minutes,
0 seconds, January 1, 1970, Coordinated Universal Time (UTC).
.El
.Ss Input/Output and General Functions
.Bl -tag -width "getline [var] < file"
.It Fn close expr
Closes the file or pipe
.Fa expr .
.Fa expr
should match the string that was used to open the file or pipe.
.It Ar cmd | Ic getline Op Va var
Read a record of input from a stream piped from the output of
.Ar cmd .
If
.Va var
is omitted, the variables
.Va $0
and
.Va NF
are set.
Otherwise
.Va var
is set.
If the stream is not open, it is opened.
As long as the stream remains open, subsequent calls
will read subsequent records from the stream.
The stream remains open until explicitly closed with a call to
.Fn close .
.Ic getline
returns 1 for a successful input, 0 for end of file, and \-1 for an error.
.It Fn fflush [expr]
Flushes any buffered output for the file or pipe
.Fa expr ,
or all open files or pipes if
.Fa expr
is omitted.
.Fa expr
should match the string that was used to open the file or pipe.
.It Ic getline
Sets
.Va $0
to the next input record from the current input file.
This form of
.Ic getline
sets the variables
.Va NF ,
.Va NR ,
and
.Va FNR .
.Ic getline
returns 1 for a successful input, 0 for end of file, and \-1 for an error.
.It Ic getline Va var
Sets
.Va $0
to variable
.Va var .
This form of
.Ic getline
sets the variables
.Va NR
and
.Va FNR .
.Ic getline
returns 1 for a successful input, 0 for end of file, and \-1 for an error.
.It Xo
.Ic getline Op Va var
.Pf <\ \& Ar file
.Xc
Sets
.Va $0
to the next record from
.Ar file .
If
.Va var
is omitted, the variables
.Va $0
and
.Va NF
are set.
Otherwise
.Va var
is set.
If
.Ar file
is not open, it is opened.
As long as the stream remains open, subsequent calls will read subsequent
records from
.Ar file .
.Ar file
remains open until explicitly closed with a call to
.Fn close .
.It Fn system cmd
Executes
.Fa cmd
and returns its exit status.
This will be \-1 upon error,
.Ar cmd Ns 's
exit status upon a normal exit,
256 +
.Em sig
if
.Fa cmd
was terminated by a signal, where
.Em sig
is the number of the signal,
or 512 +
.Em sig
if there was a core dump.
.El
.Ss Bit-Operation Functions
.Bl -tag -width "lshift(a, b)"
.It Fn compl x
Returns the bitwise complement of integer argument x.
.It Fn and x y
Performs a bitwise AND on integer arguments x and y.
.It Fn or x y
Performs a bitwise OR on integer arguments x and y.
.It Fn xor x y
Performs a bitwise Exclusive-OR on integer arguments x and y.
.It Fn lshift x n
Returns integer argument x shifted by n bits to the left.
.It Fn rshift x n
Returns integer argument x shifted by n bits to the right.
.El
.Sh ENVIRONMENT
The following environment variables affect the execution of
.Nm :
.Bl -tag -width POSIXLY_CORRECT
.It Ev LC_CTYPE
The character encoding
.Xr locale 1 .
It decides which byte sequences form characters, which characters are
letters, and how letters are mapped from lower to upper case and vice versa.
If unset or set to
.Qq C ,
.Qq POSIX ,
or an unsupported value, each byte is treated as a character,
and non-ASCII bytes are not regarded as letters.
.It Ev POSIXLY_CORRECT
When set, behave in accordance with the standard, even when it conflicts
with historical behavior.
.El
.Sh EXIT STATUS
.Ex -std awk
.Pp
But note that the
.Ic exit
expression can modify the exit status.
.Sh EXAMPLES
Print lines longer than 72 characters:
.Pp
.Dl length($0) > 72
.Pp
Print first two fields in opposite order:
.Pp
.Dl { print $2, $1 }
.Pp
Same, with input fields separated by comma and/or spaces and tabs:
.Bd -literal -offset indent
BEGIN { FS = ",[ \et]*|[ \et]+" }
      { print $2, $1 }
.Ed
.Pp
Add up first column, print sum and average:
.Bd -literal -offset indent
{ s += $1 }
END { print "sum is", s, " average is", s/NR }
.Ed
.Pp
Print all lines between start/stop pairs:
.Pp
.Dl /start/, /stop/
.Pp
Simulate
.Xr echo 1 :
.Bd -literal -offset indent
BEGIN { # Simulate echo(1)
        for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
        printf "\en"
        exit }
.Ed
.Pp
Print an error message to standard error:
.Bd -literal -offset indent
{ print "error!" > "/dev/stderr" }
.Ed
.Sh UNUSUAL FLOATING-POINT VALUES
.Nm
was designed before IEEE 754 arithmetic defined Not-A-Number (NaN)
and Infinity values, which are supported by all modern floating-point
hardware.
.Pp
Because
.Nm
uses
.Xr strtod 3
and
.Xr atof 3
to convert string values to double-precision floating-point values,
modern C libraries also convert strings starting with
.Dv inf
and
.Dv nan
into infinity and NaN values respectively.
This led to strange results,
with something like this:
.Pp
.Li echo nancy | awk '{ print $1 + 0 }'
.Pp
printing
.Dv nan
instead of zero.
.Pp
.Nm
now follows GNU
.Nm ,
and prefilters string values before attempting
to convert them to numbers, as follows:
.Bl -tag -width Ds
.It Hexadecimal values
Hexadecimal values (allowed since C99) convert to zero, as they did
prior to C99.
.It NaN values
The two strings
.Dq +NAN
and
.Dq -NAN
(case independent) convert to NaN.
No others do.
(NaNs can have signs.)
.It Infinity values
The two strings
.Dq +INF
and
.Dq -INF
(case independent) convert to positive and negative infinity, respectively.
No others do.
.El
.Sh SEE ALSO
.Xr cut 1 ,
.Xr date 1 ,
.Xr grep 1 ,
.Xr lex 1 ,
.Xr printf 1 ,
.Xr sed 1 ,
.Xr strftime 3 ,
.Xr re_format 7 ,
.Xr script 7
.Rs
.\" 4.4BSD USD:16
.\".%R Computing Science Technical Report
.\".%N 68
.\".%D July 1978
.%A A. V. Aho
.%A P. J. Weinberger
.%A B. W. Kernighan
.%T AWK \(em A Pattern Scanning and Processing Language
.%J Software \(em Practice and Experience
.%V 9:4
.%P pp. 267-279
.%D April 1979
.Re
.Rs
.%A A. V. Aho
.%A B. W. Kernighan
.%A P. J. Weinberger
.%T The AWK Programming Language
.%I Addison-Wesley
.%D 2024
.%O ISBN 0-13-826972-6
.Re
.Sh STANDARDS
The
.Nm
utility is compliant with the
.St -p1003.1-2008
specification except that consecutive backslashes in the replacement
string argument for
.Fn sub
and
.Fn gsub
are not collapsed and a slash
.Pq Ql /
does not need to be escaped in a bracket expression.
Also, the behaviour of
.Fn rand
and
.Fn srand
has been changed to support non-deterministic random numbers.
.Pp
In
.Ev LC_CTYPE Ns Li =POSIX
mode, treating non-ASCII input bytes as non-letter characters rather
than as input encoding errors intentionally violates the specification.
.Pp
The flags
.Op Fl \&dV ,
.Op Fl -csv ,
and
.Op Fl safe ,
support for regular expressions in
.Va RS ,
as well as the functions
.Fn fflush ,
.Fn gensub ,
.Fn compl ,
.Fn and ,
.Fn or ,
.Fn xor ,
.Fn lshift ,
.Fn rshift ,
.Fn mktime ,
.Fn strftime
and
.Fn systime
are extensions to that specification.
.Sh HISTORY
An
.Nm
utility appeared in
.At v7 .
.Sh BUGS
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
.Li \&""
to it.
.Pp
The scope rules for variables in functions are a botch;
the syntax is worse.