SED
On this page
- Stream Editor
- Common Examples
- Replace First Occurrence
- Replace Nth Occurrence
- Replace All Occurrences
- Execute Mult Commands
- Using Command File
- Print Only Replaced Lines
- Write Result to File
- Modify Delimiter
- Specify Line Match
- Specify Command Group
- Delete All Lines
- Delete Specific Lines
- Delete Matched Lines
- Insert a Line Before
- Insert a Line After
- Specify Line Insert
- Matched Line Insert
- Insert Multiple Lines
- Modify Line
- Single Char Replace
- Print Specific Lines
- Print Before/After Replacement
- Print Line Number
- Reading from File
Stream Editor
A Stream Text Processor, authored by Lee E. McMahon.
Basic Syntax
sed [OPTIONS] 'command' file
OPTIONS
: Command options.command
: Print, replace, delete, etc.file
: The file to process; if omitted, reads from STDIN.
Omitting file
enters interactive mode, executing one line at a time.
Execution Process
- Reading a line of data:
- With a match rule:
- Match successful: Executes related operations.
- Match failed: Prints data as is.
- Without a match rule: Executes related operations.
- With a match rule:
Command Quotes
- Single quotes: Reduces the impact of escape characters, preferably used.
- Double quotes: Can use variable parameters, requires handling special characters.
Common Examples
Replace First Occurrence
Replace the first occurrence of ‘a’ with ‘b’.
echo 'aba' | sed 's/a/b/'
bba
Replace Nth Occurrence
Replace the Nth occurrence of ‘a’ with ‘b’.
echo 'aba' | sed 's/a/b/2'
abb
Replace All Occurrences
echo 'aba' | sed 's/a/b/g'
bbb
Execute Mult Commands
Can use ;
to separate, or use the -e
option.
echo 'aba' | sed 's/a/b/; s/a/c/'
echo 'aba' | sed -e 's/a/b/' -e 's/a/c/'
bbc
Using Command File
The cmd.sed
file content is as follows.
s/a/b/
s/a/c/
echo 'aba' | sed -f cmd.sed
bbc
Print Only Replaced Lines
-n
indicates suppressing output, p
indicates only outputting matched lines.
echo '
aa bb
cc dd
' | sed -n 's/aa/bb/p'
bb bb
Write Result to File
echo '
aa bb
cc dd
' | sed 's/aa/bb/w out.txt'
cat out.txt
bb bb
Modify Delimiter
Can use another symbol to replace the command delimiter /
.
echo '/bin/sh' | sed 's#/sh#/bash#'
/bin/bash
Specify Line Match
Match the 2nd line.
sed '2s/aa/bb/'
Match lines 2-4.
sed '2,4s/aa/bb/'
Match lines 2 to the last.
sed '2,$s/aa/bb/'
Specify Command Group
sed '2{s/cc/aa/; s/dd/bb/}'
sed '2,4{
s/cc/aa/
s/dd/bb/
}'
Delete All Lines
sed 'd'
Delete Specific Lines
sed '1d'
sed '2,4d'
sed '2,$d'
Delete Matched Lines
sed '/aa bb/d'
Insert a Line Before
echo "hello" | sed 'i\New Line'
Insert a Line After
echo "hello" | sed 'a\New Line'
Specify Line Insert
sed '3i\New Line'
Matched Line Insert
sed '/cc/i\New Line'
Insert Multiple Lines
Must use \
.
sed '2i\
New Line 1\
New Line 2
'
Modify Line
sed '2c\
Change Line 1\
Change Line 2
'
sed '/aa/c\
Change Line 1
'
Single Char Replace
echo 'aabbcc' | sed 'y/ac/ca/'
ccbbaa
Print Specific Lines
sed -n '2,5p'
Print Before/After Replacement
sed -n '/aa/{p; s/aa/cc/p}'
Print Line Number
sed -n '/bb/{=; p}'
Reading from File
Create foo and bar files.
echo -e 'aa\nbb' > foo
echo -e '11\n22' > bar
Read from the foo file and insert after the first line of bar.
sed '1r foo' bar
11
aa
bb
22
Match string then insert.
sed '/22/r foo' bar
11
22
aa
bb
Match string, insert, and use d
to delete the matched line.
sed '/22/{
r foo
d
}' bar
11
aa
bb
The following will report a syntax error.
# Will error
sed '/22/{r foo; d}' bar
It’s not impossible to do it in one line.
sed '/22/r foo' bar | sed '/22/d'