grep, sed, and awk: The Text Processing Power Toolbox
Master the three core Unix text processing tools for searching, transforming, and analyzing log files and config data.
Netbay Cloud Team
Netbay Engineering
On this page
If you work with Linux servers, you will spend a significant amount of time reading and transforming text. Log files, configuration files, CSV exports, API responses, and process listings are all text. Three tools form the backbone of Unix text processing: grep for searching, sed for stream editing, and awk for field-based analysis. Together, they replace heavy GUI tools and scripting languages for the vast majority of day-to-day tasks.
grep: Finding Lines That Match
The most basic use of grep is filtering lines that contain a pattern:
grep "ERROR" /var/log/syslog
grep -i "timeout" /var/log/nginx/error.log
grep -rn "password" /etc/ssh/The -i flag ignores case, -r recurses into directories, and -n shows line numbers. For log analysis, combining grep with the --color=always flag highlights matches visually, and piping through less -R preserves the colors.
More powerful patterns use extended regex with -E:
grep -E "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" /var/log/auth.log
grep -c "403" /var/log/nginx/access.logThe first command extracts lines starting with IP addresses. The second counts how many 403 responses occurred. These patterns are the building blocks for every monitoring and debugging workflow on a server.
sed: Stream Editing Without Opening Files
sed performs find-and-replace operations directly on streams. The most common use is substituting text:
sed -i 's/old-text/new-text/g' config.conf
sed -n '50,100p' /var/log/syslog
sed -i '/^#/d' /etc/nginx/conf.d/custom.confThe first command replaces every occurrence of "old-text" with "new-text" in the file. The -i flag edits in-place. The second prints lines 50 through 100. The third deletes all comment lines from a config file.
For multi-line operations, sed can insert, append, and delete blocks:
sed -i '/server {/a \ client_max_body_size 64M;' /etc/nginx/conf.d/app.conf
sed -i '/location /api/,/}/ s/proxy_pass .*/proxy_pass http://127.0.0.1:3000;/' /etc/nginx/conf.d/app.confThe first line inserts a client_max_body_size directive after every server { block. The second rewrites the proxy_pass inside /api location blocks. These one-liners save minutes compared to opening a text editor for simple changes.
awk: Field Processing and Pattern Matching
While grep filters lines and sed edits text, awk processes fields. By default, awk splits each line on whitespace and assigns fields to $1, $2, and so on:
awk '{print $1, $7}' /var/log/nginx/access.log
awk '/500/ {print $1, $4}' /var/log/nginx/access.log
awk -F: '{print $1, $3}' /etc/passwdThe first prints the client IP and request path from nginx access logs. The second shows IPs and timestamps for 500 errors. The third lists usernames and UIDs from /etc/passwd using colon as the field separator.
For aggregated analysis, awk has built-in variables and pattern-action blocks:
awk '{count[$9]++} END {for (code in count) print code, count[code]}' /var/log/nginx/access.log | sort -k2 -rn
awk '{sum+=$10} END {print "Total bytes:", sum}' /var/log/nginx/access.logThe first counts HTTP response codes and sorts by frequency. The second sums the response size column. These are the kinds of quick analytics that normally require a scripting language, but awk handles them in a single pipeline.
Chaining Them Together
The real power emerges when you combine these tools:
grep "POST" /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -10
journalctl -u nginx --since "1 hour ago" | grep -E "error|warn" | sed 's/.*]: //' | sort | uniq -c | sort -rnThe first pipeline finds POST requests, extracts IPs, counts unique occurrences, and shows the top 10 offenders. The second pulls recent nginx logs, filters for errors and warnings, strips the timestamp prefix, and ranks messages by frequency. This kind of pipeline is essential for incident response and capacity planning.
Takeaway
grep, sed, and awk form a text processing toolkit that handles everything from quick log searches to complex data transformations. Practice these tools on a live Netbay Linux VPS and build muscle memory for server administration at netbayhosts.in.
Keep reading
Follow along on a real VPS
Deploy Linux in under 60 seconds
These guides are written against Ubuntu, Debian, and RHEL-family images — the same ones on NetBay.
Deploy an instance