#sed
##tldr
sh
sed -i "[range]s/[patern]/[replacement]/[options]" file_*
sed -i "2,5s|[patern]|[replacement]|[options]" file_*###Search for text
Search for a string and only print the lines that were matched
sh
$ sed -n '/hello/p' file.txtCase insensitive search
sh
$ sed -n '/hello/Ip' file.txt###Sed Usage
Syntax
shell
$ sed [options] command [input-file]With pipeline
sh
$ cat report.txt | sed 's/Nick/John/g'sh
$ echo '123abc' | sed 's/[0-9]+//g'###Option Examples
| 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 |
###Multiple commands
sh
$ echo "hello world" | sed -e 's/h/H/g' -e 's/w/W/g'
Hello WorldUse -e to execute multiple sed commands
###Sed script
sh
$ echo 's/h/H/g' >> hello.sed
$ echo 's/w/W/g' >> hello.sed
$ echo "hello world" | sed -f hello.sed
Hello WorldUse -f to execute sed script file
###Examples
sh
$ 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###Sed Usage
Syntax
shell
$ sed [options] command [input-file]With pipeline
sh
$ cat report.txt | sed 's/Nick/John/g'sh
$ echo '123abc' | sed 's/[0-9]+//g'###Option Examples
| 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 |
###Multiple commands
sh
$ echo "hello world" | sed -e 's/h/H/g' -e 's/w/W/g'
Hello WorldUse -e to execute multiple sed commands
###Sed script
sh
$ echo 's/h/H/g' >> hello.sed
$ echo 's/w/W/g' >> hello.sed
$ echo "hello world" | sed -f hello.sed
Hello WorldUse -f to execute sed script file
###Examples
sh
$ 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##Sed commands
###Commands
| 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 |
###Space commands
| 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 |
###Flags
sh
$ 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 |
###Loops commands
| Command | Description |
|---|---|
b label |
Branch to a label (for looping) |
t label |
Branch to a label only on successful substitution (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 |
###Misc Flags
| Flag | Description |
|---|---|
/ | ^ @ ! # |
Substitution delimiter can be any character |
& |
Gets the matched pattern |
( ) \1 \2 \3 |
Group using ( and ).Use \1, \2 in replacement to refer the group |
##Sed examples
###Replacing text
Replace all occurrences of a string
sh
$ sed 's/old/new/g' file.txtReplace only the nth occurrence of a string
sh
$ sed 's/old/new/2' file.txtReplace a string only on the 5th line
sh
$ sed '5 s/old/new/' file.txtReplace “world” with “universe” but only if the line begins with “hello”
sh
$ sed '/hello/s/world/universe/' file.txtRemove “" from the end of each line
sh
$ sed 's/\\$//' file.txtRemove all whitespace from beginning of each line
sh
$ sed 's/^\s*//' file.txtRemove comments. Even those that are at the end of a line
sh
$ sed 's/#.*$//' file.txt###Search for text
Search for a string and only print the lines that were matched
sh
$ sed -n '/hello/p' file.txtCase insensitive search
sh
$ sed -n '/hello/Ip' file.txtSearch for a string but only output lines that do not match
sh
$ sed -n '/hello/!p' file.txt###Appending lines
Append line after line 2
sh
$ sed '2a Text after line 2' file.txtAppend line at the end of the file
sh
$ sed '$a THE END!' file.txtAppend line after every 3rd line starting from line 3
sh
$ sed '3~3a Some text' file.txt###Numbering
Number line of a file (simple left alignment)
sh
$ sed = file.txt | sed 'N;s/\n/\t/'Number line of a file (number on left, right-aligned)
sh
$ sed = file.txt | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /'Number line of file, but only print numbers if line is not blank
sh
$ sed '/./=' file.txt | sed '/./N; s/\n/ /'Count lines (emulates “wc -l”)
sh
$ sed -n '$='###Prepending lines
Insert text before line 5
sh
$ sed '5i line number five' file.txtInsert “Example: " before each line that contains “hello”
sh
$ sed '/hello/i Example: ' file.txt###Deleting lines
Delete line 5-7 in file
sh
$ sed '5,7d' file.txtDelete every 2nd line starting with line 3
sh
$ sed '3~2d' file.txtDelete the last line in file
sh
$ sed '$d' file.txtDelete lines starting with “Hello”
sh
$ sed '/^Hello/d' file.txtDelete all empty lines
sh
$ sed '/^$/d' file.txtDelete lines starting with “#”
sh
$ sed '/^#/d' file.txt###File spacing
Double space
sh
$ sed GDelete all blank lines and double space
sh
$ sed '/^$/d;G'Triple space a file
sh
$ sed 'G;G'Undo double-spacing
sh
$ sed 'n;d'Insert a blank line above line which matches “regex”
sh
$ sed '/regex/{x;p;x;}'Insert a blank line below line which matches “regex”
sh
$ sed '/regex/G'Insert a blank line around line which matches “regex”
sh
$ sed '/regex/{x;p;x;G;}'##Also see
-
sed cheatsheet (gist.github.com)
本文由 简悦 SimpRead 转码, 原文地址 quickref.me Sed is a stream editor, this sed cheat sheet contains sed commands and some common sed tricks.
###Replacing text
Replace all occurrences of a string
TEXT
$ sed 's/old/new/g' file.txtReplace only the nth occurrence of a string
TEXT
$ sed 's/old/new/2' file.txtReplace replace a string only on the 5th line
TEXT
$ sed '5 s/old/new/' file.txtReplace “world” with “universe” but only if the line begins with “hello”
TEXT
$ sed '/hello/s/world/universe/' file.txtRemove "” from the end of each line
TEXT
$ sed 's/\\$//' file.txtRemove all whitespace from beginning of each line
TEXT
$ sed 's/^\s*//' file.txtRemove comments. Even those that are at the end of a line
TEXT
$ sed 's/#.*$//' file.txt###Search for text
Search for a string and only print the lines that were matched
TEXT
$ sed -n '/hello/p' file.txtCase insensitive search
TEXT
$ sed -n '/hello/Ip' file.txtSearch for a string but only output lines that do not match
TEXT
$ sed -n '/hello/!p' file.txt###Appending lines
Append line after line 2
TEXT
$ sed '2a Text after line 2' file.txtAppend line at the end of the file
TEXT
$ sed '$a THE END!' file.txtAppend line after every 3rd line starting from line 3
TEXT
$ sed '3~3a Some text' file.txt###Numbering
Number line of a file (simple left alignment)
TEXT
$ sed = file.txt | sed 'N;s/\n/\t/'Number line of a file (number on left, right-aligned)
TEXT
$ sed = file.txt | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /'Number line of file, but only print numbers if line is not blank
TEXT
$ sed '/./=' file.txt | sed '/./N; s/\n/ /'Count lines (emulates “wc -l”)
TEXT
$ sed -n '$='###Prepending lines
Insert text before line 5
TEXT
$ sed '5i line number five' file.txtInsert “Example:” before each line that contains “hello”
TEXT
$ sed '/hello/i Example: ' file.txt###Deleting lines
Delete line 5-7 in file
TEXT
$ sed '5,7d' file.txtDelete every 2nd line starting with line 3
TEXT
$ sed '3~2d' file.txtDelete the last line in file
TEXT
$ sed '$d' file.txtDelete lines starting with “Hello”
TEXT
$ sed '/^Hello/d' file.txtDelete all empty lines
TEXT
$ sed '/^$/d' file.txtDelete lines starting with “#”
TEXT
$ sed '/^#/d' file.txt###File spacing
Double space
TEXT
$ sed GDelete all blank lines and double space
TEXT
$ sed '/^$/d;G'Triple space a file
TEXT
$ sed 'G;G'Undo double-spacing
TEXT
$ sed 'n;d'Insert a blank line above line which matches “regex”
TEXT
$ sed '/regex/{x;p;x;}'Insert a blank line below line which matches “regex”
TEXT
$ sed '/regex/G'Insert a blank line around line which matches “regex”
TEXT
$ sed '/regex/{x;p;x;G;}'