Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 38 ยท sed Basics

sed Options: -n, -e, -f, -E, -i, -s and -z

Learn every important sed option: -n, -e, -f, -E, -s, -z, -u, --posix and safe in-place editing with -i and backups, including a dry-run workflow for many files.

So far you have only passed sed a script and an input. Its options change how it reads, prints and writes. A handful of them cover nearly all real work. In this lesson you will learn them one at a time, and finish with the most important skill: editing files in place without losing data.

Overview

Option Long form Meaning
-n --quiet, --silent do not auto-print
-e script --expression add a script fragment (repeatable)
-f file --file read the script from a file
-E or -r --regexp-extended use ERE instead of BRE
-i[SUFFIX] --in-place edit files in place (GNU)
-s --separate treat each file separately (line numbers restart)
-z --null-data records end with NUL, not newline
-u --unbuffered flush output after every line
--posix disable GNU extensions to check portability

-n and multiple -e

You have used -n. The option -e lets you give several scripts; sed joins them in order, as if separated by newlines. You can also separate commands by a semicolon, but a few commands (such as a, i, c, r, w and labels) swallow the rest of the line, so use -e or newlines for them.

Bash Several -e fragments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

-E: extended regular expressions

-E (POSIX, also -r in GNU sed) switches to ERE, so + ? | () {} work without backslashes. Use it by default in new scripts.

Bash BRE versus -E

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

-f: the script from a file

Long scripts are easier to read in a file. Lines starting with # are comments. The special first line #n (alone) works like -n.

Bash A script file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

You can mix -f and -e; the fragments run in the order given. The next lessons use script files a lot, starting with sed scripts and recipes.

-s: files separately

By default, sed treats several input files as one long stream: line numbers and $ refer to the whole thing. With -s (GNU), each file starts over, so 1p prints the first line of every file and $p the last line of every file.

Bash -s and multiple files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

-z: records that are not lines

With -z, sed uses the NUL character as record separator. If your input has no NULs, the whole input is one record, so a regex can span newlines. That makes multi-line substitutions easy:

Bash -z treats the whole file as one record

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first command joins all lines with commas (it also converts the final newline). The second collapses runs of blank lines into one. -z is also what you use with find -print0 output.

In-place editing with -i

-i makes sed write the result back to the input file. There is no undo. Always use one of these safety nets:

  1. Give a suffix: sed -i.bak ... keeps the original as file.bak.
  2. Preview first: run the command without -i, check the output, then add -i.
  3. Keep the files under version control.
Bash -i, backups and a dry run

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

diff old new is the best review tool: it shows exactly what sed changed. For many files, loop and show diffs before committing to changes. (The GNU/BSD difference for -i is covered in Versions and Portability.)

sed -i replaces a file by writing a temporary file and renaming it. This changes the inode, can break hard links, and replaces symlinks with regular files unless you pass --follow-symlinks (GNU). It also never edits two files at once atomically. Do not run sed -i on files you cannot recreate.

In-place on many files

Combine find or a shell glob with -i:

Terminal
# preview
grep -rl 'oldname' src/
# change every match in .txt files, keeping .bak backups
find . -name '*.txt' -exec sed -i.bak 's/oldname/newname/g' {} +

Run the grep -rl first so you know which files will change.

-u and --posix

-u flushes output after each line, which matters in pipelines that must react immediately (for example tail -f log | sed -u ...). --posix disables GNU extensions and is a cheap way to see whether your script is portable.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the effect of sed -s -n '1p' a.txt b.txt?

    -s restarts line numbering for every file, so address 1 matches once per file.
  2. 2. Which command edits f.txt in place and keeps the original as f.txt.orig?

    The suffix is attached directly to -i.
  3. 3. What does sed -z 's/\n/,/g' do on input without NUL characters?

    With -z the separator is NUL, so all lines belong to one record that contains the newlines.
  4. 4. Which is the safest workflow before sed -i?

    Dry run, diff and a backup give you a clear picture and a way back.

Summary

  • -n stops auto-print; -e adds script fragments; -f reads a script file; -E selects ERE.
  • -s treats files separately; -z reads NUL-separated records (or the whole input at once).
  • -i[SUFFIX] edits in place; always preview, diff and keep a backup.
  • --posix helps you check portability.

Next: Capture Groups and Backreferences