$

SED Syntax

$ sed [options]'command' file

SED (Stream Editor) is a powerful text processing tool essential for Linux server administration and log file analysis. Perfect for processing configuration files and automating text transformations.

Options
Flags like -n, -i, -e
Command
Actions like s, d, p, a, i
File
Target file to process

SED Options

Option Description Example
-n Suppress automatic printing of pattern space $ sed -n'/banana/p' data.txt
-i Edit file in-place (backup created if extension provided) $ sed -i's/banana/orange/' data.txt
-e Execute multiple sed commands $ sed -e'command1' -e'command2' input-file
-f Read sed commands from a script file $ sed -f script.sed data.txt
-r or -E Use extended regular expressions $ sed -E's/(word1|word2)/match/' file.txt

SED Commands

Command Description Example
d Delete the current pattern space (line) $ echo"Hello, World!" | sed's/World!/World/'
s Used for substitution $ echo"Hello, World!" | sed's/World!/Universe/'
i Insert text before the current line $ echo"Hello!" | sed'i Hello'
a Append text after the current line $ echo"Hello." | sed'a World!'
r Append a read from a file $ echo"Hello." | sed'r additional.txt'
c Change (replace) text $ echo"Hello, World!" | sed'c Goodbye'
p Print the current pattern space $ sed -n'/pattern/p' file.txt
q Quit the sed command at the specified point $ sed'2q' data.txt
y Transform characters (like tr command) $ echo"hello" | sed'y/helo/HELO/'
n Read next input line $ sed'n;d' file.txt

SED Flags (for substitution command)

Flag Description Example
g Globally replace all occurrences of a pattern in the current line $ echo"Hello, hello, hello!" | sed's/hello/Hi/g'
p Print only the substituted line (use with -n) $ sed -n's/pattern/&/p' data.txt
d Delete the pattern space (current line) $ sed'/pattern/d' file.txt
i Case-insensitive matching $ sed's/hello/Hi/i' file.txt
w Write the pattern space to a file $ sed -n's/pattern/&/w output.txt' data.txt
1,2,3... Replace the Nth occurrence only $ echo"hello hello hello" | sed's/hello/Hi/2'

Address Patterns

Addresses specify which lines the sed command should act upon. You can use line numbers, patterns, or ranges.

Single Line Number

$ sed'3d' file.txt # Delete line 3

Line Range

$ sed'2,5d' file.txt # Delete lines 2 to 5
$ sed'2,5p' file.txt # Print lines 2 to 5

Pattern Matching

$ sed'/pattern/d' file.txt # Delete lines matching pattern
$ sed'/^$/d' file.txt # Delete empty lines

Pattern Range

$ sed'/start/,/end/d' file.txt # Delete from start to end pattern

Last Line

$ sed'$d' file.txt # Delete last line
$ sed'$p' file.txt # Print last line

Negation (NOT)

$ sed'/pattern/!d' file.txt # Delete lines NOT matching pattern

Regex Metacharacters

Metacharacter Description
. Match any character except new line
? Match 0 or one characters
* Match 0 or more characters
+ Match 1 or more characters
^ Beginning of line
$ End of line
[abc] Match any character in the set (a, b, or c)
[^abc] Match any character NOT in the set
| Alternation (OR operator)
\< Start of word
\> End of word
\( \) Group expressions (backreferences)
\n Backreference (nth captured group)

Common Examples

Replace Text (Basic Substitution)

$ sed's/old/new/' file.txt # Replace first occurrence per line
$ sed's/old/new/g' file.txt # Replace all occurrences

Delete Lines

$ sed'/pattern/d' file.txt # Delete lines matching pattern
$ sed'/^$/d' file.txt # Delete empty lines
$ sed'/^#/d' file.txt # Delete comment lines

Tip: Combine SED with grep for powerful log analysis workflows.

Print Specific Lines

$ sed -n'5p' file.txt # Print line 5
$ sed -n'10,20p' file.txt # Print lines 10 to 20
$ sed -n'/pattern/p' file.txt # Print lines matching pattern

Multiple Commands

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

In-Place Editing

$ sed -i's/old/new/g' file.txt # Edit file directly (Linux)
$ sed -i.bak's/old/new/g' file.txt # Create backup with .bak extension

Insert and Append Text

$ sed'3i\New line here' file.txt # Insert before line 3
$ sed'3a\New line here' file.txt # Append after line 3

Backreferences (Capture Groups)

$ sed's/\(.*\)@\(.*\)/\2 - \1/' emails.txt # Swap email parts
$ sed's/\([0-9]\{3\}\)-\([0-9]\{4\}\)/(\1) \2/' phone.txt

Case Conversion (GNU sed)

$ sed's/.*/\U&/' file.txt # Convert to uppercase
$ sed's/.*/\L&/' file.txt # Convert to lowercase

Remove Leading/Trailing Whitespace

$ sed's/^[ \t]*//' file.txt # Remove leading whitespace
$ sed's/[ \t]*$//' file.txt # Remove trailing whitespace
$ sed's/^[ \t]*//; s/[ \t]*$//' file.txt # Remove both

Number Lines

$ sed'=' file.txt | sed'N;s/\n/\t/' # Add line numbers