Invoking sed From the Command Line
The simplest way to run sed is to pass a single quoted script as the first argument followed by zero or more filenames. Always wrap the script in single quotes so the shell does not expand special characters like $, *, or backslashes before sed ever sees them. If you give filenames, sed processes each in order; if you give none, it reads standard input, which is what makes 'cmd | sed ...' pipelines work.
Cricket analogy: Single-quoting the script is like a batsman calling for the sightscreen to be fixed before facing the ball — you settle the environment first so nothing interferes with the delivery you actually intended.
# One script, two files, output concatenated to stdout
sed 's/foo/bar/' file1.txt file2.txt
# Reading from a pipe (no filename)
cat access.log | sed 's/GET/POST/'
# Note the single quotes protecting the $ anchor from the shell
sed 's/error$/error!/' log.txtRunning Multiple Commands
Real edits often need more than one command. You have three ways to supply several: separate them with semicolons inside one script, give each its own -e option, or place them on separate lines within a single quoted block. All three are equivalent; sed applies every command to each line in the order written. The -e form is handy in shell scripts because each command reads clearly on its own line and is easy to comment around.
Cricket analogy: Chaining commands is like setting a full field placement — slip, gully, and mid-on each have a job, and every ball is judged against all of them together in the order you arranged them.
# Semicolon-separated
sed 's/red/green/; s/blue/yellow/' colors.txt
# Multiple -e options
sed -e 's/red/green/' -e 's/blue/yellow/' colors.txt
# Newline-separated inside one quoted block
sed '
s/red/green/
s/blue/yellow/
' colors.txtScript Files With -f
When a script grows long or you want to reuse it, put the commands in a file (conventionally with a .sed extension) and run it with -f scriptfile. Each command goes on its own line, comments begin with #, and no surrounding quotes are needed because the shell never sees the contents. This keeps complex transformations under version control and out of unwieldy one-liners.
Cricket analogy: A .sed script file is like a team's written match plan in the dressing room — captured, reusable, and reviewable, rather than tactics shouted ball by ball.
# clean.sed
# Remove comment lines and blank lines
/^#/d
/^$/d
s/[ \t]*$// # strip trailing whitespace
# Run it:
sed -f clean.sed config.iniTwo other everyday options: -n suppresses sed's automatic printing (pair it with the p command to print only what you want), and -i edits files in place. GNU sed accepts an optional suffix with -i (e.g. -i.bak) to save a backup of the original.
BSD/macOS sed and GNU sed differ on -i. GNU accepts -i with no argument; BSD requires an explicit suffix argument, so 'sed -i ...' on macOS errors unless you write 'sed -i ...' as "sed -i '' ...". Portable scripts should account for this.
- Wrap sed scripts in single quotes to protect them from shell expansion.
- Give filenames to process files, or none to read standard input.
- Combine commands with semicolons, multiple -e options, or newlines.
- Store reusable or long scripts in a file and run them with -f.
- Use -n to suppress automatic printing and -i to edit files in place.
- GNU and BSD sed differ in -i syntax; write portable scripts carefully.
Practice what you learned
1. Why should a sed script be wrapped in single quotes?
2. Which option runs a sed script stored in a separate file?
3. What does the -n option do?
4. Which of these correctly chains two substitutions in one script?
5. What is a portability concern with the -i option?
Was this page helpful?
You May Also Like
What Is sed?
An introduction to sed, the non-interactive stream editor that transforms text line by line as it flows through a pipeline.
sed Substitution Basics
A practical guide to the s command — sed's find-and-replace engine — covering delimiters, flags, backreferences, and the ampersand.
Your First sed One-Liner
Build a genuinely useful sed one-liner from scratch, combining an address, a substitution, and options into a single practical command.
Addresses and Line Selection
How to target specific lines in sed using line numbers, regex patterns, ranges, and the negation operator so commands run only where you want.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics