Bash Text Processing

Bash has powerful text processing tools built in.

grep — Search Text

Bash
grep "error" log.txt          # basic search
grep -i "error" log.txt       # case-insensitive
grep -n "error" log.txt       # line numbers
grep -v "debug" log.txt       # inverse match
grep -c "error" log.txt       # count matches
grep -r "TODO" src/           # recursive search

sed — Stream Editor

Bash
sed 's/old/new/' file.txt          # find and replace
sed -i 's/old/new/g' file.txt     # in-place edit
sed '/^#/d' config.txt            # delete comments

awk — Pattern Scanning

Bash
awk '{print $1}' data.csv                      # print first column
awk '$3 > 100' data.csv                         # filter rows
awk -F: '{print $1, $3}' /etc/passwd           # custom delimiter
awk '{sum += $2} END {print sum}' sales.txt    # calculate sum

Real-World Example

Bash
# Count errors per hour
grep "ERROR" app.log | awk '{print $1, $2}' | cut -d: -f1,2 | sort | uniq -c

# Find top 10 IPs from access log
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10

Tips

  • grep for searching, sed for editing, awk for column processing.
  • Use | to chain tools — one-liners are the Bash superpower.

Next: error-handling.