What Is “awk ‘{print $1}'”?

The awk is a very powerful command or interpreted scripting language to process different text or string data. The awk is generally used to process command output or text or configuration files. The awk provides '{print $1}' command in order to print the first column for the specified file or output. In this tutorial, we examine this in detail.

Print First Column with “awk ‘{print $1}'”

The awk ‘{print $1}’ is generally used to print the first column of a file or output. The first column is generally decided with a well-known separator-like space. Every line from the start to the first space is interpreted as the first column and the characters can be printed like below. The sample file is like below.

İsmail Baydan
Ahmet Ali Baydan
Elif Ecrin Baydan
$ awk '{print $1}' names.txt
Print First Column with “awk ‘{print $1}'”

“awk ‘{print $1}'” CSV File

The awk command by default uses space or whitespace as a separator to number columns. But there are different separators where a comma is a popular separator for the CSV files. We can explicitly specify the command as a separator to print first column values in a file or command output. The -F option is used to specify a command as separator or delimeter.

İsmailBaydan,38,Turkey
AhmetAliBaydan,9,Turkey
ElifEcrinBaydan,12,Turkey
$ awk -F',' '{print $1}' names.txt
“awk ‘{print $1}'” CSV File

Print Second (Next of First) Column

In some cases, we may need to print the second or next of the first column. We can use the “awk ‘{print $2}'” command to print the second column like below.

İsmail Baydan
Ahmet Ali Baydan
Elif Ecrin Baydan
$ awk '{print $2}' names.txt

Leave a Comment