| name | text-processing |
| description | Production-grade text processing - grep, sed, awk, regex |
| sasmp_version | 1.3.0 |
| bonded_agent | 02-text-processing |
| bond_type | PRIMARY_BOND |
| version | 2.0.0 |
| difficulty | intermediate |
| estimated_time | 6-8 hours |
Text Processing Skill
Master text manipulation with grep, sed, awk, and regular expressions
Learning Objectives
After completing this skill, you will be able to:
Prerequisites
- Bash basics (variables, control flow)
- Command line navigation
- Understanding of stdin/stdout
Core Concepts
1. Grep Essentials
grep 'pattern' file.txt
grep -i 'pattern' file.txt
grep -v 'pattern' file.txt
grep -n 'pattern' file.txt
grep -c 'pattern' file.txt
grep -E 'pat1|pat2' file.txt
grep -E '^start.*end$' file.txt
grep -r 'pattern' ./
grep -rn --include='*.py' 'def ' ./
2. Sed Essentials
sed 's/old/new/' file
sed 's/old/new/g' file
sed -i 's/old/new/g' file
sed -n '5p' file
sed '5d' file
sed '/pattern/d' file
sed -e 's/a/b/' -e 's/c/d/' file
3. Awk Essentials
awk '{print $1}' file
awk -F: '{print $1}' file
awk '{print $NF}' file
awk '/pattern/' file
awk '$3 > 100' file
awk '{sum+=$1} END{print sum}' file
awk 'NR>1 {total++} END{print total}' file
4. Regex Quick Reference
.
^
$
*
+
?
[abc]
[^abc]
[a-z]
\d
\w
\s
Common Patterns
Log Analysis
awk '{print $1}' access.log | sort | uniq -c | sort -rn
grep -E 'ERROR|FATAL' app.log | tail -20
grep 'ERROR' app.log | sed 's/.*\[\([^]]*\)\].*/\1/'
Data Transformation
sed 's/,/\t/g' data.csv
grep -oP '"name":\s*"\K[^"]+' data.json
sed '/^$/d' file.txt
Anti-Patterns
| Don't | Do | Why |
|---|
cat file | grep | grep pattern file | Useless use of cat |
| Multiple sed calls | Single sed with -e | Reduces overhead |
grep -E ".*" | Omit if not needed | Slower with regex |
Practice Exercises
- Log Parser: Extract top 10 IPs from access log
- CSV Filter: Filter CSV rows by column value
- Config Editor: Update config values with sed
- Report Generator: Summarize data with awk
Troubleshooting
Common Errors
| Error | Cause | Fix |
|---|
Invalid regex | Bad pattern | Escape special chars |
No match | Wrong case | Use -i flag |
sed delimiter | / in pattern | Use # or | |
Debug Techniques
echo "test" | sed -n 's/\(.*\)/\1/p'
awk '{print NR, NF, $0}' file
Performance Tips
rg 'pattern' --type py
LC_ALL=C grep 'pattern' file
grep -m 10 'pattern' file
Resources