awk Command Examples in Linux¶
Introduction¶
These examples show practical ways to use awk on a Linux terminal. Each example is written so you can adapt it for administration or troubleshooting.
Example 1: Basic Usage¶
awk '{ print $1 }' /etc/passwd
This is the simplest form of the command and is a good starting point before adding options.
Example 2: Common Admin Task¶
awk -F: '{ print $1, $7 }' /etc/passwd
This example reflects a common task on RHEL, Rocky Linux, AlmaLinux, or similar systems.
Example 3: Useful Option¶
awk '$5 > 1000 { print $1, $5 }' data.txt
This option helps narrow the result, change behavior, or handle a more realistic target.
Example 4: Real-World Scenario¶
df -h | awk 'NR>1 { print $1, $5 }'
Use this pattern when the task moves beyond a single basic command.
Example 5: Verification¶
awk --version
Example output:
root
bin
daemon
Common Mistakes¶
- Forgetting that awk splits on whitespace by default unless
-Fis set. - Using double quotes around the awk program and letting the shell expand
$1. - Trying to parse complex formats when a dedicated parser would be safer.
Quick Reference¶
awk '{ print $1 }' /etc/passwd
awk -F: '{ print $1, $7 }' /etc/passwd
awk '$5 > 1000 { print $1, $5 }' data.txt
df -h | awk 'NR>1 { print $1, $5 }'
awk --version
Related Guides¶
Summary¶
Good awk usage means choosing the right option, keeping the target clear, and verifying the result with output you can explain.