Skip to content

Linux awk command

The Linux awk command is a versatile tool for processing and manipulating text data in a straightforward and efficient manner. By using patterns and actions, awk can search for specific content, extract data, perform calculations, and generate reports. With its ability to work with fields, records, variables, and functions, awk is well-suited for a wide range of text processing tasks. Whether you need to reformat log files, extract specific information from a dataset, or automate data processing tasks, awk is a valuable addition to any command-line toolkit.

awk Syntax:

Terminal window
awk [options] 'pattern { action }' file

Options:

OptionDescription
-FSpecifies the field separator.
-vAssigns a value to a variable.
-fSpecifies a file that contains the AWK script.
-FSpecifies the field separator.
-rEnables extended regular expressions.

Parameters:

ParameterDescription
’pattern { action }‘Specifies the pattern and corresponding action to be performed.
fileSpecifies the input file.

awk Usage:

Terminal window
awk '{print $1, $3}' file.txt

This command will print the first and third columns from the file.txt.

Search for a Specific Value in a File

Terminal window
awk '/searchterm/' file.txt

Searches for the term “searchterm” in file.txt and prints lines containing it.

Calculate Total Value of a Column

Terminal window
awk '{sum+=$1} END {print sum}' file.txt

Calculates the total sum of values in the first column of file.txt.

Terminal window
awk 'length($0) > 50' file.txt

Prints lines in file.txt that are longer than 50 characters.

How do I use awk in Linux?

To use the awk command in Linux, execute the following command:

Terminal window
awk '{print $1}' file.txt

What is the purpose of awk ‘{print $1}’ command?

The command awk '{print $1}' is used to print the first field of each line in the input file. Using $1 as the field reference specifies the first field, so this command will print the first column or word in each line.

How can I specify a delimiter for awk?

You can specify a delimiter for awk using the -F option followed by the delimiter character. For example, to use a comma as the delimiter, you would run:

Terminal window
awk -F',' '{print $1}' file.csv

How can I perform calculations with awk?

To perform calculations with awk, you can use arithmetic operators like +, -, *, / within the action part. For example, you can add the values in the second column and print the result:

Terminal window
awk '{sum+=$2} END {print sum}' file.txt

How do I filter data with awk?

To filter data with awk, you can specify a condition in the pattern part. Only lines that match the condition will have the specified action performed. For example, to print lines where the first column is greater than 10:

Terminal window
awk '$1 > 10 {print}' file.txt

How can I use awk to format output?

You can use the printf function within awk to format and print output in a specific way. For instance, to print the first two columns with a specific format:

Terminal window
awk '{printf "Name: %-10s Age: %d\n", $1, $2}' file.txt

How can I read input from a pipe with awk?

You can read input from a pipe with awk by using a dash - as the filename. For example, to process output from another command using awk:

Terminal window
ls -l | awk '{print $9}'

Applications of the awk Command

  • Processing and analyzing text files
  • Generating reports and summaries
  • Searching and replacing text
  • Extracting specific data fields
  • Formatting data for further processing
  • System administration tasks
  • Text manipulation and transformation