06 Text Processing
Learning objectives
- Sort, deduplicate, and count text.
- Extract fields by delimiter.
- Perform simple text replacement and batch processing.
Command overview
| Command | Purpose | Meaning | Basic usage | Example |
|---|---|---|---|---|
sort | Sort text lines | Sorts lexically or numerically | sort file | sort names.txt |
uniq | Remove adjacent duplicate lines | Short for unique; usually used with sort | uniq file | sort names.txt | uniq |
cut | Extract text columns | Selects fields by character position or delimiter | cut [options] file | cut -d ":" -f 1 /etc/passwd |
awk | Analyze text and process columns | Suitable for field-oriented text processing | awk 'rules' file | awk '{print $1}' app.log |
sed | Stream text editor | Commonly replaces, removes, or prints lines | sed 'expression' file | sed 's/old/new/g' file.txt |
tr | Translate or delete characters | Performs character-level transformations | tr 'old_chars' 'new_chars' | printf '%s\n' abc | tr '[:lower:]' '[:upper:]' |
xargs | Turn NUL-delimited input into arguments | Ordinary newlines cannot represent every legal filename; on GNU/Linux use with -print0/-0 | find ... -print0 | xargs -0 command | find . -type f -name '*.log' -print0 | xargs -0 -- wc -l |
Common scenarios
Sort and deduplicate:
sort names.txt | uniq
sort names.txt | uniq -cExtract the first field of a colon-separated file:
cut -d ":" -f 1 /etc/passwdExtract the first log field:
awk '{print $1}' app.logReplace text in output:
sed 's/error/ERROR/g' app.logTurn lowercase into uppercase:
printf '%s\n' 'linux' | LC_ALL=C tr '[:lower:]' '[:upper:]'Pass found files safely to another command:
find . -type f -name '*.log' -exec wc -l -- {} +
find . -type f -name '*.log' -print0 | xargs -0 -- wc -lNotes for beginners
uniqremoves only adjacent duplicate lines, so usesortbeforeuniqin the usual case.sed 's/old/new/g' fileprints a transformed result by default; it does not modify the source file.sed -iis not POSIX. GNU and BSD/macOS differ on-iand backup-suffix syntax. This course first writes to a new file and compares it instead of editing in place.awksplits fields on whitespace by default;$1is the first field and$2is the second.- An ordinary newline pipeline cannot safely represent every legal filename. The portable first choice is
find -exec ... {} +; GNU/Linux can use-print0 | xargs -0. - Sorting, case conversion, and character classes depend on locale. Set
LC_ALL=Cexplicitly when reproducible byte order is required.
Replacement exercise without overwriting a source file
sed 's/error/ERROR/g' -- app.log > app.log.new
cmp --silent -- app.log app.log.new
cmp_rc=$?
printf 'content_changed=%s\n' "$cmp_rc"Inspect app.log.new first, then decide whether to replace the original with a controlled mv. Do not redirect output back into the input file.
Mastery check
- I can explain why
uniqusually requires sorted input. - I can handle filenames containing spaces and newlines without bare
find | xargs. - I know that sed in-place editing and locales have platform differences.