📑 Command: awk (Pattern Scanning and Processing)
awk is a powerful, text-processing language used for manipulating data and generating reports. Unlike grep, which just finds lines, awk can understand columns (fields), perform math, and transform data as it processes it.
1. The "Why"
For a developer managing Arch Linux and working on projects, awk is the "Swiss Army Knife" for logs and data.
- Extract Specific Columns: Pull out just the process IDs (PIDs) from a list or just the filenames from a directory listing.
- Log Analysis: Process your journalctl or Supabase logs to find specific errors occurring at certain times.
- System Monitoring: Check your GPU temperature or CPU usage by filtering system stats.
- Math in the Terminal: Sum up file sizes or calculate averages without opening a spreadsheet.
2. How awk Thinks
awk views a file as a series of Records (lines) and Fields (columns). By default, it uses whitespace (spaces or tabs) to separate columns.
$0: Represents the entire line.$1,$2,$3...: Represent the first, second, third columns, and so on.NF: The total Number of Fields in the current line.NR: The current Number of Record (the line number).
3. Basic Syntax
awk 'pattern { action }' filename
If you don't provide a pattern, it performs the action on every line. If you don't provide an action, the default is to print the line.
4. Practical Examples for Your Workflow
A. Print Specific Columns
To see only the names and sizes of files in a directory:
ls -l | awk '{ print $9, $5 }'
B. Search for a Pattern (Like grep, but better)
Find lines in your LaTeX e-book source that contain the word "Chapter":
awk '/Chapter/ { print "Found at line " NR ": " $0 }' blueprint.tex
C. Using a Different Separator
If you are looking at /etc/passwd (which uses : instead of spaces):
awk -F: '{ print $1 }' /etc/passwd
D. Filtering Based on Value
Find all files in a folder that are larger than 1MB (1,048,576 bytes):
ls -l | awk '$5 > 1048576 { print $9 }'
5. Advanced Scripting (One-Liners)
1. Summing up a column:
Sum the total size of all .java files in your Preader directory:
ls -l *.java | awk '{ sum += $5 } END { print "Total Java code size:", sum, "bytes" }'
2. Extracting your Local IP address:
A quick way to grab just your IP from the ip addr command:
ip addr show wlan0 | awk '/inet / { print $2 }' | cut -d/ -f1
6. Pro-Tips
- The Single Quotes: Always wrap your
awkcommand in single quotes ('...') so the shell doesn't try to interpret the$signs as shell variables. BEGINandENDblocks: UseBEGIN { ... }to run code before any lines are read (like printing a header) andEND { ... }to run code after the file is finished (like printing a total).- Arch Linux Context: You'll often see
awkused in custom Waybar scripts or Hyprland status monitors to parse output from commands likefree -morupowerinto a format the bar can read.
7. Summary Reference
| Goal | Command Example |
|---|---|
| Print 1st and 3rd column | awk '{ print $1, $3 }' |
| Filter by column value | awk '$3 == "ERROR" { print $0 }' |
| Change field separator | awk -F"," '{ print $2 }' |
| Count lines | awk 'END { print NR }' |