Awk Command Examples
Awk is a powerful text-processing utility that scans files line by line and performs actions based on patterns. It's widely used in shell scripting for data extraction, transformation, and reporting. Below are several practical examples demonstrating its capabilities.
Summing Integers with Awk
This example shows how to sum integers from a file or standard input, where each integer is on a new line. The sum += $1 part accumulates the value of the first field (the integer) in each line into the sum variable. The END block executes after all lines are processed, printing the final sum.
# To sum integers from a file or stdin, one integer per line:
printf '1\n2\n3\n' | awk '{ sum += $1} END {print sum}'
Using Custom Field Separators
Awk allows you to specify a custom delimiter for fields using the -F option. This is useful when your data is not separated by whitespace. In this example, the colon (:) is used as the separator.
# To use a specific character as separator to sum integers from a file or stdin:
printf '1:2:3' | awk -F ":" '{print $1+$2+$3}'
Generating Multiplication Tables
This command generates a multiplication table. It uses seq 9 to get numbers from 1 to 9, sed 'H;g' to duplicate lines for the table structure, and Awk to format the output, printing i x NR = product for each combination.
# To print a multiplication table:
seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}'
Specifying Output Separators
You can control the output field separator (OFS) using the BEGIN {OFS="..."} block. This example sets the output separator to a colon (:).
# To specify an output separator character:
printf '1 2 3' | awk 'BEGIN {OFS=":"}; {print $1,$2,$3}'
Conditional Execution on Rows
Awk can execute commands only on rows that meet specific criteria. This example prints lines where the second field ($2) is exactly "bailey".
# To execute commands only on rows that satisfy a certain condtion
printf "george jetson\nolive oyl\nbeetle bailey" | awk '$2=="bailey"{print $0}'
Row Matching with Regular Expressions
You can use regular expressions to filter lines. This example prints lines that end with "ley" using the regex /ley$/.
# To execute commands only on matching rows using regex
printf "george jetson\nolive oyl\nbeetle bailey" | awk '/ley$/{print $0}'
Further Resources
- GNU Awk Manual
- MDN Date Documentation (for understanding date/time formats often processed by Awk)
- Stack Overflow - Awk Tag