Text Processing with Sed and Awk
Uses sed for stream editing and awk for field-based processing to transform, filter, and extract data from text files and command output.
Code
#!/usr/bin/env bash
set -euo pipefail
# Create sample data
cat > /tmp/data.csv << "EOF"
name,age,city,salary
Alice,30,New York,75000
Bob,25,Boston,65000
Charlie,35,Chicago,85000
Diana,28,Seattle,70000
EOF
# sed: substitute and transform
echo "=== Sed Operations ==="
sed "s/Boston/London/" /tmp/data.csv
# Delete lines matching pattern
sed "/^$/d; /^#/d" /tmp/data.csv
# Print specific lines
sed -n "2,4p" /tmp/data.csv
# Multiple commands
sed -e "s/Alice/Alicia/" -e "s/Bob/Robert/" /tmp/data.csv
# awk: field processing
echo "=== Awk Operations ==="
awk -F, "{print \$1, \$4}" /tmp/data.csv
# Filter and format
awk -F, "$3 == "Chicago" {print \$1, "earns \$" \$4}" /tmp/data.csv
# Compute totals
awk -F, "NR > 1 {total += \$4; count++}
END {printf "Average: \$%.2f\n", total/count}" /tmp/data.csv
# Conditional processing
awk -F, "NR > 1 {"
" if (\$4 > 70000) category = "High""
" else if (\$4 > 60000) category = "Medium""
" else category = "Low""
" printf "%-10s %s\n", \$1, category"
"}" /tmp/data.csv
# Group by city
awk -F, "NR > 1 {"
" cities[\$3]++"
" salaries[\$3] += \$4"
}"
END {"
" for (city in cities) {"
" printf "%-12s count=%d avg=\$%.0f\n","
" city, cities[city], salaries[city]/cities[city]"
" }"
"}" /tmp/data.csv
rm -f /tmp/data.csvWhat the code does
set -euo pipefail removes the shell’s usual optimism — errors abort, empty variables abort, hidden pipeline failures abort.
The work is delegated to cat, sed, awk and rm, chained so each stage’s output feeds the next.
Statements that matter most here
cat > /tmp/data.csv << "EOF"
awk -F, "NR > 1 {total += \$4; count++}
awk -F, "NR > 1 {"
Interactive Tools for This Topic
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- Does the script stop on the first error?
- Yes — `set -euo pipefail` aborts on the first failing command, treats unset variables as errors, and fails the whole pipeline if any stage fails. Nothing continues half-broken.
- Which external commands does the script rely on?
- `cat`, `sed`, `awk`, `rm` must be available on the PATH environment variable; everything else is shell built-in syntax.
Report a problem with this exampleOpens a pre-filled issue on GitHub.