awk Command In Linux With 5 Examples

awk command in Linux platform allows users to manipulate with data and generate reports. It’s basically a scripting language or a command programming language which needs no compilation. Also, the users can use variables, numeric functions, string functions and logical operators as well with awk.

In compare to other well known programming languages, the awk is data-driven. It means that users need to define a set of actions to perform against the input text. It takes the data from users, process it, and shows the results to standard output.

The term awk is basically an abbreviation based on the name of its developers Aho, Weinberger, and Kernighan.

Syntax to use awk in Linux

awk options 'selection-criteria (action)' input-filename > output-filename

What are the functions of awk and how it can be useful?

Awk in Linux, allows users to do a number of tasks like scanning a file line by line, splitting each input line into fields, comparing input line or fields to pattern, and performing actions on matched lines.

This programming language can be useful for users to transform data files and produce formatted reports.

How awk in Linux works?

Technically, awk can be used for various purposes. In this article, we are using GNU implementation of awk, that is also known as gawk. In most of the instances, the awk is just a symbolic link to gawk.

About awk Records and fields

With the usage of awk, the users can process textual data files. The input data is actually divided into records and fields. So, when the awk runs, it works on one record at a time, until it reaches the end of input line. Record separator is used to separate different records, and the by default record separator is a newline character. In order to set a new record separator, the users can set the RS variable.

Available options to use with awk

-f program-file: Used for reading awk program source from provided file name despite the first command line argument
-F fs: Users can utilize fs for the input field separator

More built-in variables

NF: indicates the number of fiends in the record
NR: indicates the number of current record
FILENAME: indicates the name of input file which is being processed currently
FS: Field separator
RS: Record separator
OFS: Output field separator
ORS: Output record separator

Example of awk utility in Linux

Suppose a text file (sheet.txt) that includes following details, and we are using it as input file for all instances below:

mark teamleader salary 22000
john teamleader 25000
bella teamleader incentive 35000
mitesh teamleader salary 45000
henry executive incentive 15000
laura worker sales 20000

Case 1: awk is being used to print every line of data by default from specified file sheet.txt.

awk '{print}' sheet.txt

Output will be:

mark teamleader salary 22000
john teamleader 25000
bella teamleader incentive 35000
mitesh teamleader salary 45000
henry executive incentive 15000
laura worker sales 20000

Case 2: Printing the line that matches a specified pattern ‘teamleader

awk '/teamleader/ {print}' sheet.txt

Output will be:

mark teamleader salary 22000
john teamleader 25000
bella teamleader incentive 35000
mitesh teamleader salary 45000

Case 3: Splitting a line into Fields ($1 and $2)

awk '{print $1,$2}' sheet.txt

Output will be:

mark teamleader
john teamleader
bella teamleader
mitesh teamleader
henry executive
laura worker

Case 4: Displaying line number using NR built-in variable

awk '{print NR,$0}' sheet.txt

Output will be:

1 mark teamleader salary 22000
2 john teamleader 25000
3 bella teamleader incentive 35000
4 mitesh teamleader salary 45000
5 henry executive incentive 15000
6 laura worker sales 20000

Case 5: Displaying last field using NF built-in variable

awk '{print $NF}' sheet.txt

Output will be:

22000
25000
35000
45000
15000
20000

Conclusion: awk command in linux simply appears to be a very powerful tool which can be used to manipulate with text files. Hope this article helped you to learn its usage easily.