sodcalculator

10 Tech How-To

Linux sed Command Tips and Tricks for Everyday Editing

Guide 10-41086 · Updated · Step by step

Linux sed is a stream editor for making text changes from the command line, especially substitutions, deletions, and selective printing. The most useful tips are to test commands without changing files, quote expressions carefully, and account for differences between GNU sed on Linux and other implementations.

What sed Does and How Its Commands Work

sed reads input one line at a time, applies an editing script, and normally writes the result to standard output. It does not usually alter the source file: that makes it useful in pipelines and a relatively safe way to preview a transformation.

A basic substitution has this form:

sed 's/old/new/' file.txt

The s command substitutes text; the first slash-delimited field is the pattern and the second is the replacement. By default, only the first match on each line is replaced. Add g to replace every match on a line:

sed 's/old/new/g' file.txt

Most sed scripts use single quotes in a shell command. This prevents the shell from interpreting characters such as $ and backticks before sed receives them. The pattern language is a regular expression, not always a literal string; characters such as ., *, and [ can have special meanings.

Useful Substitution Tips

  • Choose another delimiter when the pattern contains slashes. For example, sed 's|/usr/local/bin|/opt/tools|g' file.txt avoids escaping each slash. Any suitable delimiter can be used consistently in the substitution.
  • Limit a change to a line range. sed '5,10s/draft/final/g' file.txt changes matches only on lines 5 through 10. A range can also use patterns: sed '/BEGIN/,/END/s/old/new/g' file.txt.
  • Use an address to target a line. sed '3s/cat/dog/' file.txt applies the substitution only to line 3. Addresses may be line numbers or regular expressions.
  • Remember replacement backreferences. In basic regular expressions, parentheses and plus signs generally need backslashes to act as operators. A captured group can be reused in the replacement: sed 's/\([0-9][0-9]*\)-\([0-9][0-9]*\)/\2:\1/' swaps two digit groups separated by a hyphen.

For more advanced expressions, GNU sed supports extended regular expressions with -E, which lets you write grouping parentheses without backslashes. This changes the pattern syntax, so keep scripts consistent with the selected mode.

Print, Delete, and Select Lines

Use -n to suppress the normal output, then the p command to print only what you select. For example, sed -n '10,20p' file.txt prints lines 10 through 20. To show lines matching a pattern, use sed -n '/ERROR/p' app.log.

The d command deletes selected lines from sed’s output; it does not delete them from the input file. For instance, sed '/^#/d' settings.txt omits lines beginning with a hash. Combine a range and deletion to remove a block of output, or use !d to delete lines that do not match an address. Be careful with negation: it often reverses which lines are retained.

These commands are handy for quick inspection, but they are not a substitute for a parser when working with structured formats. A simple regular expression can miss quoting, escaped delimiters, nested syntax, or multiline records in formats such as JSON and CSV.

Use sed in Pipelines and Scripts

Because sed reads standard input when no filename is supplied, it fits naturally into pipelines:

printf '%s\n' 'status=old' | sed 's/status=old/status=new/'

It can also transform command output before another tool receives it. Keep each pipeline stage focused: sed is strongest at line-oriented editing, while tools such as grep, cut, and awk may be clearer for selection, field handling, or calculations.

For a reusable script, put commands in a file and pass it with -f, such as sed -f edits.sed input.txt. This is easier to read than a long, heavily escaped shell command. Add comments in a script where the purpose of a non-obvious regular expression might otherwise be unclear.

Editing Files Safely and Portably

To change a file in place with GNU sed, use -i, but make a backup first or request a suffix: sed -i.bak 's/old/new/g' file.txt. Inspect the result and the backup before removing anything. In-place editing is not a transaction; a mistake in the pattern or file selection can affect many lines quickly.

The spelling and behavior of -i differ between GNU sed and the BSD sed commonly found on macOS. A command written for Linux may therefore fail elsewhere, particularly around backup suffix arguments. For portable workflows, write transformed output to a temporary file, verify it, and then replace the original using appropriate shell or system tools. Do not assume a sed command copied from a web page is portable without checking its target implementation.

Common Problems and How to Avoid Them

  • Only the first match changes: add the g flag if every match on each line should be replaced.
  • The pattern matches too much or too little: inspect regular-expression metacharacters and test against a small sample. Escape a character when it should be literal.
  • The shell changes the command: use single quotes for the sed script when possible. If a literal apostrophe is required inside single quotes, shell quoting becomes more complex; consider a script file.
  • A replacement includes an unexpected character: in many sed implementations, & in the replacement expands to the entire matched text. Escape it as \& to insert a literal ampersand.
  • A multiline operation behaves unexpectedly: sed’s default pattern space is one line. Multi-line commands exist, but for complex records a dedicated parser or another tool is often easier to maintain.

Before applying a destructive change, run the command without -i and review its output. Test boundary cases too: an empty line, a line with multiple matches, and text that nearly resembles the target can reveal mistakes.

Frequently Asked Questions

Does sed change the original file?
Not by default. It writes transformed text to standard output. An in-place option such as GNU sed’s -i changes the file directly, so preview and back up first.

How do I replace every occurrence in a file?
Use the substitution’s global flag: sed 's/old/new/g' file.txt. The flag applies to every match on each line.

How do I replace text only on a specific line?
Put a line-number address before the command, as in sed '4s/old/new/' file.txt. A pattern address can target lines by their contents.

Can sed edit multiple files?
Yes. You can provide multiple input filenames, but in-place editing behavior and options vary by implementation. Test on copies, especially when portability matters.

Why does my Linux sed command fail on macOS?
Linux commonly uses GNU sed, while macOS includes BSD sed. Options such as -i have different syntax. Check the local manual page or use a portable approach that writes to a separate output file.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →