Master awk: From Simple Column Extraction to Powerful Data Aggregation
This tutorial explains how awk works, demonstrates printing specific columns, changing field separators, applying conditional filters, using BEGIN/END blocks for headers and footers, performing calculations, and leveraging built‑in variables and functions for advanced text processing on Linux.
awkworks similarly to sed, but while sed excels at line selection, awk excels at column selection. It processes input line by line, applying commands to each line.
Basic Syntax
awk [options] 'BEGIN{commands} pattern{commands} END{commands}' filenamePrinting Specific Columns
$ echo 'I love you' | awk '{print $3 $2 $1}'
# youloveIBy default, fields are split on whitespace. To change the delimiter, use -F:
$ echo '192.168.1.1' | awk -F "." '{print $2}'
# 168Conditional Filtering (pattern)
The pattern part selects which lines to process. For example, to print rows where the second column is at least 90:
$ awk '$2>=90{print $0}' score.txt
kitty 90 95 87If Statements
$ awk '{if($2>=90) print $0}' score.txt
kitty 90 95 87
$ awk '{if($2>=90) print $1,"优秀"; else print $1,"良好"}' score.txt
tom 良好
kitty 优秀
jack 良好BEGIN Block – Defining Header
$ awk 'BEGIN{print "姓名 语文 数学 英语"}{printf "%-8s%-5d%-5d%-5d
",$1,$2,$3,$4}' score.txt
姓名 语文 数学 英语
tom 60 60 60
kitty 90 95 87
jack 72 84 99The printf format %-8s left‑aligns the field to width 8, similar to C.
END Block – Adding Footer
$ echo ok | awk '{print $1} END{print "end"}'
ok
endData Aggregation Example
$ awk 'BEGIN{print "姓名 语文 数学 英语 总成绩"; sum1=0;sum2=0;sum3=0;sumall=0} {printf "%5s%5d%5d%5d%5d
",$1,$2,$3,$4,$2+$3+$4; sum1+=$2; sum2+=$3; sum3+=$4; sumall+=$2+$3+$4} END{printf "%5s%5d%5d%5d%5d
","总成绩",sum1,sum2,sum3,sumall}' score.txt
姓名 语文 数学 英语 总成绩
tom 60 60 60 180
kitty 90 95 87 272
jack 72 84 99 255
总成绩 222 239 246 707Useful Built‑in Variables
NF – number of fields in the current line (e.g., $NF is the last field)
NR – current record number (line number)
FILENAME – name of the current input file
OFMT – output format for numbers (default %.6g)
Common Built‑in Functions
Mathematical functions such as sqrt() and random number generation with rand() and srand():
$ echo 1 2 | awk '{print $1+sqrt($2)}'
2.41421
$ echo 1 | awk 'BEGIN{srand()}{print rand()}'
0.929885Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
