#!/bin/bash
# Write the sdtin to a datafile
cp /dev/stdin awk-data.csv
# Read a file awk-data.csv and print
awk -F, '{print;}' awk-data.csv
AWK Examples
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:
Months | Water | Gas | Elec | Phone |
Jan | 647 | 2851 | 3310 | 1200 |
Feb | 287 | 3544 | 2550 | 1000 |
Mar | 238 | 3212 | 21900 | 1710 |
Apr | 468 | 6986 | 35000 | 2000 |
May | 124 | 1097 | 96300 | 13000 |
The csv formatted data file awk-data.csv has already been uploaded to the system, you should be good to go, just press the Run button!
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.
AWK print
with specific field:
#!/bin/bash
# Write the sdtin to a datafile
cp /dev/stdin awk-data.csv
# Read a file awk-data.csv and print the col 1-2
awk -F, '{print $1,$2;}' awk-data.csv
Print with 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.
#!/bin/bash
# Write the sdtin to a datafile
cp /dev/stdin awk-data.csv
# Read a file awk-data.csv and print the col 1-3
# with BEGIN and END actions
awk -F, 'BEGIN {print "Period";} \
{print $1,"\t", $2,$3,$NF;} \
END{print "END\n--------------"; }' awk-data.csv
Complete example
Let's consider we want to find a total of al bills in all months in the data. We then create the following script:
#!/bin/bash
# Write the sdtin to a datafile
cp /dev/stdin awk-data.csv
echo -n "Field sum: Water + Gas + Electtricty + Phones = $"
awk -F "," '{
if(FNR == 1){
next;
}
Water=$2;
Gas=$3;
Electricity=$4;
Phones=$5;
fields_sum=Water + Gas + Electtricty + Phones;
total +=fields_sum;
} END { print total; }' awk-data.csv
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.
AWK built-in variables: As mentioned earlier, the built-in variable $NF
represents number of field, in this case last field (5):
#!/bin/bash
# Write the sdtin to a datafile
cp /dev/stdin awk-data.csv
awk -F, '{print $1,$NF;}' awk-data.csv
AWK fields comparison >
: Let's now find the months with water bills > 500
:
#!/bin/bash
# Write the sdtin to a datafile
cp /dev/stdin awk-data.csv
awk -F, '$2 > 500' awk-data.csv
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:
#!/bin/bash
# Write the sdtin to a datafile
cp /dev/stdin awk-data.csv
awk -f /usr/local/data/printfile.awk awk-data.csv