How do you handle multi-gigabyte log files efficiently with these tools?
AdvancedConceptText processing: grep, sed, awk, cut, sort, uniq
Answer
LC_ALL=Cspeeds upgrepandsortdramatically by avoiding locale-aware comparisons.- Filter early: put
grep -F(fixed string, fastest) first, then process fewer lines. - One
awkpass instead of several piped tools. - Read compressed logs directly:
zgrep,zcat f.gz | awk ...,zstdcat. sort -S 2G --parallel=4 -T /data/tmpfor large sorts (memory buffer, threads, temp dir on a big disk).grep -m1orawk '... {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
More on Text processing: grep, sed, awk, cut, sort, uniq
What are the most useful `grep` flags?BeginnerWhat is the difference between basic, extended and Perl regular expressions in grep?IntermediateWhat are the core `sed` commands you use in automation?BeginnerWhy does `sed -i` behave differently on macOS and Linux?IntermediateHow do you use variables inside a `sed` expression safely?AdvancedWhat is the structure of an `awk` program?Beginner