Advanced Text Processing | sed, awk & tr | Mac/Linux Terminal
Video: Advanced Text Processing | sed, awk & tr | Mac/Linux Terminal by Taught by Celeste AI - AI Coding Coach
Watch full page →Advanced Text Processing with sed, awk & tr on Mac/Linux Terminal
Unlock powerful text manipulation techniques using the Mac/Linux terminal with three essential commands: sed for stream editing, awk for pattern scanning and data extraction, and tr for character translation. These tools enable efficient batch text replacement, structured data processing, and character-level transformations.
Code
# Stream editing with sed: replace all occurrences of 'world' with 'universe'
echo "hello world" | sed 's/world/universe/g'
# Replace only the first occurrence of 'foo' with 'bar' in a file (in-place editing)
sed -i '' 's/foo/bar/' filename.txt
# Print only lines containing 'error'
sed -n '/error/p' logfile.txt
# Delete lines matching 'DEBUG'
sed '/DEBUG/d' logfile.txt
# awk: print the first field of each line (default separator is whitespace)
awk '{print $1}' data.txt
# awk: use colon as field separator and print second field
awk -F: '{print $2}' /etc/passwd
# awk: print lines where the first field is greater than 80
awk '$1 > 80 {print $0}' scores.txt
# tr: convert lowercase letters to uppercase
echo "hello world" | tr 'a-z' 'A-Z'
# tr: delete all digits from input
echo "abc123def456" | tr -d '0-9'
# tr: squeeze multiple spaces into one
echo "this is spaced" | tr -s ' '
Key Points
sedperforms powerful text substitutions, deletions, and selective printing with support for regular expressions and in-place file editing.awkexcels at field-based processing, filtering, arithmetic, and formatting, making it ideal for structured text like CSV or system files.trtranslates, deletes, or squeezes characters on a per-character basis, perfect for simple character-level transformations in pipelines.- Combining these tools enables efficient automation of complex text processing tasks directly from the terminal.