Module 7: Text Processing
Text processing is a crucial skill for Bash scripting. It allows you to search, manipulate, and analyze textual data efficiently.
This module covers tools like grep, cut, sort, uniq, paste, sed, awk, and basic regular expressions.
✅ Searching Text with grep
#!/bin/bash
# Search for the word "error" in log.txt
grep "error" log.txt
# Case-insensitive search
grep -i "warning" log.txt
# Show line numbers
grep -n "failed" log.txt
✅ Cutting Columns with cut
#!/bin/bash
# Extract first and third column (delimiter is :)
cut -d ":" -f 1,3 /etc/passwd
✅ Sorting and Unique Lines
#!/bin/bash
# Sort file.txt alphabetically
sort file.txt
# Get unique lines
sort file.txt | uniq
# Count unique lines
sort file.txt | uniq -c
✅ Merging Files with paste
#!/bin/bash
# Combine two files column-wise
paste file1.txt file2.txt
✅ Editing Text with sed
sed 's/old/new/g'→ Replace textsed -n '2,5p'→ Print specific linessed '/pattern/d'→ Delete matching lines
#!/bin/bash
# Replace "apple" with "orange" in file.txt
sed 's/apple/orange/g' file.txt
✅ Parsing Data with awk
#!/bin/bash
# Print 1st and 3rd columns of file.txt
awk '{print $1, $3}' file.txt
# Sum the values in column 2
awk '{sum+=$2} END {print "Total:", sum}' file.txt
✅ Regular Expressions (Basic & Extended)
- Basic regex in grep:
grep "^[a-z]" file.txt→ Lines starting with lowercase letters - Extended regex:
grep -E "error|warning" file.txt
✅ Summary
grep→ Search patterns in filescut→ Extract columns or fieldssort&uniq→ Organize and count unique datapaste→ Merge files horizontallysed→ Stream editor for text replacement & manipulationawk→ Powerful data parsing and processing tool- Regular expressions allow complex pattern matching for text processing
✅ By the end of this module, you can efficiently search, manipulate, and process text files in Bash scripts.
