Using AWK Scripts

In our last article over AWK "Getting started with AWK" see introduced our self's with AWK, now lets try to see how we can create AWK scripts and use them.

Passing your scripts to awk as a command line argument can be very handy for small one-liners, but when it comes to complex, multi-line programs, you'll definitely want to compose your script in an external file. Awk can then be told to source this script file by passing it the -f option.

See example: 
awk -f script.awk file.txt
Where the content of my awk script is:
BEGIN {
        FS=":"
}
{ print $1 }
And my file.txt:
1: bla 23
2: bla 24
3: bla 25
Putting your scripts in their own text files also allows you to take advantage of additional awk features. For example, this multi-line script does the same thing as one of our earlier one-liners, printing out the first field of each line in file.txt. More complex awk scripts can be written but more about this on the next article .