Bash
Text Processing
Text processing with grep, sed, awk.
By EZ4Code Team
bashtext
Code
#!/bin/bash
# grep: search text
grep "pattern" file.txt
grep -i "pattern" file.txt # Ignore case
grep -r "pattern" dir/ # Recursive search
grep -n "pattern" file.txt # Show line numbers
grep -v "pattern" file.txt # Invert match
grep -c "pattern" file.txt # Match count
grep -E "pat1|pat2" file.txt # Extended regex
# sed: stream editor
sed 's/old/new/' file.txt # Replace first
sed 's/old/new/g' file.txt # Global replace
sed -i 's/old/new/g' file.txt # In-place edit
sed '/pattern/d' file.txt # Delete matching lines
sed '3d' file.txt # Delete line 3
sed '2,5d' file.txt # Delete lines 2-5
sed -n '10,20p' file.txt # Print lines 10-20
# awk: text analysis
awk '{print $1}' file.txt # Print column 1
awk -F',' '{print $2}' file.csv # Specify delimiter
awk 'NR>1' file.txt # Skip first line
awk '{sum+=$1} END{print sum}' file.txt # Sum
awk '$3>100' file.txt # Conditional filter
awk '{print NR, $0}' file.txt # Add line numbers
# cut: extract field
cut -d',' -f1,3 file.csv # Extract columns 1,3
cut -c1-10 file.txt # Extract chars 1-10
# sort & uniq
sort file.txt | uniq # Deduplicate
sort file.txt | uniq -c # Count
sort -rn file.txt # Numeric descending
sort -t',' -k2 file.csv # Sort by column 2
# tr: character translation
echo "HELLO" | tr 'A-Z' 'a-z' # To lowercase
cat file.txt | tr -d '\n' # Delete newlinesExplanation
grep search, sed replace, awk analyze, cut extract, sort/uniq sort and deduplicate are core text processing tools.