The sed
command (short for Stream Editor) is a powerful text manipulation tool in Linux/Unix. It is used to perform basic text transformations on an input stream (a file or input from a pipeline). sed
is commonly used for tasks such as search-and-replace, text insertion, deletion, and more.
sed 'command' filename
- command: The operation
sed
should perform. - filename: The file on which the
sed
operation is to be applied.
The most common use of sed
is for search and replace, using the s
command. The basic syntax is:
sed 's/old_text/new_text/' filename
- This will replace the first occurrence of
old_text
withnew_text
in each line.
sed 's/hello/world/' file.txt
- This replaces the first occurrence of "hello" with "world" in each line of
file.txt
.
To replace all occurrences of a pattern in a line, use the g
flag:
sed 's/old_text/new_text/g' filename
sed 's/hello/world/g' file.txt
- This replaces every occurrence of "hello" with "world" in each line of
file.txt
.
To modify the file directly without needing to redirect the output to a new file, use the -i
option:
sed -i 's/old_text/new_text/' filename
- This changes the file
filename
in place.
sed -i 's/hello/world/g' file.txt
- This will replace every occurrence of "hello" with "world" directly in
file.txt
.
You can delete lines that match a specific pattern using the d
command.
sed '/pattern/d' filename
- This will delete any line containing the word
pattern
.
sed '/error/d' file.txt
- This deletes all lines that contain the word "error" in
file.txt
.
To print only specific lines (using line numbers), use the -n
option in conjunction with the p
command:
sed -n '5p' filename
- This will print the 5th line of the file
filename
.
sed -n '3,6p' file.txt
- This will print lines 3 through 6 from
file.txt
.
You can also replace text only on a specific line or range of lines by specifying the line number:
sed '3s/old_text/new_text/' filename
- This will replace
old_text
withnew_text
only on the 3rd line.
sed '2,4s/hello/world/' file.txt
- This will replace "hello" with "world" in lines 2 through 4 of
file.txt
.
You can insert a line before or after a matching line with the i
(insert) or a
(append) commands.
-
Insert before a line:
sed '/pattern/i\new_line' filename
- This inserts
new_line
before every line containingpattern
.
- This inserts
sed '/error/i\This is an error message' file.txt
-
This inserts "This is an error message" before every line containing "error".
-
Append after a line:
sed '/pattern/a\new_line' filename
- This appends
new_line
after every line containingpattern
.
- This appends
sed '/error/a\Please check this error' file.txt
- This appends "Please check this error" after every line containing "error".
To change a line, you can use the c
command:
sed '3c\new_line' filename
- This changes the 3rd line of the file to
new_line
.
sed '3c\This is the new line' file.txt
- This changes the 3rd line to "This is the new line."
sed
supports regular expressions (regex) for more advanced pattern matching.
-
Match any character:
sed 's/./X/' file.txt
- This replaces the first character of each line with "X".
-
Match one or more digits:
sed 's/[0-9]\+/NUMBER/' file.txt
- This replaces one or more digits with the word "NUMBER".
You can combine multiple sed
commands using the -e
option or by separating commands with semicolons.
sed -e 's/old_text/new_text/' -e 's/foo/bar/' filename
- This applies two commands in sequence: replacing
old_text
withnew_text
andfoo
withbar
.
sed 's/hello/world/; s/foo/bar/' file.txt
- This replaces "hello" with "world" and "foo" with "bar" in the same pass.
-
Replace "cat" with "dog" in a file:
sed 's/cat/dog/' file.txt
-
Replace all occurrences of "cat" with "dog":
sed 's/cat/dog/g' file.txt
-
Delete lines that contain "error":
sed '/error/d' file.txt
-
Print lines 1 to 5:
sed -n '1,5p' file.txt
-
Insert "This is a new line" before every line containing "pattern":
sed '/pattern/i\This is a new line' file.txt
-
Change line 2 to "This is a changed line":
sed '2c\This is a changed line' file.txt
-
Replace digits with the word "number":
sed 's/[0-9]\+/number/' file.txt
-
Replace only on the 3rd line:
sed '3s/old_text/new_text/' file.txt
-
In-place editing:
sed -i 's/old_text/new_text/' file.txt
Command | Description | Example |
---|---|---|
s/old_text/new_text/ |
Substitute old_text with new_text |
sed 's/cat/dog/' file.txt |
d |
Delete lines matching a pattern | sed '/error/d' file.txt |
i\new_line |
Insert new_line before lines matching a pattern |
sed '/error/i\This is an error' file.txt |
a\new_line |
Append new_line after lines matching a pattern |
sed '/error/a\Please check this error' file.txt |
c\new_line |
Change lines to new_line |
sed '3c\New line' file.txt |
-i |
Edit the file in place (without creating a new file) | sed -i 's/cat/dog/' file.txt |
-n |
Suppress automatic output, useful for selective printing | sed -n '3p' file.txt |
g |
Global replacement (replace all occurrences in a line) | sed 's/cat/dog/g' file.txt |
sed
is a powerful and efficient tool for text manipulation in Unix-like systems. It is particularly useful for batch processing, scripting, and automation tasks involving text files.