100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Running sed Scripts

How to invoke sed from the command line and from script files, including the key options that control input, output, and multi-command execution.

FoundationsBeginner9 min readJul 10, 2026
Analogies

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.

bash
# 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.txt

Running 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.

bash
# 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.txt

Script 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.

bash
# clean.sed
# Remove comment lines and blank lines
/^#/d
/^$/d
s/[ \t]*$//   # strip trailing whitespace

# Run it:
sed -f clean.sed config.ini

Two 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

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#RunningSedScripts#Running#Sed#Scripts#Invoking#StudyNotes#SkillVeris#ExamPrep