Text Processing

Essential Linux text processing utilities for searching, filtering, and manipulating file content

File Content Display

cat [filename]

  • Display entire file contents
  • Simple output to stdout

less [filename]

  • View file with pagination
  • Navigate with arrows/PgUp/PgDn

head -n [num] [file]

  • Show first N lines of file
  • Default: 10 lines

tail -n [num] [file]

  • Show last N lines of file
  • Use -f to follow live updates

Text Searching

grep [pattern] [file]

  • Search for text patterns
  • Case-sensitive by default

grep -i [pattern] [file]

  • Case-insensitive search

grep -r [pattern] [directory]

  • Recursive search in directories

Text Manipulation

sed 's/old/new/g' [file]

  • Replace text globally
  • Use -i for in-place editing

awk '{print $1}' [file]

  • Print specific columns
  • $1=first column, $2=second, etc

sort [file]

  • Sort lines alphabetically
  • Use -n for numeric sort

uniq [file]

  • Remove duplicate lines
  • Often used with sort

Text Filtering

cut -d',' -f1-3 [file]

  • Extract specific fields
  • -d sets delimiter, -f selects fields

tr 'a-z' 'A-Z' < [file]

  • Translate/replace characters
  • Example converts to uppercase

Word/Line Counting

wc -l [file]

  • Count lines in file

wc -w [file]

  • Count words in file

wc -c [file]

  • Count characters in file

Example Pipeline

1
2
3
4
cat access.log | grep "ERROR" | \
awk '{print $1, $4}' | \
sort | uniq -c | \
head -10