Free tech learning for everyone, from basics to pro

ScriptingProgramming and Application SecurityBeginner

Text processing on the command line: grep, awk, sed, cut and sort

In short: Most network and security data arrives as text: logs, CSV exports and show command output. A handful of tools, grep, cut, awk, sed, sort and uniq, chained with pipes, can filter, extract and count that data in seconds.

Small tools, chained together

The Unix approach is to give each tool one job and connect them with the pipe symbol |. The output of one command becomes the input of the next. Once you are comfortable with six tools you can answer questions like "which source addresses failed SSH logins most often today?" without opening a spreadsheet.

A text pipelineSourcelog or showoutputgrepkeep linescut / awkpick fieldssort | uniq-ccountResulttop items
Figure 1. Text tools chain together: each one does a small job and passes its output to the next.

grep and cut

grep keeps lines that match a pattern. Useful options are -i (ignore case), -v (invert), -c (count), -E (extended regular expressions) and -r (search folders).

grep -i "err" /var/log/syslog               # any line with ERR, error, Err...
grep -v "^#" /etc/ssh/sshd_config | grep -v "^$"   # config without comments or blanks
grep -E "%LINK-3-UPDOWN|%LINEPROTO-5" switch.log  # Cisco interface flaps
cut -d',' -f2,5 devices.csv                   # columns 2 and 5 of a CSV

cut is quick but only works with a single fixed delimiter. When columns are separated by a varying number of spaces, as in most show commands, use awk instead.

awk: fields and simple logic

awk splits each line into fields ($1, $2 and so on) on whitespace by default and lets you add conditions and arithmetic.

# Interfaces that are down from 'show ip interface brief' saved to a file
awk '$5 == "down" {print $1, $2}' show_ip_int_brief.txt

# Total bytes per source IP from a CSV export (field 3 = src, field 7 = bytes)
awk -F',' 'NR > 1 {sum[$3] += $7} END {for (ip in sum) print sum[ip], ip}' flows.csv | sort -rn | head

NR > 1 skips the header row. The END block runs once after all lines are read, which is where totals are printed.

sed, sort and uniq

Which tool for which jobgrepfind matching linescutfixed delimitersawkfields, sums, logicsedfind and replacesortorder linesuniq -ccount duplicates
Figure 2. Each classic Unix text tool has one main job; combining them handles most log analysis.
# Mask anything that looks like an SNMP community before sharing a config
sed -E 's/(snmp-server community) [^ ]+/\1 <removed>/' router.cfg

# Top 10 source IPs with failed SSH logins
grep "Failed password" /var/log/auth.log |
  grep -oE "from [0-9.]+" | awk '{print $2}' |
  sort | uniq -c | sort -rn | head -n 10

uniq only removes adjacent duplicates, so always sort before uniq -c. The final sort -rn orders the counts numerically, largest first. Note that sed prints the changed text to the screen; add -i only when you really want to edit the file in place, ideally with a backup suffix such as -i.bak.

Build pipelines one stage at a time. Run the first grep and look at the output, then add the next command and check again. When something goes wrong you will know exactly which stage caused it. Also be careful with regular expressions that are too loose: a pattern such as 10.1.1.1 also matches 10.1.1.10 and 10.1.1.100, and an unescaped dot matches any character. Anchor and escape patterns when precision matters, for example grep -w to match whole words only.

QuestionOne-liner idea
How many errors today?grep -c with a date pattern
Which users logged in?grep then awk on the user field, sort -u
Tidy a config before reviewsed to mask secrets, grep -v to drop comments

Tip: these skills appear in practical exams and on the job when a SIEM is not available. Practise on your own lab logs; never copy customer logs to personal machines.

Practice

Why must you sort before uniq -c?
uniq only collapses duplicate lines that sit next to each other, so unsorted input gives wrong counts.
When is awk better than cut?
When fields are separated by a variable number of spaces, or when you need conditions or arithmetic such as sums.
What does grep -v do?
It inverts the match, printing only lines that do not match the pattern.

Key takeaways

  • Chain small tools with pipes.
  • grep filters lines; cut and awk extract fields.
  • awk can filter, sum and group with arrays.
  • sed edits text; use -i carefully and keep a backup.
  • sort before uniq -c, then sort -rn for top counts.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.