AWK examples

Last updated on August 6th, 2022 at 05:08 pm

AWK is a command line option available in Linux / Unix based operating systems which is mainly used to file processing. Simple awk commands can be run in the command line itself. More complex programs can be written to an awk script file for processing.

Here you will understand,

  • How to print different columns in a text file using AWK
  • How to use if condition to print specific rows using AWK
  • Using -F option in AWK

Let us say I have a file named myfile.txt which has the following contents like Country and UserName in the 2nd and 3rd column along with the Index starting from 1 to 8

$ cat myfile.txt
1 USA David
2 India John
3 Australia Carlos
4 USA Brent
5 Brazil Luciano
6 USA Kirk
7 Australia Greg
8 India Arun

Print only Country from the file without using -F field separator?

AWK by default takes space as the field separator. So you don’t have to basically mention -F in the command below. $2 represents the second column and here it is the Country list.

$ awk '{print $2}' myfile.txt
USA
India
Australia
USA
Brazil
USA
Australia
India

Print only Country from the file using -F field separator [Optional in this case] ?

You can see that output is the same if we use -F‘ ‘ in the command to print column number 2.

$ awk -F' ' '{print $2}' myfile.txt
USA
India
Australia
USA
Brazil
USA
Australia
India

Print Country and username from the file ?

$ awk -F' ' '{print $2,$3}' myfile.txt
USA David
India John
Australia Carlos
USA Brent
Brazil Luciano
USA Kirk
Australia Greg
India Arun

Print country name and username separated by a pound [#] sign ?

$ awk -F' ' '{print $2,"#",$3}' myfile.txt
USA # David
India # John
Australia # Carlos
USA # Brent
Brazil # Luciano
USA # Kirk
Australia # Greg
India # Arun

How can I use if condition in AWK ?

Here I am printing all rows that doesn’t contain USA as the Country.

$ awk '{if($2!~"USA") print $2,$3}' myfile.txt
India John
Australia Carlos
Brazil Luciano
Australia Greg
India Arun

Print rows that contains USA as the country?

$ awk '{if($2=="USA") print $2,$3}' myfile.txt
USA David
USA Brent
USA Kirk
$

How to define variables in AWK?

You can define a variable in AWK by using -v switch. Here awk will assign the value mistonline.in to the variable name, before execution of the program begins

$ awk -v name=mistonline.in 'BEGIN { print name}'
mistonline.in

How to define arrays in AWK ?

Defining arrays is simple. As mentioned in AWK documentation “In awk, it isn’t necessary to specify the size of an array before starting to use it. Additionally, any number or string, not just consecutive integers, may be used as an array index.”

Create a file hello_array.awk and save the content below. Make sure your interpreter is in the same path as defined ( /usr/bin/awk ) otherwise change it accordingly.

#!/usr/bin/awk -f
BEGIN {
    myarray[0] = "alpha";
    myarray[1] = "beta";
    myarray[2] = "gamma";
    for (i in myarray)
        print "Index:", i, "with value:", myarray[i];

}

Now run the file using this command, use -f to provide the filename as a argument.

$awk -f hello_array.awk
Index: 0 with value: alpha
Index: 1 with value: beta
Index: 2 with value: gamma

Leave a Reply

Your email address will not be published. Required fields are marked *