AWK Command in Linux with Examples
AWK Command in Linux with Examples
Examples
LinuxOperating SystemOpen Source
If you're working with Linux, then you may have come across AWK command.
AWK is a powerful text processing tool that is used for a variety of tasks such as
filtering, manipulating and transforming data. In this article, we'll explore AWK
command in detail, including its syntax, functions, and examples of its use.
The pattern is enclosed in single quotes and specifies which lines of input file
should be processed. action is enclosed in curly braces and specifies what
should be done with those lines. Multiple patterns and actions can be combined
to create more complex scripts.
Learn Linux/Unix in-depth with real-world projects through our Linux/Unix
certification course. Enroll and become a certified expert to boost your
career.
This script will match every line in input file and print it to console.
One of most common uses of AWK is to extract specific columns from a file.
Here's how to extract first and third columns of a file −
awk '{ print $1, $3 }' input_file
This script will match every line in input file and print first and third columns to
console.
AWK can also be used to filter lines based on a condition. Here's how to print all
lines in a file that contain word "error" −
This script will match every line in input file that contains word "error" and print
it to console.
AWK can also be used to perform mathematical operations on data. Here's how
to sum values in second column of a file −
This script will add up all values in second column of input file and print total to
console.
This script will extract first three characters from first column of input file and
print them to console.
This script will read first file into memory and create an array with values in
second column. It will then read second file and print each line, followed by
corresponding value from first file.
To extract first column of a file using AWK command, use following syntax −
Here, $1 refers to first column of input file. print command is used to display
output.
Here, sum is a variable that stores sum of values in first column. NR is a built-in
variable that stores number of records (lines) processed by AWK command. END
pattern matches end of input file.
Here, $NF refers to last field of input file. print command is used to display
output.
Regular expressions are a powerful feature of AWK command that allows users
to search for patterns in data. Here's an example that demonstrates use of
regular expressions −
Here, /pattern/ is a regular expression that matches any line that contains
pattern.
Variables can be used in AWK command to store values that can be used in
actions. Here's an example that demonstrates use of variables −
$ awk '{ total += $1 } END { print "Total: ", total }' filename
The AWK command has several built-in variables that can be used to perform
various tasks. Here are some examples −
Control statements such as if-else and while loops can be used in AWK
command to perform conditional operations. Here's an example that
demonstrates use of if-else statements −
$ awk '{ if ($1 > 50) { print "Pass" } else { print "Fail" } }' filename
Here, if value in first column is greater than 50, output will be "Pass," otherwise
it will be "Fail."
Here, function square is defined and used to calculate square of value in first
column.
Conclusion
The AWK command is a powerful tool for text processing and data manipulation
in Linux. With its simple syntax, built-in functions, and ability to perform
complex tasks, AWK is an essential tool for any Linux user. By using examples in
this article, you should be able to get started with AWK and begin to unlock its
full potential.