The awk
command is a powerful text processing tool in Unix-like operating systems. It is primarily used for pattern scanning and processing, allowing users to extract and manipulate data from files or input streams based on specified patterns.
The basic syntax of the awk
command is as follows:
awk [options] 'pattern { action }' file
-F
: Specifies the field separator (default is whitespace).-v
: Allows you to assign a value to a variable before executing the program.-f
: Allows you to specify a file containing awk
commands.-e
: Allows you to specify the awk
program as a command line argument.To print the first and third columns from a file named data.txt
, you can use:
awk '{print $1, $3}' data.txt
If your data is comma-separated, you can specify the field separator with the -F
option:
awk -F, '{print $1, $2}' data.csv
To print lines that contain the word “error” from a log file:
awk '/error/' log.txt
To calculate the sum of values in the second column of a file:
awk '{sum += $2} END {print sum}' data.txt
You can use the -v
option to set a variable and use it in your awk
command:
awk -v threshold=100 '$2 > threshold {print $1}' data.txt
awk
commands to avoid shell interpretation issues.BEGIN
and END
blocks to perform actions before processing the input or after finishing it, respectively.awk
commands with sample data to ensure they work as expected before applying them to larger datasets.