Table of Contents
In this tutorial, we will see about what is meant by awk print $2.
awk is interpreted programming language. It is very powerful programming language and used for text processing. Awk stands for the names of its authors “Aho, Weinberger, and Kernighan”.
awk print $2
As said before, awk can be used for text processing. awk treats tab or whitespace for file separator by default.
Awk actually uses some variables for each data field found.
- $0 for whole line
- $1 for first field
- $2 for second field
- $3 for third field
- $n for nth field
so, here awk print $2 represents second field in the file.
Let’s understand with the help of an example.
$ cat > sample.txt
This is sample file
Java is programming language.
This is Java tutorial
I know Java very well.
Let’s run the command now
$awk ‘{print $2}’ sample.txt
is
is
is
know
If you notice awk print $2
prints second word of each line.
If you use $3, it will print 3rd word of each line.
$awk ‘{print $3}’ sample.txt
sample
programming
Java
Java
Let’s use csv file now for demonstration
$ cat > countries.csv
#Country,Population,Captital
India,10000,Delhi
Nepal,2000,Kathmandu
China,20000,Beijing
Let’s print second column of csv file using awk print $2 command. We need to provide explicit delimeter over here.
$awk -F’,’ ‘{print $2}’ countries.csv
Population
10000
2000
20000
That’s all about awk print $2 command.