ErrorFixHub

Sed Command in Linux/Unix: 25+ Practical Examples for Text Processing

Master the sed command with 25+ practical examples. Learn find-and-replace, line deletion, insertion, and advanced text transformations in Linux/Unix.

CC++

Manually editing a 10,000-line log file to fix a formatting error? The sed command can do it in a single line. I've lost count of how many times this stream editor has rescued me from what would otherwise be hours of tedious, error-prone manual editing. Whether you're a system administrator wrangling config files or a developer cleaning up generated output, sed is one of those tools that, once mastered, becomes almost an extension of your hands.

This isn't going to be a dry rehash of the man page. Instead, I'm going to walk you through the sed command the way I actually use it—task by task, with real examples you can adapt immediately. We'll cover everything from basic find-and-replace to some genuinely tricky advanced transformations, and I'll point out the gotchas that tripped me up when I was learning (like the time I accidentally wiped a config file's comments with a poorly-planned -i flag—more on that later).

By the end, you'll have a solid mental model of how sed works, plus a toolbox of 25+ practical examples. Let's dive in.


Close-up of a glowing laptop keypad with digital interface, representing futuristic technology.

What is the sed Command? Understanding the Stream Editor

At its core, the sed command (short for stream editor) is a Unix utility that reads text input line by line, applies a series of transformations, and writes the result to output. The key word here is stream—sed processes data as a continuous flow, not as a file you open and save.

How sed Works: A One-Pass Text Processing Engine

Think of sed as a conveyor belt in a factory. Text goes in on one end, gets processed by a series of stations (your commands), and comes out the other end transformed. It makes exactly one pass over the input, which makes it incredibly efficient for large files.

Here's the basic flow:

Input Stream (file or pipe) --> [sed: read line into pattern space] --> [apply commands] --> Output Stream

Sed maintains two memory buffers:

  • Pattern space: The current line being processed. This is where all the action happens.
  • Hold space: A temporary storage area. You can copy or append pattern space content here, then retrieve it later. This is essential for multi-line operations.

This single-pass design is what distinguishes sed from interactive editors like ed or vi. Those tools load the entire file into memory and require you to navigate to specific locations. Sed, by contrast, never looks back—it processes line 1, outputs it, moves to line 2, and so on. This makes it perfect for pipelines, where data flows from one command to another without ever touching disk.

Sed vs. Awk vs. Grep: Choosing the Right Tool

One of the most common questions I get from students is: "When do I use sed versus awk versus grep?" It's a fair question—the three tools overlap in some areas, but each has its sweet spot.

ToolPrimary Use CaseBest ForExample
grepSearching and filteringFinding lines that match a patterngrep "error" app.log
sedSimple text transformationsFind-and-replace, line deletion, insertionsed 's/foo/bar/g' file.txt
awkComplex data processing and reportingField extraction, calculations, formatted outputawk '{print $1, $3}' data.txt
Here's a practical comparison. Suppose you have a file with lines like John,25,Engineer and you want to extract just the names.
  • grep can find lines containing "John" but can't isolate the name field.
  • sed can do it with a regex: sed 's/,.*//' file.txt (removes everything after the first comma).
  • awk makes it trivial: awk -F',' '{print $1}' file.txt.

My rule of thumb: if you're searching, use grep. If you're doing simple substitutions or line-based edits, use sed. If you need to process fields, calculate values, or generate reports, use awk. And when you're in doubt, remember that these tools compose beautifully—you can pipe grep into sed into awk to build powerful one-liners.


Colorful PHP code displayed on a dark screen, ideal for programming themes.

Sed Command Syntax and Essential Options

Before we get to the examples, let's make sure the fundamentals are solid. The sed command syntax is straightforward once you understand its structure.

Basic Syntax: sed [options] 'script' [input-file]

The general form is:

sed [options] 'script' [input-file]
  • options: Flags that modify sed's behavior (we'll cover the key ones below).
  • script: The actual sed commands, usually enclosed in single quotes.
  • input-file: The file to process. If omitted, sed reads from standard input.

