Table of Contents
In this tutorial, we will see about what is meant by awk print $1.
awk is interpreted programming language. It is very powerful and used for text processing.Awk stands for the names of its authors “Aho, Weinberger, and Kernighan”.
awk print $1
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 print $1 represents first 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 $1}’ sample.txt
This
Java
This
I
If you notice awk ‘print $1’ prints first 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 first column of csv file using awk print $1 command. We need to provide explicit delimeter over here.
$awk -F’,’ ‘{print $1}’ countries.csv
#Country
India
Nepal
China
That’s all about awk print $1 command.