Linux Text Processing
Learn how to search, filter, transform, and count text in Linux using grep, awk, sed, cut, sort, uniq, wc, and pipes.
Why Text Processing Matters in DevOps
Linux systems expose a huge amount of information as plain text: logs, configuration files, command output, and status reports. That means DevOps engineers do not just read text; they filter it, search it, transform it, count it, and pipe it into other tools.
The commands in this lesson turn the shell into a lightweight data-processing environment.
Searching with grep
grep searches for lines that match a pattern.
grep error app.log
grep -i failed app.log
grep -n timeout app.log
grep -r nginx /etc
Useful flags:
-iignores case-nshows line numbers-rsearches recursively
Basic Regular Expressions
grep supports regular expressions, which let you search by pattern instead of fixed text.
grep '^ERROR' app.log
grep '200$' access.log
grep '[0-9][0-9][0-9]' access.log
Examples:
^ERRORmeans lines starting withERROR200$means lines ending with200[0-9]matches a digit
Tip: Start simple with fixed text, then add regex only when needed.
Selecting Fields with cut
cut extracts parts of each line.
cut -d: -f1 /etc/passwd
cut -d' ' -f1 access.log
Here:
-dsets the delimiter-fchooses the field number
For /etc/passwd, fields are separated by :. The first field is the username.
Sorting and Deduplicating
sort orders lines. uniq removes adjacent duplicate lines.
sort names.txt
sort access.log | uniq
sort access.log | uniq -c
uniq -c also counts repeated entries.
This is helpful when summarizing logs or finding repeated values.
Counting with wc
wc stands for word count, but it can count more than words.
wc app.log
wc -l app.log
wc -w notes.txt
wc -c payload.json
Useful flags:
-lcounts lines-wcounts words-ccounts bytes
Example use case:
grep ERROR app.log | wc -l
This counts how many error lines appear in the log.
Pattern-Based Processing with awk
awk is a powerful text-processing language. For beginners, think of it as a tool for working with columns and applying logic.
awk '{print $1}' access.log
awk '/ERROR/ {print $0}' app.log
awk -F: '{print $1, $7}' /etc/passwd
Key ideas:
$1,$2,$3refer to fields-F:sets the field separator to:/pattern/ { action }runs the action only on matching lines
A realistic example:
awk '$9 == 500 {print $1, $7}' access.log
In a typical web log, this might print client IPs and paths for HTTP 500 responses.
Editing Streams with sed
sed is a stream editor. It is often used for quick substitutions.
sed 's/http:/https:/g' urls.txt
sed 's/DEBUG/INFO/g' app.log
The s/old/new/g pattern means substitute all matches on each line.
In-Place Editing
You can modify a file directly with -i.
sed -i 's/listen 80/listen 8080/' nginx.conf
Be careful with in-place editing, especially on production configs.
Note: It is often smart to copy a config file before running
sed -ion it.
Pipes and Redirection
A pipe sends the output of one command into another command.
cat access.log | grep 500 | wc -l
This works, but you can usually skip cat and write:
grep 500 access.log | wc -l
Redirection sends output to files.
grep ERROR app.log > errors.txt
grep WARN app.log >> errors.txt
command 2> error-output.txt
>overwrites a file>>appends to a file2>redirects standard error
A Real Log Analysis Workflow
Suppose you want to find the most common failing endpoint from a web log.
awk '$9 >= 500 {print $7}' access.log | sort | uniq -c | sort -nr
This pipeline does four things:
- selects lines with server errors
- prints the request path
- counts duplicates
- sorts from highest count to lowest
That is a strong example of why text processing matters. You are turning raw logs into actionable insight with one command chain.
When to Use Which Tool
- Use
grepto find matching lines - Use
cutfor simple field extraction - Use
awkfor field logic and patterns - Use
sedfor substitutions and quick edits - Use
sort,uniq, andwcfor summarizing output
You do not need to master every advanced flag today. Learn the core use cases first, then build from there.
Test Your Understanding
Let's see how well you understood the concepts! These exercises will help reinforce what you just learned.
Exercise 1: Anchoring a grep search
Question: If you want only log lines that begin with ERROR, which grep pattern from the lesson should you use?
Exercise 1: Anchoring a grep search
Pick the grep pattern that matches lines starting with ERROR.
Exercise 2: Splitting fields with cut
Question: What does cut -d: -f1 /etc/passwd return?
Exercise 2: Splitting fields with cut
Determine what cut prints when it uses : as the delimiter and field 1.
Exercise 3: Counting repeated values
Scenario: You want to know which HTTP path appears most often among 500-level responses.
Question: What is the main job of sort | uniq -c in the tutorial pipeline after awk prints the request path?
Exercise 3: Counting repeated values
Choose why sort and uniq -c are paired in the error-analysis pipeline.