AWK useful examples
Learn to apply AWK commands effectively through practical examples involving file data processing. Understand print functions, field variables, BEGIN and END blocks, built-in variables, and script creation for text analysis in Bash.
We'll cover the following...
The best way to learn AWK is probably looking at some examples in a given context, let us create file data.txt file which has the following content:
File content: data.txt
| Months | Water | Gas | Elec | Phone |
|---|---|---|---|---|
| Jan | 647 | 2851 | 3310 | 1200 |
| Feb | 287 | 3544 | 25500 | 1000 |
| Mar | 238 | 3212 | 21900 | 1710 |
| Apr | 468 | 6986 | 35000 | 2000 |
| May | 124 | 1097 | 96300 | 13000 |
Thanks to educative.io team for helping me to apriori upload this file (data.txt) to the Educative storage space. So, you should be good to go, just press the Run button!
Example 1. AWK print function
By default Awk prints every line from the file.
Action print with out any argument prints the whole line by default. So it prints all the lines of the file with out fail. Note that the actions need to be enclosed with in the braces.
Example 2. AWK print specific field
Example 3. AWK’s BEGIN and END Actions
Here, the actions specified in the BEGIN section executed before AWK starts reading the lines from the input and END actions will be performed after completing the reading and processing the lines from the input.
Example 4. AWK fields variable ($1, $2 and so on)
Let’s consider we want to find a total of al bills in all months in the data. We then create the following script:
Note that it’s a bash script that calls awk from inside and we have used FNR to detect the first row which we want to avoid in the sum calculation.
Example 5. AWK built-in variables
As mentioned earlier, the built-in variable $NF represents number of field, in this case last field (5):
Example 6. AWK fields comparison >
Let’s now find the months with water bills > 500:
Self-contained AWK scripts example
In Linux systems self-contained AWK scripts can be constructed using. For example, a script that prints the content of a given file may be built by creating a file named printfile.awk with the following content:
The -f flag tells AWK that the argument that follows is the file to read the AWK program from.