Interview Prep/Shell Scripting/Text processing: grep, sed, awk, cut, sort, uniq

How do you handle multi-gigabyte log files efficiently with these tools?

AdvancedConceptText processing: grep, sed, awk, cut, sort, uniq
Answer
  • LC_ALL=C speeds up grep and sort dramatically by avoiding locale-aware comparisons.
  • Filter early: put grep -F (fixed string, fastest) first, then process fewer lines.
  • One awk pass instead of several piped tools.
  • Read compressed logs directly: zgrep, zcat f.gz | awk ..., zstdcat.
  • sort -S 2G --parallel=4 -T /data/tmp for large sorts (memory buffer, threads, temp dir on a big disk).
  • grep -m1 or awk '... {print; exit}' to stop at the first match.
LC_ALL=C zgrep -F ' 502 ' access.log.*.gz | awk '{print $7}' | LC_ALL=C sort | uniq -c | sort -rn | head
All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Text processing: grep, sed, awk, cut, sort, uniq