Mastering AWK: Quick Commands to Clean, Filter, and Analyze Text Files
This guide demonstrates how to use AWK’s sub, gsub, and pattern‑matching features to delete blank lines, trim whitespace, reverse file order, add line numbers, count or filter specific strings, replace text, and combine AWK with other Linux utilities for everyday text‑processing tasks.
Awk is a powerful text‑processing utility that scans files line by line, splits each line into fields, matches patterns, and executes actions.
Preparing a sample file
The following heredoc creates content.txt with several space‑separated records:
cat << EOF >> content.txt
hitesh engineer sales 30000
jayesh director account 25000
vyom manager purchase 20000
bhavesh engineer sales 30000
rajesh directory sales 40000
niraj clerk account 20000
jay peon purchase 23000
deep clerk sales 20000
EOFDelete all blank lines
Use the built‑in variable NF to filter out empty records:
awk NF content.txtTrim leading and trailing spaces or tabs
Remove leading whitespace with sub: awk '{ sub(/^[ \t]+/, ""); print }' content.txt Remove trailing whitespace with sub again: awk '{ sub(/[ \t]+$/, ""); print }' content.txt Handle both ends in a single command using gsub:
awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); print }' content.txtReverse the order of all lines
Store each line in an array and print it backwards in the END block:
awk '{ b[i++] = $0 } END { for (j=i-1; j>=0; j--) print b[j] }' content.txtAdd line numbers to non‑empty lines
Use the NF variable to skip blank lines and prepend a counter:
awk 'NF { $0 = ++a ": " $0 }; { print }' content.txtCount lines containing a specific string
Count occurrences of the word engineer:
awk '/engineer/{n++} END {print n+0}' content.txtPrint lines that match or do not match a pattern
Matching lines: awk '/engineer/' content.txt Non‑matching lines (e.g., exclude jayesh):
awk '!/jayesh/' content.txtReplace strings
Replace the word engineer with doctor:
awk '{ gsub(/engineer/, "doctor"); print }' content.txtReplace any of jayesh, hitesh, or bhavesh with mahesh:
awk '{ gsub(/jayesh|hitesh|bhavesh/, "mahesh"); print }' content.txtCombine AWK with other commands
Show device name, available space, and usage percentage from df -h:
df -h | awk '{ printf("%-24s\t%-6s\t%-4s
", $1, $4, $5) }'List the number of open connections per IP (useful for spotting attacks):
netstat -ntu | awk '{print $5}' | cut -d: -f1 | awk '/[0-9]/' | sort | uniq -c | sort -nConclusion
Through concrete examples we have shown how a single AWK command can perform a wide range of everyday text‑processing and system‑administration tasks efficiently.
Signed-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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
