AWK Basic Tutorial With Examples
AWK Basic Tutorial With Examples
AWK Basic Tutorial With Examples
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 #