Sed Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。
Syntax
$ sed [options] command [input-file]
With pipeline
$ cat report.txt | sed 's/Nick/John/g'
$ echo '123abc' | sed 's/[0-9]+//g'
| Option | Example | Description |
|---|---|---|
-i | sed -ibak 's/On/Off/' php.ini | Backup and modify input file directly |
-E | sed -E 's/[0-9]+//g' input-file | Use extended regular expressions |
-n | sed -n '3 p' config.conf | Suppress default pattern space printing |
-f | sed -f script.sed config.conf | Execute sed script file |
-e | sed -e 'command1' -e 'command2' input-file | Execute multiple sed commands |
{.show-header}
$ echo "hello world" | sed -e 's/h/H/g' -e 's/w/W/g'
Hello World
Use -e to execute multiple sed commands
$ 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
$ 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
| Command | Example | Description |
|---|---|---|
p | sed -n '1,4 p' input.txt | Print lines 1-4 |
p | sed -n -e '1,4 p' -e '6,7 p' input.txt | Print lines 1-4 and 6-7 |
d | sed '1,4 d' input.txt | Print lines except 1-4 |
w | sed -n '1,4 w output.txt' input.txt | Write pattern space to file |
a | sed '2 a new-line' input.txt | Append line after |
i | sed '2 i new-line' input.txt | Insert line before |
{.show-header}
| Command | Description |
|---|---|
n | Print pattern space, empty pattern space, and read next line |
x | Swap pattern space with hold space |
h | Copy pattern space to hold space |
H | Append pattern space to hold space |
g | Copy hold space to pattern space |
G | Append hold space to pattern space |
See also: File spacing
$ sed 's/old/new/[flags]' [input-file]
| Flag | Description |
|---|---|
g | Global substitution |
1,2... | Substitute the nth occurrence |
p | Print only the substituted line |
w | Write only the substituted line to a file |
I | Ignore case while searching |
e | Substitute and execute in the command line |
| Command | Description |
|---|---|
b label | Branch to a label (for looping) |
t label | Branch to a label only on successful substitution<br>(for looping) |
:label | Label for the b and t commands (for looping) |
N | Append next line to pattern space |
P | Print 1st line in multi-line |
D | Delete 1st line in multi-line |
| Flag | Description |
|---|---|
| <code>/ | ^ @ ! #</code> | Substitution delimiter can be any character |
& | Gets the matched pattern |
( ) \1 \2 \3 | Group using ( and ).<br>Use \1, \2 in replacement to refer the group |
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 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
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
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 '$='
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
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
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;}'
地址
Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)Level 2, 171 La Trobe St, Melbourne VIC 3000四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号Business Hub, 155 Waymouth St, Adelaide SA 5000Disclaimer
JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.
匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议
© 2017-2025 JR Academy Pty Ltd. All rights reserved.
ABN 26621887572