Operations 7 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering AWK: Quick Commands to Clean, Filter, and Analyze Text Files

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
EOF

Delete all blank lines

Use the built‑in variable NF to filter out empty records:

awk NF content.txt

Trim 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.txt

Reverse 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.txt

Add 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.txt

Count lines containing a specific string

Count occurrences of the word engineer:

awk '/engineer/{n++} END {print n+0}' content.txt

Print 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.txt

Replace strings

Replace the word engineer with doctor:

awk '{ gsub(/engineer/, "doctor"); print }' content.txt

Replace any of jayesh, hitesh, or bhavesh with mahesh:

awk '{ gsub(/jayesh|hitesh|bhavesh/, "mahesh"); print }' content.txt

Combine 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 -n

Conclusion

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

data cleaningtext processingShell scriptingawk
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.