Text Processing with grep, sed, and awk

Tabla de Contenidos

Linux provides powerful tools for processing text. Master these, and you can manipulate data with incredible efficiency.

grep - Pattern Matching

grep searches for patterns in text:

grep "error" logfile.txt
grep -r "pattern" /path/to/dir    # Recursive search
grep -i "case" file.txt            # Case insensitive
grep -v "exclude" file.txt         # Invert match

sed - Stream Editor

sed performs text transformations:

sed 's/old/new/g' file.txt         # Replace all occurrences
sed -n '5,10p' file.txt             # Print lines 5-10
sed '/pattern/d' file.txt           # Delete matching lines

awk - Pattern Processing

awk is a programming language for text processing:

awk '{print $1}' file.txt          # Print first field
awk -F: '{print $1}' /etc/passwd   # Use colon as delimiter
awk '$3 > 1000 {print $1}' file    # Conditional printing

Combining Tools

The real power comes from combining these tools:

# Find all error lines, extract timestamps, count occurrences
grep "ERROR" app.log | awk '{print $1}' | sort | uniq -c

# Replace IP addresses in config files
sed -i 's/192\.168\.1\.1/10.0.0.1/g' *.conf

Practical Examples

# Extract email addresses from a file
grep -oE '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' file.txt

# Count lines in all Python files
find . -name "*.py" -exec wc -l {} + | awk '{sum+=$1} END {print sum}'

These tools form the foundation of command-line text processing. Practice combining them!

¿Te gusta este contenido?

Si este artículo te fue útil, considera invitarme un café. ¡Tu apoyo ayuda a mantener este sitio!

Prerequisitos

Siguientes Rutas

Rutas Alternativas

Artículos Relacionados