Pcornel
Linux

Text Processing

Cheat sheet · https://pcornel.dk/topic.php?topic=2679

Back to the Topic

Answer

Text processing tools read and transform text streams: grep (search), sed (edit), awk (fields and logic), cut, sort, uniq, tr, wc, head/tail, jq for JSON.

Remember

Build pipelines step by step and check each stage's output. Use sed -i.bak to keep a backup when editing in place. For JSON use jq, not grep. Locale affects sort order (LC_ALL=C sort).

First checks

  • grep -E '<regex>' <file> — matching lines
  • awk '{print $1, $5}' <file> — selected fields
  • sed -n '/start/,/end/p' <file> — lines between two patterns
  • sort | uniq -c | sort -rn — count and rank values
  • jq '.items[].name' <file.json> — fields from JSON

Commands

cat
Concatenate files or standard input and write the byte stream to standard output.
less
Page through text interactively without loading the entire input into the terminal.
head
Read the first requested lines or bytes from one or more inputs.
tail
Read the end of a file or follow appended data as it arrives.
grep
Select lines whose text matches a literal or regular-expression pattern.
sort
Order text records according to lexical, numeric, key or locale rules.
uniq
Collapse or count adjacent duplicate lines in an already grouped text stream.
cut
Extract selected byte, character or delimiter-separated fields from each input line.
tr
Translate, squeeze or delete characters in a byte or character stream.
wc
Count lines, words, bytes or characters in text input.
sed
Transform text streams with scripted substitutions, selections and line-oriented edits.
awk
Process structured text records using field-aware patterns, expressions and actions.
xargs
Build command invocations from standard-input items while controlling batching and quoting.
diff
Compare two files or directory trees and report line-oriented differences.
df -P

Verify in this order

input text → filter → extract fields → transform → aggregate → output

Where it usually breaks

First mismatch in persistent intent → provider/parser/generator → kernel/userspace runtime → dependency/security context → logs/counters → consumer-visible read-back; preserve identity/native diagnostic/pre-state before mutation.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=2679

Confirmation

Confirm action

Continue?