logo

Sed Cheat Sheet


title: Sed date: 2020-12-27 18:34:08 background: bg-red-400 tags: - editor - replace - text - utility categories: - Linux Command intro: | Sed is a stream editor, this sed cheat sheet contains sed commands and some common sed tricks. plugins: - copyCode

Getting Started

Sed Usage

Syntax

$ sed [options] command [input-file]

With pipeline

$ cat report.txt | sed 's/Nick/John/g'
$ echo '123abc' | sed 's/[0-9]+//g'

Option Examples {.col-span-2}

OptionExampleDescription
-ised -ibak 's/On/Off/' php.iniBackup and modify input file directly
-Esed -E 's/[0-9]+//g' input-fileUse extended regular expressions
-nsed -n '3 p' config.confSuppress default pattern space printing
-fsed -f script.sed config.confExecute sed script file
-esed -e 'command1' -e 'command2' input-fileExecute multiple sed commands

{.show-header}

Multiple commands

$ echo "hello world" | sed -e 's/h/H/g' -e 's/w/W/g'
Hello World

Use -e to execute multiple sed commands

Sed script

$ echo 's/h/H/g' >> hello.sed
$ echo 's/w/W/g' >> hello.sed
$ echo "hello world" | sed -f hello.sed
Hello World

Use -f to execute sed script file

Examples

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

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

See: Sed examples

Sed commands

Commands {.col-span-2}

CommandExampleDescription
psed -n '1,4 p' input.txtPrint lines 1-4
psed -n -e '1,4 p' -e '6,7 p' input.txtPrint lines 1-4 and 6-7
dsed '1,4 d' input.txtPrint lines except 1-4
wsed -n '1,4 w output.txt' input.txtWrite pattern space to file
ased '2 a new-line' input.txtAppend line after
ised '2 i new-line' input.txtInsert line before

{.show-header}

Space commands

CommandDescription
nPrint pattern space, empty pattern space, and read next line
xSwap pattern space with hold space
hCopy pattern space to hold space
HAppend pattern space to hold space
gCopy hold space to pattern space
GAppend hold space to pattern space

See also: File spacing

Flags

$ sed 's/old/new/[flags]' [input-file]

FlagDescription
gGlobal substitution
1,2...Substitute the nth occurrence
pPrint only the substituted line
wWrite only the substituted line to a file
IIgnore case while searching
eSubstitute and execute in the command line

Loops commands

CommandDescription
b labelBranch to a label (for looping)
t labelBranch to a label only on successful substitution
(for looping)
:labelLabel for the b and t commands (for looping)
NAppend next line to pattern space
PPrint 1st line in multi-line
DDelete 1st line in multi-line

Misc Flags

FlagDescription
/ | ^ @ ! #Substitution delimiter can be any character
&Gets the matched pattern
( ) \1 \2 \3Group using ( and ).
Use \1, \2 in replacement to refer the group

Sed examples

Replacing text {.row-span-2}

Replace all occurrences of a string

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

Replace only the nth occurrence of a string

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

Replace a string only on the 5th line

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

Replace "world" with "universe" but only if the line begins with "hello"

$ sed '/hello/s/world/universe/' file.txt

Remove "" from the end of each line

$ sed 's/\\$//' file.txt

Remove all whitespace from beginning of each line

$ sed 's/^\s*//' file.txt

Remove comments. Even those that are at the end of a line

$ sed 's/#.*$//' file.txt

Search for text

Search for a string and only print the lines that were matched

$ sed -n '/hello/p' file.txt

Case insensitive search

$ sed -n '/hello/Ip' file.txt

Search for a string but only output lines that do not match

$ sed -n '/hello/!p' file.txt

Appending lines

Append line after line 2

$ sed '2a Text after line 2' file.txt

Append line at the end of the file

$ sed '$a THE END!' file.txt

Append line after every 3rd line starting from line 3

$ sed '3~3a Some text' file.txt

Numbering {.col-span-2}

Number line of a file (simple left alignment)

$ sed = file.txt | sed 'N;s/\n/\t/'

