🐧Go deeper — read the bookLinux for C++ Interviews: tracing a live process— runnable code & full walkthrough →

linux · medium

Text Processing — grep, awk, sed, pipes & xargs

Command-line text processing: grep (-r/-I/-n/-o/-c), pipes and the 2>&1 ordering trap, awk for column aggregation, sed -i for substitution, and the universal sort | uniq -c | sort -rn count-and-rank pipeline.

🔑 Key line

grep -rIn / -o / -c to search. Streams: > truncate, >> append, 2>&1 (after >) merges stderr. awk for columns ({sum+=$N} END), sed -i for substitution. Universal idiom: sort | uniq -c | sort -rn = count & rank.

The code

grep -rIn 'error' /var/log/ # recursive, line nums, skip binaries
grep -oE 'feed:[0-9]+' f # print only the match
cmd > out 2>&1 # stdout + stderr to a file
awk '{sum+=$10} END{print sum}' f # sum a column
sed -i 's/old/new/g' f # in-place substitute
# The universal count-and-rank pipeline
awk '{print $1}' f | sort | uniq -c | sort -rn | head

What this lesson walks through

  1. 01grep: find lines fast
  2. 02Pipes & the 2>&1 order trap
  3. 03The count-and-rank pipeline

grep -rIn searches recursively with line numbers; -o extracts just the match, -c counts, -v inverts.

See it animated — step by step, at your own pace

Unlock the full interactive walkthrough of Text Processing — grep, awk, sed, pipes & xargs and 100+ animated C++ interview lessons.

← Previous
Processes & Signals — ps, kill, jobs, systemctl
Next →
Files, Permissions & Disk — find, chmod, du, df