Ever spent an afternoon cleaning a CSV with thousands of rows? Or extracting data from a system log miles long? If you manage servers, e-commerce, or even just database backups via terminal, sed and awk are the two Swiss army knives that save you hours every week. Here at Meteora Web, we use them daily to manipulate configs, transform inventory exports, and clean data before importing into WooCommerce or our custom ERPs. In this guide, we'll show exactly how to use them, with examples that work right away.
How does sed solve the problem of massive file modification on Linux?
Sed (stream editor) is a non-interactive editor. You don't open the file in nano or vim: you tell it "find this string and replace it" and it does it across the entire file in a fraction of a second. Perfect for repetitive changes on hundreds of config files, logs, or structured data.
Practical example: replace a string across all config files
Suppose you need to change the PHP version in all Apache virtual hosts on a server with dozens of sites. With sed:
Sponsored Protocol
# Replace 'php7.4' with 'php8.1' in all .conf files recursively
find /etc/apache2/sites-available -name "*.conf" -exec sed -i 's/php7\.4/php8.1/g' {} \;
The -i flag edits the file in-place. The pattern uses a literal dot \. to avoid matching any character.
Delete rows from a CSV without opening it
Imagine an ERP export with header lines and totals you need to remove. With sed:
# Delete the first line (header) and lines starting with 'Total'
sed -i '1d;/^Total/d' export.csv
1d deletes line 1; /^Total/d deletes any line starting with "Total".
What is awk used for when processing structured files and logs?
Awk is a programming language designed for extracting and processing tabular data. If you have a file with columns (spaces, tabs, commas), awk lets you analyze, filter, aggregate, and transform that data like a mini SQL database from the terminal.
Real example: analyze server logs to find the most frequent IPs
# Extract first field (IP) from access log, count occurrences, sort by frequency
awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head -10
Each log line is split into fields by whitespace. $1 is the first field. Then we pipe to sort, count, and sort again. With one e-commerce client, we discovered a bot scanning products thousands of times a day — we blocked it in 5 minutes.
Sponsored Protocol
Calculate total value of a warehouse CSV with awk
A clothing store we work with had a CSV with columns: article, quantity, price. They wanted the total warehouse value. With awk:
# CSV format: name,qty,price
awk -F',' '{ total += $2 * $3 } END { print "Total warehouse value: " total " EUR" }' warehouse.csv
-F',' sets comma as field separator. The variable total accumulates row by row. The END block runs after the last line.
Which to choose between sed and awk for a text processing task on Linux?
There's no single answer. We think in terms of complexity and structure:
- Choose sed for simple substitutions or line deletions based on a linear text pattern. Sed is faster and has minimal syntax.
- Choose awk when your data has a field structure (CSV, TSV, logs), or when you need calculations, comparisons, or aggregations. Awk is more expressive and handles multiple conditions easily.
We often combine them: clean with sed the raw format, then extract with awk. For example, to prepare a Google Shopping feed from an ERP export, we first use sed to remove control characters and headers, then awk to format columns according to the required schema.
Sponsored Protocol
How to combine sed and awk in a bash script for server automation
The classic scenario: every night a server receives a CSV report via FTP, must be processed and loaded into a database. Here's a minimal script that runs the entire pipeline:
#!/bin/bash
# Nightly processing script
INPUT="/home/data/raw_report.csv"
OUTPUT="/home/data/cleaned_report.csv"
# Remove empty lines and header lines with sed
sed -i '/^$/d;/^#/d' "$INPUT"
# Extract only columns 1,3,5 with awk and add header
awk -F',' 'BEGIN { print "ID,Quantity,Price" } { print $1 "," $3 "," $5 }' "$INPUT" > "$OUTPUT"
echo "Processed report: $OUTPUT"
Drop it into a cron job and you have an automated flow, no manual work. We manage product feeds for several online stores exactly like this: sed removes impurities, awk normalizes fields.
Sponsored Protocol
Common mistakes when using sed and awk (and how to avoid them)
1. Forgetting backup. The -i flag in sed overwrites the original file. Always test without -i (prints to screen). Or use -i.bak to auto-create a backup:
sed -i.bak 's/old/new/g' file.conf
2. Misinterpreting special characters. In sed, . means "any character". For a literal dot you must escape it: \.. In awk, backslashes need to be doubled inside patterns. Always test on a sample line.
3. Forgetting the field separator in awk. By default awk splits on spaces and tabs. For CSV use -F',' or -F';'. For Apache logs, the separator is whitespace, so $1, $2 etc. work directly.
Sponsored Protocol
4. Not using environment variables in scripts. If you need to pass a variable value (e.g. a date) to sed or awk, use double quotes for expansion:
DATE="2026-01-15"
awk -F',' -v d="$DATE" '$1 == d { print }' log.csv
The -v flag passes the shell variable into awk.
What to do now
- Try sed on a test file: take a log or CSV, do a selective substitution without
-ito see the output. - Extract with awk the first 10 lines of a log and count HTTP status codes (e.g. 200, 404) — you'll quickly spot Error pages that Google reports.
- Automate a repetitive task: write a bash script that nightly cleans an ERP export and saves it ready for import.
- Check the official documentation: GNU sed manual and GNU awk manual.
- Read our main guide: Shell Scripting and Linux Automation for more background.
Remember: a well-placed command today saves you hours tomorrow. And if a colleague says "I just open it manually in Excel," smile and send them this guide.