-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawk.html
More file actions
5807 lines (4471 loc) · 205 KB
/
Copy pathawk.html
File metadata and controls
5807 lines (4471 loc) · 205 KB
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
<!DOCTYPE html><html><body><title>AWK Tutorial</title>
<h1>Awk Tutorial</h1>
<p>This tutorial takes you through AWK, one of the most prominent text-processing utility on GNU/Linux. It is very powerful and uses simple programming language. It can solve complex text processing tasks with a few lines of code. Starting with an overview of AWK, its environment, and workflow, the tutorial proceeds to explain the syntax, variables, operators, arrays, loops, and functions used in AWK. It also covers topics such as output redirection and pretty printing.</p>
<h1>Audience</h1>
<p>This tutorial will be useful for software developers, system administrators, or any enthusiastic reader inclined to learn how to do text processing and data extraction in Unix-like environment.</p>
<h1>Prerequisites</h1>
<p>You must have a basic understanding of GNU/Linux operating system and shell scripting.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>AWK Overview</title>
<h1>AWK - Overview</h1>
<p>AWK is an interpreted programming language. It is very powerful and specially designed for text processing. Its name is derived from the family names of its authors − <b>Alfred Aho, Peter Weinberger, and Brian Kernighan.</b></p>
<p>The version of AWK that GNU/Linux distributes is written and maintained by the Free Software Foundation (FSF); it is often referred to as <b>GNU AWK.</b></p>
<h2>Types of AWK</h2>
<p>Following are the variants of AWK −</p>
<p><b>AWK</b> − Original AWK from AT & T Laboratory.</p>
<p><b>NAWK</b> − Newer and improved version of AWK from AT & T Laboratory.</p>
<p><b>GAWK</b> − It is GNU AWK. All GNU/Linux distributions ship GAWK. It is fully compatible with AWK and NAWK.</p>
<h2>Typical Uses of AWK</h2>
<p>Myriad of tasks can be done with AWK. Listed below are just a few of them −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>AWK Environment</title>
<h1>AWK - Environment</h1>
<p>This chapter describes how to set up the AWK environment on your GNU/Linux system.</p>
<h2>Installation Using Package Manager</h2>
<p>Generally, AWK is available by default on most GNU/Linux distributions. You can use <b>which</b> command to check whether it is present on your system or not. In case you don’t have AWK, then install it on Debian based GNU/Linux using Advance Package Tool <b>(APT)</b> package manager as follows −</p>
<p>Similarly, to install AWK on RPM based GNU/Linux, use Yellowdog Updator Modifier <b>yum</b> package manager as follows −</p>
<p>After installation, ensure that AWK is accessible via command line.</p>
<p>On executing the above code, you get the following result −</p>
<h2>Installation from Source Code</h2>
<p>As GNU AWK is a part of the GNU project, its source code is available for free download. We have already seen how to install AWK using package manager. Let us now understand how to install AWK from its source code.</p>
<p>The following installation is applicable to any GNU/Linux software, and for most other freely-available programs as well. Here are the installation steps −</p>
<p><b>Step 1</b> − Download the source code from an authentic place. The command-line utility <b>wget</b> serves this purpose.</p>
<p><b>Step 2</b> − Decompress and extract the downloaded source code.</p>
<p><b>Step 3</b> − Change into the directory and run configure.</p>
<p><b>Step 4</b> − Upon successful completion, the <b>configure</b> generates Makefile. To compile the source code, issue a <b>make</b> command.</p>
<p><b>Step 5</b> − You can run the test suite to ensure the build is clean. This is an optional step.</p>
<p><b>Step 6</b> − Finally, install AWK. Make sure you have super-user privileges.</p>
<p>That is it! You have successfully compiled and installed AWK. Verify it by executing the <b>awk</b> command as follows −</p>
<p>On executing this code, you get the following result −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
[jeryy]$ sudo apt-get update
[jeryy]$ sudo apt-get install gawk
</pre>
<p>Similarly, to install AWK on RPM based GNU/Linux, use Yellowdog Updator Modifier <b>yum</b> package manager as follows −</p>
<pre class="prettyprint notranslate">
[root]# yum install gawk
</pre>
<p>After installation, ensure that AWK is accessible via command line.</p>
<pre class="prettyprint notranslate">
[jerry]$ which awk
</pre>
<p>On executing the above code, you get the following result −</p>
<pre class="result notranslate">
/usr/bin/awk
</pre>
<h2>Installation from Source Code</h2>
<p>As GNU AWK is a part of the GNU project, its source code is available for free download. We have already seen how to install AWK using package manager. Let us now understand how to install AWK from its source code.</p>
<p>The following installation is applicable to any GNU/Linux software, and for most other freely-available programs as well. Here are the installation steps −</p>
<p><b>Step 1</b> − Download the source code from an authentic place. The command-line utility <b>wget</b> serves this purpose.</p>
<pre class="prettyprint notranslate">
[jerry]$ wget http://ftp.gnu.org/gnu/gawk/gawk-4.1.1.tar.xz
</pre>
<p><b>Step 2</b> − Decompress and extract the downloaded source code.</p>
<pre class="prettyprint notranslate">
[jerry]$ tar xvf gawk-4.1.1.tar.xz
</pre>
<p><b>Step 3</b> − Change into the directory and run configure.</p>
<pre class="prettyprint notranslate">
[jerry]$ ./configure
</pre>
<p><b>Step 4</b> − Upon successful completion, the <b>configure</b> generates Makefile. To compile the source code, issue a <b>make</b> command.</p>
<pre class="prettyprint notranslate">
[jerry]$ make
</pre>
<p><b>Step 5</b> − You can run the test suite to ensure the build is clean. This is an optional step.</p>
<pre class="prettyprint notranslate">
[jerry]$ make check
</pre>
<p><b>Step 6</b> − Finally, install AWK. Make sure you have super-user privileges.</p>
<pre class="prettyprint notranslate">
[jerry]$ sudo make install
</pre>
<p>That is it! You have successfully compiled and installed AWK. Verify it by executing the <b>awk</b> command as follows −</p>
<pre class="prettyprint notranslate">
[jerry]$ which awk
</pre>
<p>On executing this code, you get the following result −</p>
<pre class="result notranslate">
/usr/bin/awk
</pre>
<title>AWK Workflow</title>
<h1>AWK - Workflow</h1>
<p>To become an expert AWK programmer, you need to know its internals. AWK follows a simple workflow − Read, Execute, and Repeat. The following diagram depicts the workflow of AWK −</p>
<h3>Read</h3>
<p>AWK reads a line from the input stream (file, pipe, or stdin) and stores it in memory.</p>
<h3>Execute</h3>
<p>All AWK commands are applied sequentially on the input. By default AWK execute commands on every line. We can restrict this by providing patterns.</p>
<h3>Repeat</h3>
<p>This process repeats until the file reaches its end.</p>
<h2>Program Structure</h2>
<p>Let us now understand the program structure of AWK.</p>
<h3>BEGIN block</h3>
<p>The syntax of the BEGIN block is as follows −</p>
<p><b>Syntax</b></p>
<p>The BEGIN block gets executed at program start-up. It executes only once. This is good place to initialize variables. BEGIN is an AWK keyword and hence it must be in upper-case. Please note that this block is optional.</p>
<h3>Body Block</h3>
<p>The syntax of the body block is as follows −</p>
<p><b>Syntax</b></p>
<p>The body block applies AWK commands on every input line. By default, AWK executes commands on every line. We can restrict this by providing patterns. Note that there are no keywords for the Body block.</p>
<h3>END Block</h3>
<p>The syntax of the END block is as follows −</p>
<p><b>Syntax</b></p>
<p>The END block executes at the end of the program. END is an AWK keyword and hence it must be in upper-case. Please note that this block is optional.</p>
<p>Let us create a file <b>marks.txt</b> which contains the serial number, name of the student, subject name, and number of marks obtained.</p>
<p>Let us now display the file contents with header by using AWK script.</p>
<p><b>Example</b></p>
<p>When this code is executed, it produces the following result −</p>
<p><b>Output</b></p>
<p>At the start, AWK prints the header from the BEGIN block. Then in the body block, it reads a line from a file and executes AWK's print command which just prints the contents on the standard output stream. This process repeats until file reaches the end.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
BEGIN {awk-commands}
</pre>
<p>The BEGIN block gets executed at program start-up. It executes only once. This is good place to initialize variables. BEGIN is an AWK keyword and hence it must be in upper-case. Please note that this block is optional.</p>
<h3>Body Block</h3>
<p>The syntax of the body block is as follows −</p>
<p><b>Syntax</b></p>
<pre class="result notranslate">
/pattern/ {awk-commands}
</pre>
<p>The body block applies AWK commands on every input line. By default, AWK executes commands on every line. We can restrict this by providing patterns. Note that there are no keywords for the Body block.</p>
<h3>END Block</h3>
<p>The syntax of the END block is as follows −</p>
<p><b>Syntax</b></p>
<pre class="result notranslate">
END {awk-commands}
</pre>
<p>The END block executes at the end of the program. END is an AWK keyword and hence it must be in upper-case. Please note that this block is optional.</p>
<p>Let us create a file <b>marks.txt</b> which contains the serial number, name of the student, subject name, and number of marks obtained.</p>
<pre class="result notranslate">
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
</pre>
<p>Let us now display the file contents with header by using AWK script.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate">
[jerry]$ awk 'BEGIN{printf "Sr No\tName\tSub\tMarks\n"} {print}' marks.txt
</pre>
<p>When this code is executed, it produces the following result −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
Sr No Name Sub Marks
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
</pre>
<title>AWK Basic Syntax</title>
<h1>AWK - Basic Syntax</h1>
<p>AWK is simple to use. We can provide AWK commands either directly from the command line or in the form of a text file containing AWK commands.</p>
<h2>AWK Command Line</h2>
<p>We can specify an AWK command within single quotes at command line as shown −</p>
<h3>Example</h3>
<p>Consider a text file <b>marks.txt</b> with the following content −</p>
<p>Let us display the complete content of the file using AWK as follows −</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h2>AWK Program File</h2>
<p>We can provide AWK commands in a script file as shown −</p>
<p>First, create a text file <b>command.awk</b> containing the AWK command as shown below −</p>
<p>Now we can instruct the AWK to read commands from the text file and perform the action. Here, we achieve the same result as shown in the above example.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h2>AWK Standard Options</h2>
<p>AWK supports the following standard options which can be provided from the command line.</p>
<h3>The -v option</h3>
<p>This option assigns a value to a variable. It allows assignment before the program execution. The following example describes the usage of the -v option.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>The --dump-variables[=file] option</h3>
<p>It prints a sorted list of global variables and their final values to file. The default file is <b>awkvars.out</b>.</p>
<p><b>Example</b></p>
<p>On executing the above code, you get the following result −</p>
<p><b>Output</b></p>
<h3>The --help option</h3>
<p>This option prints the help message on standard output.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>The --lint[=fatal] option</h3>
<p>This option enables checking of non-portable or dubious constructs. When an argument <b>fatal</b> is provided, it treats warning messages as errors. The following example demonstrates this −</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>The --posix option</h3>
<p>This option turns on strict POSIX compatibility, in which all common and gawk-specific extensions are disabled.</p>
<h3>The --profile[=file] option</h3>
<p>This option generates a pretty-printed version of the program in file. Default file is <b>awkprof.out</b>. Below simple example illustrates this −</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>The --traditional option</h3>
<p>This option disables all gawk-specific extensions.</p>
<h3>The --version option</h3>
<p>This option displays the version information of the AWK program.</p>
<p><b>Example</b></p>
<p>When this code is executed, it produces the following result −</p>
<p><b>Output</b></p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
awk [options] file ...
</pre>
<h3>Example</h3>
<p>Consider a text file <b>marks.txt</b> with the following content −</p>
<pre class="result notranslate">
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
</pre>
<p>Let us display the complete content of the file using AWK as follows −</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate">
[jerry]$ awk '{print}' marks.txt
</pre>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
</pre>
<h2>AWK Program File</h2>
<p>We can provide AWK commands in a script file as shown −</p>
<pre class="result notranslate">
awk [options] -f file ....
</pre>
<p>First, create a text file <b>command.awk</b> containing the AWK command as shown below −</p>
<pre class="result notranslate">
{print}
</pre>
<p>Now we can instruct the AWK to read commands from the text file and perform the action. Here, we achieve the same result as shown in the above example.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate">
[jerry]$ awk -f command.awk marks.txt
</pre>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
</pre>
<h2>AWK Standard Options</h2>
<p>AWK supports the following standard options which can be provided from the command line.</p>
<h3>The -v option</h3>
<p>This option assigns a value to a variable. It allows assignment before the program execution. The following example describes the usage of the -v option.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate">
[jerry]$ awk -v name=Jerry 'BEGIN{printf "Name = %s\n", name}'
</pre>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
Name = Jerry
</pre>
<h3>The --dump-variables[=file] option</h3>
<p>It prints a sorted list of global variables and their final values to file. The default file is <b>awkvars.out</b>.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate">
[jerry]$ awk --dump-variables ''
[jerry]$ cat awkvars.out
</pre>
<p>On executing the above code, you get the following result −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
ARGC: 1
ARGIND: 0
ARGV: array, 1 elements
BINMODE: 0
CONVFMT: "%.6g"
ERRNO: ""
FIELDWIDTHS: ""
FILENAME: ""
FNR: 0
FPAT: "[^[:space:]]+"
FS: " "
IGNORECASE: 0
LINT: 0
NF: 0
NR: 0
OFMT: "%.6g"
OFS: " "
ORS: "\n"
RLENGTH: 0
RS: "\n"
RSTART: 0
RT: ""
SUBSEP: "\034"
TEXTDOMAIN: "messages"
</pre>
<h3>The --help option</h3>
<p>This option prints the help message on standard output.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate">
[jerry]$ awk --help
</pre>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options : GNU long options: (standard)
-f progfile --file=progfile
-F fs --field-separator=fs
-v var=val --assign=var=val
Short options : GNU long options: (extensions)
-b --characters-as-bytes
-c --traditional
-C --copyright
-d[file] --dump-variables[=file]
-e 'program-text' --source='program-text'
-E file --exec=file
-g --gen-pot
-h --help
-L [fatal] --lint[=fatal]
-n --non-decimal-data
-N --use-lc-numeric
-O --optimize
-p[file] --profile[=file]
-P --posix
-r --re-interval
-S --sandbox
-t --lint-old
-V --version
</pre>
<h3>The --lint[=fatal] option</h3>
<p>This option enables checking of non-portable or dubious constructs. When an argument <b>fatal</b> is provided, it treats warning messages as errors. The following example demonstrates this −</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate">
[jerry]$ awk --lint '' /bin/ls
</pre>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
awk: cmd. line:1: warning: empty program text on command line
awk: cmd. line:1: warning: source file does not end in newline
awk: warning: no program text at all!
</pre>
<h3>The --posix option</h3>
<p>This option turns on strict POSIX compatibility, in which all common and gawk-specific extensions are disabled.</p>
<h3>The --profile[=file] option</h3>
<p>This option generates a pretty-printed version of the program in file. Default file is <b>awkprof.out</b>. Below simple example illustrates this −</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate">
[jerry]$ awk --profile 'BEGIN{printf"---|Header|--\n"} {print}
END{printf"---|Footer|---\n"}' marks.txt > /dev/null
[jerry]$ cat awkprof.out
</pre>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
# gawk profile, created Sun Oct 26 19:50:48 2014
# BEGIN block(s)
BEGIN {
printf "---|Header|--\n"
}
# Rule(s) {
print $0
}
# END block(s)
END {
printf "---|Footer|---\n"
}
</pre>
<h3>The --traditional option</h3>
<p>This option disables all gawk-specific extensions.</p>
<h3>The --version option</h3>
<p>This option displays the version information of the AWK program.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate">
[jerry]$ awk --version
</pre>
<p>When this code is executed, it produces the following result −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
GNU Awk 4.0.1
Copyright (C) 1989, 1991-2012 Free Software Foundation.
</pre>
<title>AWK Basic Examples</title>
<h1>AWK - Basic Examples</h1>
<p>This chapter describes several useful AWK commands and their appropriate examples. Consider a text file <b>marks.txt</b> to be processed with the following content −</p>
<h2>Printing Column or Field</h2>
<p>You can instruct AWK to print only certain columns from the input field. The following example demonstrates this −</p>
<h3>Example</h3>
<p>On executing this code, you get the following result −</p>
<h3>Output</h3>
<p>In the file <b>marks.txt</b>, the third column contains the subject name and the fourth column contains the marks obtained in a particular subject. Let us print these two columns using AWK print command. In the above example, <b>$3 and $4</b> represent the third and the fourth fields respectively from the input record.</p>
<h2>Printing All Lines</h2>
<p>By default, AWK prints all the lines that match pattern.</p>
<h3>Example</h3>
<p>On executing this code, you get the following result −</p>
<h3>Output</h3>
<p>In the above example, we are searching form pattern <b>a</b>. When a pattern match succeeds, it executes a command from the body block. In the absence of a body block − default action is taken which is print the record. Hence, the following command produces the same result −</p>
<h3>Example</h3>
<h2>Printing Columns by Pattern</h2>
<p>When a pattern match succeeds, AWK prints the entire record by default. But you can instruct AWK to print only certain fields. For instance, the following example prints the third and fourth field when a pattern match succeeds.</p>
<h3>Example</h3>
<p>On executing this code, you get the following result −</p>
<h3>Output</h3>
<h2>Printing Column in Any Order</h2>
<p>You can print columns in any order. For instance, the following example prints the fourth column followed by the third column.</p>
<h3>Example</h3>
<p>On executing the above code, you get the following result −</p>
<h3>Output</h3>
<h2>Counting and Printing Matched Pattern</h2>
<p>Let us see an example where you can count and print the number of lines for which a pattern match succeeded.</p>
<h3>Example</h3>
<p>On executing this code, you get the following result −</p>
<h3>Output</h3>
<p>In this example, we increment the value of counter when a pattern match succeeds and we print this value in the END block. Note that unlike other programming languages, there is no need to declare a variable before using it.</p>
<h2>Printing Lines with More than 18 Characters</h2>
<p>Let us print only those lines that contain more than 18 characters.</p>
<h3>Example</h3>
<p>On executing this code, you get the following result −</p>
<h3>Output</h3>
<p>AWK provides a built-in <b>length</b> function that returns the length of the string. <b>$0</b> variable stores the entire line and in the absence of a body block, default action is taken, i.e., the print action. Hence, if a line has more than 18 characters, then the comparison results true and the line gets printed.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
</pre>
<h2>Printing Column or Field</h2>
<p>You can instruct AWK to print only certain columns from the input field. The following example demonstrates this −</p>
<h3>Example</h3>
<pre class="prettyprint notranslate">
[jerry]$ awk '{print $3 "\t" $4}' marks.txt
</pre>
<p>On executing this code, you get the following result −</p>
<h3>Output</h3>
<pre class="result notranslate">
Physics 80
Maths 90
Biology 87
English 85
History 89
</pre>
<p>In the file <b>marks.txt</b>, the third column contains the subject name and the fourth column contains the marks obtained in a particular subject. Let us print these two columns using AWK print command. In the above example, <b>$3 and $4</b> represent the third and the fourth fields respectively from the input record.</p>
<h2>Printing All Lines</h2>
<p>By default, AWK prints all the lines that match pattern.</p>
<h3>Example</h3>
<pre class="prettyprint notranslate">
[jerry]$ awk '/a/ {print $0}' marks.txt
</pre>
<p>On executing this code, you get the following result −</p>
<h3>Output</h3>
<pre class="result notranslate">
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
</pre>
<p>In the above example, we are searching form pattern <b>a</b>. When a pattern match succeeds, it executes a command from the body block. In the absence of a body block − default action is taken which is print the record. Hence, the following command produces the same result −</p>
<h3>Example</h3>
<pre class="prettyprint notranslate">
[jerry]$ awk '/a/' marks.txt
</pre>
<h2>Printing Columns by Pattern</h2>
<p>When a pattern match succeeds, AWK prints the entire record by default. But you can instruct AWK to print only certain fields. For instance, the following example prints the third and fourth field when a pattern match succeeds.</p>
<h3>Example</h3>
<pre class="prettyprint notranslate">
[jerry]$ awk '/a/ {print $3 "\t" $4}' marks.txt
</pre>
<p>On executing this code, you get the following result −</p>
<h3>Output</h3>
<pre class="result notranslate">
Maths 90
Biology 87
English 85
History 89
</pre>
<h2>Printing Column in Any Order</h2>
<p>You can print columns in any order. For instance, the following example prints the fourth column followed by the third column.</p>
<h3>Example</h3>
<pre class="prettyprint notranslate">
[jerry]$ awk '/a/ {print $4 "\t" $3}' marks.txt
</pre>
<p>On executing the above code, you get the following result −</p>
<h3>Output</h3>
<pre class="result notranslate">
90 Maths
87 Biology
85 English
89 History
</pre>
<h2>Counting and Printing Matched Pattern</h2>
<p>Let us see an example where you can count and print the number of lines for which a pattern match succeeded.</p>
<h3>Example</h3>
<pre class="prettyprint notranslate">
[jerry]$ awk '/a/{++cnt} END {print "Count = ", cnt}' marks.txt
</pre>
<p>On executing this code, you get the following result −</p>
<h3>Output</h3>
<pre class="result notranslate">
Count = 4
</pre>
<p>In this example, we increment the value of counter when a pattern match succeeds and we print this value in the END block. Note that unlike other programming languages, there is no need to declare a variable before using it.</p>
<h2>Printing Lines with More than 18 Characters</h2>
<p>Let us print only those lines that contain more than 18 characters.</p>
<h3>Example</h3>
<pre class="prettyprint notranslate">
[jerry]$ awk 'length($0) > 18' marks.txt
</pre>
<p>On executing this code, you get the following result −</p>
<h3>Output</h3>
<pre class="result notranslate">
3) Shyam Biology 87
4) Kedar English 85
</pre>
<title>AWK Built-in Variables</title>
<h1>AWK - Built-in Variables</h1>
<p>AWK provides several built-in variables. They play an important role while writing AWK scripts. This chapter demonstrates the usage of built-in variables.</p>
<h2>Standard AWK variables</h2>
<p>The standard AWK variables are discussed below.</p>
<h3>ARGC</h3>
<p>It implies the number of arguments provided at the command line.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<p>But why AWK shows 5 when you passed only 4 arguments? Just check the following example to clear your doubt.</p>
<h3>ARGV</h3>
<p>It is an array that stores the command-line arguments. The array's valid index ranges from 0 to ARGC-1.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>CONVFMT</h3>
<p>It represents the conversion format for numbers. Its default value is <b>%.6g</b>.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>ENVIRON</h3>
<p>It is an associative array of environment variables.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<p>To find names of other environment variables, use <b>env</b> command.</p>
<h3>FILENAME</h3>
<p>It represents the current file name.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<p>Please note that FILENAME is undefined in the BEGIN block.</p>
<h3>FS</h3>
<p>It represents the (input) field separator and its default value is space. You can also change this by using <b>-F</b> command line option.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>NF</h3>
<p>It represents the number of fields in the current record. For instance, the following example prints only those lines that contain more than two fields.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>NR</h3>
<p>It represents the number of the current record. For instance, the following example prints the record if the current record contains less than three fields.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>FNR</h3>
<p>It is similar to NR, but relative to the current file. It is useful when AWK is operating on multiple files. Value of FNR resets with new file.</p>
<h3>OFMT</h3>
<p>It represents the output format number and its default value is <b>%.6g</b>.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>OFS</h3>
<p>It represents the output field separator and its default value is space.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>ORS</h3>
<p>It represents the output record separator and its default value is newline.</p>
<p><b>Example</b></p>
<p>On executing the above code, you get the following result −</p>
<p><b>Output</b></p>
<h3>RLENGTH</h3>
<p>It represents the length of the string matched by <b>match</b> function. AWK's match function searches for a given string in the input-string.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>RS</h3>
<p>It represents (input) record separator and its default value is newline.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>RSTART</h3>
<p>It represents the first position in the string matched by <b>match</b> function.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>SUBSEP</h3>
<p>It represents the separator character for array subscripts and its default value is <b>\034</b>.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>$0</h3>
<p>It represents the entire input record.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>$n</h3>
<p>It represents the n<sup>th</sup> field in the current record where the fields are separated by FS.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h2>GNU AWK Specific Variables</h2>
<p>GNU AWK specific variables are as follows −</p>
<h3>ARGIND</h3>
<p>It represents the index in ARGV of the current file being processed.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>BINMODE</h3>
<p>It is used to specify binary mode for all file I/O on non-POSIX systems. Numeric values of 1, 2, or 3 specify that input files, output files, or all files, respectively, should use binary I/O. String values of <b>r</b> or <b>w</b> specify that input files or output files, respectively, should use binary I/O. String values of <b>rw</b> or <b>wr</b> specify that all files should use binary I/O.</p>
<h3>ERRNO</h3>
<p>A string indicates an error when a redirection fails for <b>getline</b> or if <b>close</b> call fails.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>FIELDWIDTHS</h3>
<p>A space separated list of field widths variable is set, GAWK parses the input into fields of fixed width, instead of using the value of the FS variable as the field separator.</p>
<h3>IGNORECASE</h3>
<p>When this variable is set, GAWK becomes case-insensitive. The following example demonstrates this −</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>LINT</h3>
<p>It provides dynamic control of the <b>--lint</b> option from the GAWK program. When this variable is set, GAWK prints lint warnings. When assigned the string value fatal, lint warnings become fatal errors, exactly like <b>--lint=fatal</b>.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>PROCINFO</h3>
<p>This is an associative array containing information about the process, such as real and effective UID numbers, process ID number, and so on.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<h3>TEXTDOMAIN</h3>
<p>It represents the text domain of the AWK program. It is used to find the localized translations for the program's strings.</p>
<p><b>Example</b></p>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<p>The above output shows English text due to <b>en_IN</b> locale</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
[jerry]$ awk 'BEGIN {print "Arguments =", ARGC}' One Two Three Four
</pre>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
Arguments = 5
</pre>
<p>But why AWK shows 5 when you passed only 4 arguments? Just check the following example to clear your doubt.</p>
<h3>ARGV</h3>
<p>It is an array that stores the command-line arguments. The array's valid index ranges from 0 to ARGC-1.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate">
[jerry]$ awk 'BEGIN {
for (i = 0; i < ARGC - 1; ++i) {
printf "ARGV[%d] = %s\n", i, ARGV[i]
}
}' one two three four
</pre>
<p>On executing this code, you get the following result −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
ARGV[0] = awk
ARGV[1] = one
ARGV[2] = two
ARGV[3] = three