sed

text processinglinux
The sed command is one of the most frequently used commands in Linux/Unix-like operating systems. sed The sed command (stream editor) is used for parsing and transforming text files line by line. It's a powerful tool for text manipulation that performs operations like search, find and replace, insertion, and deletion on text streams.

Quick Reference

Command Name:

sed

Category:

text processing

Platform:

linux

Basic Usage:

sed [options] [arguments]

Common Use Cases

  • 1

    Text editing

    Perform search and replace operations on text data

  • 2

    Data transformation

    Transform and modify data in various formats

  • 3

    Scripting

    Use in shell scripts to edit text data programmatically

  • 4

    Configuration files

    Modify configuration files automatically

Syntax

sed [OPTION]... {script} [input-file]...
sed [OPTION]... -f script-file [input-file]...

Options

Option Description
-n, --quiet, --silent Suppress automatic printing of pattern space
-e script, --expression=script Add the script to the commands to be executed
-f script-file, --file=script-file Add the contents of script-file to the commands to be executed
-i[SUFFIX], --in-place[=SUFFIX] Edit files in place (makes backup if SUFFIX supplied)
-l N, --line-length=N Specify the desired line-wrap length for the 'l' command
-E, -r, --regexp-extended Use extended regular expressions in the script
-s, --separate Consider files as separate rather than as a single, continuous long stream
--follow-symlinks Follow symlinks when processing in place
-z, --null-data Separate lines by NULL characters
--help Display a help message and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the sed command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

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

Replaces the first occurrence of "old" with "new" on each line of file.txt. This is the most basic substitution form of sed, using the 's' command for substitution.

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

Replaces all occurrences of "old" with "new" in file.txt. The 'g' flag (global) makes sed replace all matches, not just the first one on each line.

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

Edits file.txt in-place, replacing all occurrences of "old" with "new". The -i option modifies the file directly instead of printing to standard output.

sed '5d' file.txt

Deletes the 5th line from file.txt. The 'd' command indicates deletion, and the '5' is an address that specifies which line to operate on.

Advanced Examples:

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

Prints only lines 5 through 10 from file.txt. The -n option suppresses default printing, and the 'p' command prints the lines in the specified range.

sed '/^#/d' config.txt

Deletes all lines that start with # in config.txt. This uses a regular expression (^#) to match lines beginning with a hash character, which is useful for removing comments from configuration files.

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

Replaces all occurrences of "old" with "new" in file.txt, ignoring case. The 'I' flag makes the pattern matching case-insensitive.

sed '1i\Header line' file.txt

Inserts "Header line" before the first line of file.txt. The 'i' command is used for insertion, and '1' is the address specifying where to insert.

sed '/pattern/a\Append after' file.txt

Appends "Append after" after each line containing "pattern" in file.txt. The 'a' command appends text, and '/pattern/' is an address that matches lines containing the specified pattern.

sed 's/[0-9]\+/NUMBER/g' file.txt

Replaces all sequences of digits with "NUMBER" in file.txt. This uses a regular expression with character classes to match one or more digits.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Sed Command Structure:

  • Sed scripts follow the format: [address[,address]][!]command [arguments]
  • Addresses can be line numbers, regular expressions, or special symbols like $ (last line)
  • Commands are single-letter operations like 's' (substitute), 'd' (delete), 'p' (print), etc.
  • Multiple commands can be combined with semicolons or -e options

Common Sed Commands:

  • s - Substitute: s/pattern/replacement/[flags]
  • d - Delete: remove lines matching address
  • p - Print: print lines matching address (commonly used with -n)
  • i - Insert: add text before matching lines
  • a - Append: add text after matching lines
  • c - Change: replace entire lines with new text
  • y - Transform: change characters (like tr command)
  • q - Quit: exit sed after processing up to current match

Substitution Flags:

  • g - Global: replace all occurrences (not just the first)
  • I - Ignore case: match pattern case-insensitively
  • p - Print: print the modified line (usually with -n)
  • w file - Write: write the modified line to a file
  • n - Nth occurrence: replace only the nth match on each line

Address Types:

  • Line number: 5d (delete line 5)
  • Line range: 5,10d (delete lines 5-10)
  • Regular expression: /pattern/d (delete lines matching pattern)
  • Line & regex combined: 5,/pattern/d (delete from line 5 to pattern)
  • Special address $: $d (delete last line)
  • Step value: 1~2d (delete odd lines)

Regular Expression Support:

  • Basic regular expressions are supported by default
  • Extended regular expressions are enabled with -E or -r option
  • Common patterns: ^ (line start), $ (line end), . (any char), * (zero or more), [ ] (character class)
  • Use \ to escape special characters like \(, \), \{, \} in basic regex

Common Use Cases:

  • Find and replace text in files
  • Extract specific lines from files
  • Delete lines matching patterns (like comments, empty lines)
  • Add text before or after specific lines
  • Process configuration files or log files
  • Convert file formats

Related Commands:

  • awk - More powerful text processing language
  • grep - Search for text patterns
  • tr - Translate or delete characters
  • cut - Extract fields from lines
  • perl - Perl programming language with powerful text manipulation

Tips & Tricks

1

Use the -i option to edit files in-place

2

Use the -r option to use extended regular expressions

3

Use the -n option to suppress automatic printing of pattern space

4

Use the -e script option to add multiple scripts

5

Use the -f script-file option to add scripts from a file

Common Use Cases

Text editing

Perform search and replace operations on text data

Data transformation

Transform and modify data in various formats

Scripting

Use in shell scripts to edit text data programmatically

Configuration files

Modify configuration files automatically

Text processing

Manipulate text data in pipelines and scripts

Related Commands

These commands are frequently used alongside sed or serve similar purposes:

Use Cases

1

Text editing

Perform search and replace operations on text data

2

Data transformation

Transform and modify data in various formats

3

Scripting

Use in shell scripts to edit text data programmatically

4

Configuration files

Modify configuration files automatically

5

Text processing

Manipulate text data in pipelines and scripts

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the sed command works in different scenarios.

$ sed
View All Commands