Text Processing

Advanced text manipulation and processing commands

cut/paste - Column Operations

Column extraction and file merging operations

# Extract specific columns
cut -d',' -f1,3 file.csv

# Extract characters from position
cut -c1-10 file.txt

# Extract fields with custom delimiter
cut -d':' -f1 /etc/passwd

# Paste files side by side
paste file1.txt file2.txt

# Paste with custom delimiter
paste -d',' file1.txt file2.txt

# Paste with serial numbers
paste -d',' - - file.txt

# Extract specific range
cut -c5-15 file.txt
Individual commands:
cut -d',' -f1,3 file.csv
cut -c1-10 file.txt
cut -d':' -f1 /etc/passwd
paste file1.txt file2.txt
paste -d',' file1.txt file2.txt
paste -d',' - - file.txt
cut -c5-15 file.txt
tr - Character Translation

Character translation and transformation

# Translate characters
tr 'a-z' 'A-Z' < file.txt

# Delete characters
tr -d '0-9' < file.txt

# Squeeze repeated characters
tr -s ' ' < file.txt

# Translate and delete
tr -d '\n' < file.txt

# Translate specific characters
tr '[:lower:]' '[:upper:]' < file.txt

# Delete non-printable characters
tr -cd '[:print:]' < file.txt

# Translate newlines to spaces
tr '\n' ' ' < file.txt
Individual commands:
tr 'a-z' 'A-Z' < file.txt
tr -d '0-9' < file.txt
tr -s ' ' < file.txt
tr -d '\n' < file.txt
tr '[:lower:]' '[:upper:]' < file.txt
tr -cd '[:print:]' < file.txt
tr '\n' ' ' < file.txt
wc - Word Count

Text counting and statistics operations

# Count lines, words, characters
wc file.txt

# Count only lines
wc -l file.txt

# Count only words
wc -w file.txt

# Count only characters
wc -c file.txt

# Count characters without newlines
wc -m file.txt

# Count for multiple files
wc *.txt

# Count with line numbers
wc -l file.txt | awk '{print $1 " lines"}' 
Individual commands:
wc file.txt
wc -l file.txt
wc -w file.txt
wc -c file.txt
wc -m file.txt
wc *.txt
wc -l file.txt | awk '{print $1 " lines"}'
head/tail - Line Extraction

Line extraction from beginning and end of files

# Show first 10 lines
head file.txt

# Show first N lines
head -n 20 file.txt

# Show first N bytes
head -c 100 file.txt

# Show last 10 lines
tail file.txt

# Show last N lines
tail -n 20 file.txt

# Follow file changes
tail -f logfile.txt

# Show last N bytes
tail -c 100 file.txt

# Show lines from line N to end
tail -n +20 file.txt
Individual commands:
head file.txt
head -n 20 file.txt
head -c 100 file.txt
tail file.txt
tail -n 20 file.txt
tail -f logfile.txt
tail -c 100 file.txt
tail -n +20 file.txt