Here's the simplest possible example:

cat file.txt | sed 's/foo/bar/'

This reads file.txt, replaces the first occurrence of "foo" with "bar" on each line, and writes the result to standard output. Note that the original file is unchanged—sed is a filter, not an editor (unless you use -i, which we'll get to).

Key Options: -n, -i, -e, and -E

These four options cover 90% of what you'll use in daily work:

-n (suppress automatic printing)

By default, sed prints every line after processing. The -n flag suppresses this, so only lines explicitly printed with the p command appear:

sed -n '3p' file.txt   # prints only line 3

-i (in-place editing)

This is where sed becomes dangerous and powerful. The -i flag modifies the file directly instead of writing to standard output:

sed -i 's/foo/bar/g' file.txt   # modifies file.txt in place

You can also specify a backup suffix:

sed -i.bak 's/foo/bar/g' file.txt   # creates file.txt.bak before editing

Pro tip: Always use -i.bak the first few times you edit a critical file. I learned this the hard way when I ran a sed command on a production config file without a backup and realized my regex was wrong. The file was unrecoverable.

-e (multiple scripts)

Use -e to chain multiple sed commands in a single invocation:

sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt

-E (extended regular expressions)

By default, sed uses basic regular expressions (BRE), where characters like +, ?, and | have literal meanings. The -E flag switches to extended regular expressions (ERE), which are more intuitive:

sed -E 's/[0-9]+/NUMBER/g' file.txt   # with -E, + means "one or more"

Without -E, you'd need to write [0-9]\+ instead.


Sed Command Examples: 25+ Practical Use Cases

Now we get to the meat of this article. I've organized these sed command examples by task type, so you can jump to what you need.

Find and Replace: The s Command

The s (substitute) command is the workhorse of sed. Here are the variations I use most:

Basic replacement (first occurrence per line):

$ echo "hello world" | sed 's/world/universe/'
hello universe

Global replacement (all occurrences per line):

$ echo "foo foo foo" | sed 's/foo/bar/g'
bar bar bar

Replacing on a specific line number:

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

This replaces "old" with "new" only on line 3.

Replacing within a line range:

$ sed '2,5s/old/new/g' file.txt

This applies the replacement to lines 2 through 5.

Using regular expressions:

$ echo "Order #12345 shipped" | sed -E 's/#[0-9]+/#[XXXXX]/'
Order #XXXXX shipped

Handling special characters in replacement:

The & character in the replacement refers to the entire matched pattern:

$ echo "hello" | sed 's/hello/(&)/'
(hello)

And \1 through \9 refer to captured groups:

$ echo "John Smith" | sed -E 's/([A-Za-z]+) ([A-Za-z]+)/\2, \1/'
Smith, John

Changing the delimiter:

If your pattern contains slashes, you can use a different delimiter:

$ echo "/usr/local/bin" | sed 's|/usr|/opt|'
/opt/local/bin

Deleting Lines: The d Command

The d command deletes lines from the output. Here are the patterns I use regularly:

Delete a specific line:

$ sed '3d' file.txt

Delete a range of lines:

$ sed '2,5d' file.txt

Delete lines matching a pattern:

$ sed '/DEBUG/d' app.log

This removes all lines containing "DEBUG".

Delete empty lines:

$ sed '/^$/d' file.txt

Delete the last line:

$ sed '$d' file.txt

Delete everything except lines matching a pattern (using !):

$ sed '/keep this/d' file.txt   # deletes lines with "keep this"
$ sed '/keep this/!d' file.txt  # deletes lines WITHOUT "keep this"

Inserting and Appending Text: The i and a Commands

The i (insert) and a (append) commands add text before or after a line, respectively.

Insert text before line 3:

$ sed '3i\This is inserted text' file.txt

Append text after line 3:

$ sed '3a\This is appended text' file.txt

Insert based on a pattern match:

$ sed '/ERROR/i\--- Error detected ---' app.log

Insert a line before the last line:

$ sed '$i\This goes before the last line' file.txt

Multi-line insertion:

$ sed '3i\Line one\
Line two\
Line three' file.txt

Printing Lines: The p Command and the -n Option

The p command prints the pattern space. Combined with -n, it becomes a precise line selector.

Print specific line numbers:

$ sed -n '3p' file.txt

Print a range of lines:

$ sed -n '2,5p' file.txt

Print the first 10 lines (like head):

$ sed -n '1,10p' file.txt

Print lines matching a pattern (like grep):

$ sed -n '/error/p' app.log

Print lines 10 through the end of the file:

$ sed -n '10,$p' file.txt

Advanced Text Transformations

These are the examples that make people look at me funny when I show them. They're powerful, but they require a bit more explanation.

Replace newlines with commas:

This is a classic. The default behavior of sed is line-based, so replacing newlines requires a multi-line approach:

$ sed ':a;N;$!ba;s/\n/,/g' file.txt

Let me break this down:

  • :a creates a label named "a"
  • N appends the next line to the pattern space (with an embedded newline)
  • $!ba branches to label "a" unless we're on the last line ($)
  • s/\n/,/g then replaces all newlines with commas

Replace multiple spaces with a single comma:

$ echo "John   25   Engineer" | sed -E 's/\s+/,/g'
John,25,Engineer

Extract the last 5 characters of each line:

$ echo "abcdefghij" | sed -E 's/.*(.{5})/\1/'
fghij

Transliterate characters with the y command:

$ echo "hello" | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'
HELLO

The y command maps each character in the first set to the corresponding character in the second set.

Comment out lines that don't start with #:

$ sed -E 's/^([^#])/# \1/' config.conf

This prepends # to any line that doesn't start with #.

Print only lines 5 through 10, but skip line 7:

$ sed -n '5,10p;7d' file.txt

Wait, that's not quite right. Let me correct that:

$ sed -n '5,10{/pattern/d;p}' file.txt

This prints lines 5-10 but deletes any that match "pattern".


GNU sed vs. BSD sed: Key Differences and Compatibility

If you've ever moved a sed script from Linux to macOS, you've probably hit a wall. The sed command in Unix environments comes in two main flavors: GNU sed (default on Linux) and BSD sed (default on macOS). They're mostly compatible, but the differences will bite you when you least expect it.

macOS (BSD) vs. Linux (GNU) sed

Here's a comparison table of the key differences:

FeatureGNU sed (Linux)BSD sed (macOS)
-i option-i or -i.bak-i '' or -i.bak (suffix required)
Extended regex-E or -r-E only
\n in replacementWorksDoesn't work (literal n)
\t in regexWorksDoesn't work (literal t)
--debug optionAvailableNot available
The most notorious difference is the -i flag. On GNU sed, you can write:
sed -i 's/foo/bar/g' file.txt

But on BSD sed, this will fail. You need:

sed -i '' 's/foo/bar/g' file.txt

The empty string '' tells BSD sed that you don't want a backup file. If you want a backup, you'd write -i.bak.

Portable script example:

Here's a script that works on both platforms:


if [[ "$(uname)" == "Darwin" ]]; then
    sed -i.bak 's/foo/bar/g' file.txt
else
    sed -i.bak 's/foo/bar/g' file.txt
fi

Actually, that's the same command—-i.bak works on both. The problem only arises when you use -i without a suffix on macOS.

Another difference: \n in the replacement part of s

On GNU sed, this works:

echo "hello" | sed 's/hello/hello\nworld/'

Output:

hello
world

On BSD sed, you get literal n:

hellonworld

The portable way to do this is to use an actual newline in the replacement:

echo "hello" | sed 's/hello/hello\
world/'

This works on both platforms.


Common Sed Errors and How to Debug Them

Even after 15 years, I still occasionally hit errors with sed. Here are the most common ones and how to fix them.

Troubleshooting 'unterminated s' command' and Other Errors

"unterminated `s' command"

This is the most common error, and it usually means your delimiters are unbalanced:

$ sed 's/foo/bar' file.txt
sed: -e expression #1, char 10: unterminated `s' command

The fix is to close the command with a final delimiter:

sed 's/foo/bar/' file.txt

"unknown option to `s'"

This happens when you use a flag that sed doesn't recognize:

$ sed 's/foo/bar/x' file.txt
sed: -e expression #1, char 11: unknown option to `s'

The fix is to use valid flags (g, p, w, or a number).

"extra characters after command"

This occurs when you have stray characters after a command:

$ sed '3d extra' file.txt
sed: -e expression #1, char 5: extra characters after command

Debugging with --debug (GNU sed only)

GNU sed has a --debug option that shows you exactly what's happening:

$ sed --debug 's/foo/bar/' file.txt
SED PROGRAM:
  s/foo/bar/
INPUT:   'file.txt' line 1
PATTERN: hello foo world
MATCHED: s/foo/bar/
PATTERN: hello bar world

This is invaluable when your script isn't doing what you expect.

Common mistakes checklist:

  1. Unbalanced delimiters: Always count your / characters.
  2. Missing escape for special characters: In BRE, +, ?, |, (, ), {, } need backslashes. Use -E to avoid this.
  3. Wrong address range: Remember that 1,5 means lines 1 through 5, not "line 1 and line 5".
  4. Forgetting -n with p: If you use p without -n, you'll get duplicate lines.
  5. Using -i without a backup: Always use -i.bak for critical files.

Frequently Asked Questions

How do I use the sed command to replace a string in a file?

Start with the basic syntax: sed 's/old/new/' file. This replaces the first occurrence of "old" with "new" on each line. To replace all occurrences, add the g flag: sed 's/old/new/g' file. To modify the file directly, use -i: sed -i 's/old/new/g' file. For example, to replace all instances of "localhost" with "127.0.0.1" in a config file:

sed -i 's/localhost/127.0.0.1/g' /etc/hosts

What is the difference between sed and awk?

Sed is a stream editor designed for simple text transformations—find-and-replace, line deletion, insertion. It processes text line by line and is ideal for quick, scriptable edits. Awk, on the other hand, is a full-fledged programming language. It excels at field extraction, pattern matching, and generating formatted reports. For example, to extract the second column from a CSV file, awk makes it trivial: awk -F',' '{print $2}' data.csv. Sed would require a more complex regex. In short: use sed for simple edits, awk for data processing.

How to delete a specific line number using sed?

Use the d command with a line number address: sed 'Nd' file, where N is the line number. For example, to delete line 5:

sed '5d' file.txt

To delete a range of lines, use sed 'M,Nd' file:

sed '5,10d' file.txt

This deletes lines 5 through 10.

How to use sed to replace a string with a newline character?

The \n in the replacement part of the s command is not interpreted as a newline by default—this is a common gotcha. On GNU sed, you can use a loop to replace newlines:

sed ':a;N;$!ba;s/\n/,/g' file.txt

This reads the entire file into the pattern space, then replaces all newlines with commas. On BSD sed, you'd need to use an actual newline in the replacement:

sed 's/foo/foo\
bar/' file.txt

Conclusion

The sed command is one of those Unix tools that rewards investment. The learning curve is real—regular expressions alone can take weeks to internalize—but the payoff is enormous. I've used sed to clean up multi-gigabyte log files, transform data exports, automate config file edits across hundreds of servers, and even generate code. It's not the only tool in the text-processing toolbox, but it's often the right one.

Start with the basics: master s, d, p, and the -n and -i options. Then gradually incorporate more advanced techniques like hold space operations and multi-line patterns. And when you hit a wall, remember that the GNU sed manual is excellent, and the --debug option is your friend.

The examples in this article are meant to be a starting point, not a destination. Take them, adapt them to your own problems, and see what you can build. If you're looking for a quick reference, I've put together a free sed command cheat sheet (PDF) with all the examples from this article—you can download it [here] and keep it handy for those moments when you need to remember whether it's -i or -i '' on macOS.

Now go forth and process some text. Your future self will thank you.

Related Posts