guest@linuxthings: ~/guides/text-proc.md

guest@linuxthings:~/guides$ man text-proc

Text Manipulation

grep (Global Regular Expression Print)

grep "error" logfile.txt
grep -i "term" file       # Case insensitive
grep -r "term" dir/       # Recursive search in directory
grep -v "term" file       # Invert match (lines WITHOUT term)
grep -n "term" file       # Show line numbers
grep -l "term" file*      # List filenames containing match only
grep -c "term" file       # Count matching lines
grep -w "is" file         # Match whole words only
grep -o "[0-9]\+" file    # Print only the matching part
grep -A 3 "term" file     # Also show 3 lines After each match
grep -B 2 "term" file     # Also show 2 lines Before
grep -C 2 "term" file     # 2 lines of Context on both sides
grep -E "cat|dog" file    # Extended regex (alternation without backslashes)
grep -F "a.b*c" file      # Fixed string: no regex characters at all

Regular Expressions

Pattern

Matches

.

Any single character

^ / $

Start / end of the line

[abc] / [^abc]

One character in / not in the set

[[:digit:]] [[:alpha:]] [[:space:]]

Character classes

*

Zero or more of the previous item

+

One or more of the previous item

?

Zero or one of the previous item

{n,m}

Between n and m repetitions

( )

Group items together

The last four rows use extended syntax (grep -E). Plain grep needs a backslash before them, as in \+, \?, \{2,3\} and \(...\), and alternation is written cat\|dog. Wrap patterns in single quotes so the shell leaves them alone.

grep '^#' file            # Comment lines
grep -v '^\s*$' file      # Drop blank lines
grep -E '^[0-9]{3}-[0-9]{4}$' phones.txt

Sorting and Uniqueness

sort names.txt             # Sort alphabetically
sort -n numbers.txt        # Sort numerically
sort -r names.txt          # Reverse sort
sort -f names.txt          # Ignore case
sort -u names.txt          # Sort and drop duplicates
sort -h sizes.txt          # Human-readable sizes (2K, 1G)
sort -t, -k2 data.csv      # Sort by the 2nd comma-separated field
sort -t: -k3 -n /etc/passwd  # Sort users by numeric UID
sort names.txt | uniq      # Remove duplicates (requires sorted input)
sort names.txt | uniq -c   # Count occurrences
sort names.txt | uniq -d   # Show only duplicated lines
sort access.log | uniq -c | sort -rn | head  # Top 10 most frequent lines
shuf -n 3 names.txt        # Pick 3 random lines

Extraction and Translation

wc file                    # Count lines, words, characters
wc -l file                 # Count lines only
cut -d: -f1 /etc/passwd    # Extract first field (delimiter :)
cut -d, -f1,3 data.csv     # Fields 1 and 3 of a CSV
cut -c1-10 file            # Characters 1 to 10 of each line
tr 'a-z' 'A-Z' < file      # Translate lowercase to uppercase
tr -d '\r' < dos.txt > unix.txt  # Strip Windows carriage returns
tr -s ' ' < file           # Squeeze repeated spaces into one
paste file1 file2          # Merge lines side-by-side
paste -d, file1 file2      # ...separated by commas
column -t file             # Align whitespace-separated columns as a table
column -s, -t data.csv     # Pretty-print a CSV

Stream Editing (sed & awk)

sed 's/old/new/g' file     # Replace 'old' with 'new' globally
sed 's/old/new/' file      # Replace only the first match on each line
sed -i 's/old/new/g' file  # Edit the file in place
sed -i.bak 's/a/b/g' file  # In place, keeping a backup as file.bak
sed -n '10,20p' file       # Print only lines 10 to 20
sed '/^#/d' file           # Delete comment lines
sed 's|/usr/local|/opt|g' f  # Use another delimiter when the text has slashes
awk '{print $1}' file      # Print first column of each line
awk '{print $2, $4}' file  # Print columns 2 and 4
awk -F: '{print $1, $7}' /etc/passwd  # Custom field separator
awk '$3 > 100' data        # Lines where column 3 is greater than 100
awk 'length > 80' file     # Lines longer than 80 characters
awk '{sum += $2} END {print sum}' data  # Total of column 2
awk 'NR==5' file           # Print line 5

Splitting the Stream with tee

who | tee who.txt | sort            # Save a copy mid-pipeline
cmd | sudo tee /etc/somefile        # Write to a root-owned file
cmd | tee -a log.txt                # Append instead of overwrite

guest@linuxthings:~/guides$