Number line of a file (number on left, right-aligned)

$ sed = file.txt | sed 'N; s/^/   /; s/ *\(.\{6,\}\)\n/\1  /'

Number line of file, but only print numbers if line is not blank

$ sed '/./=' file.txt | sed '/./N; s/\n/ /'

Count lines (emulates "wc -l")

$ sed -n '$='

Prepending lines

Insert text before line 5

$ sed '5i line number five' file.txt

Insert "Example: " before each line that contains "hello"

$ sed '/hello/i Example: ' file.txt

Deleting lines

Delete line 5-7 in file

$ sed '5,7d' file.txt

Delete every 2nd line starting with line 3

$ sed '3~2d' file.txt

Delete the last line in file

$ sed '$d' file.txt

Delete lines starting with "Hello"

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

Delete all empty lines

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

Delete lines starting with "#"

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

File spacing

Double space

$ sed G

Delete all blank lines and double space

$ sed '/^$/d;G'

Triple space a file

$ sed 'G;G'

Undo double-spacing

$ sed 'n;d'

Insert a blank line above line which matches "regex"

$ sed '/regex/{x;p;x;}'

Insert a blank line below line which matches "regex"

$ sed '/regex/G'

Insert a blank line around line which matches "regex"

$ sed '/regex/{x;p;x;G;}'

Also see {.cols-1}

🐧 Linux 命令

Sed

Sed Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · Linux 命令🧭 Markdown 速查🏷️ 2 个标签
#sed#text
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

Sed Usage

Syntax

SHELL
滚动查看更多
$ sed [options] command [input-file]

With pipeline

SHELL
滚动查看更多
$ cat report.txt | sed 's/Nick/John/g'
SHELL
滚动查看更多
$ echo '123abc' | sed 's/[0-9]+//g'
Option Examples
OptionExampleDescription
-ised -ibak 's/On/Off/' php.iniBackup and modify input file directly
-Esed -E 's/[0-9]+//g' input-fileUse extended regular expressions
-nsed -n '3 p' config.confSuppress default pattern space printing
-fsed -f script.sed config.confExecute sed script file
-esed -e 'command1' -e 'command2' input-fileExecute multiple sed commands

{.show-header}

Multiple commands
SHELL
滚动查看更多
$ echo "hello world" | sed -e 's/h/H/g' -e 's/w/W/g'
Hello World

Use -e to execute multiple sed commands

Sed script
SHELL
滚动查看更多
$ echo 's/h/H/g' >> hello.sed
$ echo 's/w/W/g' >> hello.sed
$ echo "hello world" | sed -f hello.sed
Hello World

Use -f to execute sed script file

Examples
SHELL
滚动查看更多
$ sed 's/old/new/g' file.txt
$ sed 's/old/new/g' file.txt > new.txt

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

See: Sed examples

Sed commands

Commands
CommandExampleDescription
psed -n '1,4 p' input.txtPrint lines 1-4
psed -n -e '1,4 p' -e '6,7 p' input.txtPrint lines 1-4 and 6-7
dsed '1,4 d' input.txtPrint lines except 1-4
wsed -n '1,4 w output.txt' input.txtWrite pattern space to file
ased '2 a new-line' input.txtAppend line after
ised '2 i new-line' input.txtInsert line before

{.show-header}

Space commands
CommandDescription
nPrint pattern space, empty pattern space, and read next line
xSwap pattern space with hold space
hCopy pattern space to hold space
HAppend pattern space to hold space
gCopy hold space to pattern space
GAppend hold space to pattern space

See also: File spacing

Flags
SHELL
滚动查看更多
$ sed 's/old/new/[flags]' [input-file]

FlagDescription
gGlobal substitution
1,2...Substitute the nth occurrence
pPrint only the substituted line
wWrite only the substituted line to a file
IIgnore case while searching
eSubstitute and execute in the command line
Loops commands
CommandDescription
b labelBranch to a label (for looping)
t labelBranch to a label only on successful substitution<br>(for looping)
:labelLabel for the b and t commands (for looping)
NAppend next line to pattern space
PPrint 1st line in multi-line
DDelete 1st line in multi-line
Misc Flags
FlagDescription
<code>/ | ^ @ ! #</code>Substitution delimiter can be any character
&Gets the matched pattern
( ) \1 \2 \3Group using ( and ).<br>Use \1, \2 in replacement to refer the group

