awk
Command in Linux
Summary
The awk
command is a powerful text processing tool in Linux used for pattern scanning and processing. It reads input files line by line, compares each line to a pattern, and performs actions based on the match. It's widely used for data extraction, report generation, and data transformation.
Introduction
awk
is a programming language and a command-line utility used for data extraction and reporting. It scans each line of a file, splits it into fields, and then compares the fields to specified patterns. When a match is found, awk
executes the corresponding actions, which can include printing, modifying, or processing the data. The name awk
comes from the initials of its creators: Alfred Aho, Peter Weinberger, and Brian Kernighan. It's a core utility in most Linux distributions.
Use case and Examples
Printing the second field of each line
This command prints the second field of each line in thedata.txt
file. Fields are separated by whitespace by default. Printing lines where the first field equals 'apple'
This command prints the entire line ($0
) from data.txt
where the first field ($1
) is equal to "apple". Printing the sum of the third field
This command calculates the sum of the third field of each line indata.txt
and prints the total at the end. Using a different field separator
This command prints the first and third fields from thedata.csv
file, using a comma (,
) as the field separator. Conditional printing
This command prints the first and second fields of lines where the second field is greater than 10.Commonly used flags
Flag | Description | Example |
---|---|---|
-F | Specifies the field separator. | awk -F',' '{print $1}' data.csv (Uses comma as field separator) |
-v | Assigns a value to a variable before the awk program starts. | awk -v threshold=10 '$1 > threshold {print $0}' data.txt (Sets a threshold variable) |
-f | Specifies a file that contains awk commands. | awk -f script.awk data.txt (Executes commands in script.awk on data.txt ) |
-m | Limit the amount of memory available to awk. (Some awk implementations only) | awk -m 1000000 '{print $1}' data.txt (Limit memory to 1MB) |
--version | Displays the version information for awk. | awk --version (Shows the awk version) |