Sed examples

Replacing text

Replace all occurrences of a string

SHELL
滚动查看更多
$ sed 's/old/new/g' file.txt

Replace only the nth occurrence of a string

SHELL
滚动查看更多
$ sed 's/old/new/2' file.txt

Replace a string only on the 5th line

SHELL
滚动查看更多
$ sed '5 s/old/new/' file.txt

Replace "world" with "universe" but only if the line begins with "hello"

SHELL
滚动查看更多
$ sed '/hello/s/world/universe/' file.txt

Remove "" from the end of each line

SHELL
滚动查看更多
$ sed 's/\\$//' file.txt

Remove all whitespace from beginning of each line

SHELL
滚动查看更多
$ sed 's/^\s*//' file.txt

Remove comments. Even those that are at the end of a line

SHELL
滚动查看更多
$ sed 's/#.*$//' file.txt
Search for text

Search for a string and only print the lines that were matched

SHELL
滚动查看更多
$ sed -n '/hello/p' file.txt

Case insensitive search

SHELL
滚动查看更多
$ sed -n '/hello/Ip' file.txt

Search for a string but only output lines that do not match

SHELL
滚动查看更多
$ sed -n '/hello/!p' file.txt
Appending lines

Append line after line 2

SHELL
滚动查看更多
$ sed '2a Text after line 2' file.txt

Append line at the end of the file

SHELL
滚动查看更多
$ sed '$a THE END!' file.txt

Append line after every 3rd line starting from line 3

SHELL
滚动查看更多
$ sed '3~3a Some text' file.txt
Numbering

Number line of a file (simple left alignment)

SHELL
滚动查看更多
$ sed = file.txt | sed 'N;s/\n/\t/'

Number line of a file (number on left, right-aligned)

SHELL
滚动查看更多
$ sed = file.txt | sed 'N; s/^/   /; s/ *\(.\{6,\}\)\n/\1  /'

Number line of file, but only print numbers if line is not blank

SHELL
滚动查看更多
$ sed '/./=' file.txt | sed '/./N; s/\n/ /'

Count lines (emulates "wc -l")

SHELL
滚动查看更多
$ sed -n '$='
Prepending lines

Insert text before line 5

SHELL
滚动查看更多
$ sed '5i line number five' file.txt

Insert "Example: " before each line that contains "hello"

SHELL
滚动查看更多
$ sed '/hello/i Example: ' file.txt
Deleting lines

Delete line 5-7 in file

SHELL
滚动查看更多
$ sed '5,7d' file.txt

Delete every 2nd line starting with line 3

SHELL
滚动查看更多
$ sed '3~2d' file.txt

Delete the last line in file

SHELL
滚动查看更多
$ sed '$d' file.txt

Delete lines starting with "Hello"

SHELL
滚动查看更多
$ sed '/^Hello/d' file.txt

Delete all empty lines

SHELL
滚动查看更多
$ sed '/^$/d' file.txt

Delete lines starting with "#"

SHELL
滚动查看更多
$ sed '/^#/d' file.txt
File spacing

Double space

SHELL
滚动查看更多
$ sed G

Delete all blank lines and double space

SHELL
滚动查看更多
$ sed '/^$/d;G'

Triple space a file

SHELL
滚动查看更多
$ sed 'G;G'

Undo double-spacing

SHELL
滚动查看更多
$ sed 'n;d'

Insert a blank line above line which matches "regex"

SHELL
滚动查看更多
$ sed '/regex/{x;p;x;}'

Insert a blank line below line which matches "regex"

SHELL
滚动查看更多
$ sed '/regex/G'

Insert a blank line around line which matches "regex"

SHELL
滚动查看更多
$ sed '/regex/{x;p;x;G;}'

Also see

相关 Cheat Sheets