The SED of Linux
SED is the abbreviation for Stream Editor, which allows text editing of text flows, specified sets of files, or standard input. Features are very powerful.
The basic mode of the SED command is:
sed [-parameter] ' command ' text
1. Two major principles of SED
sed commands always start with a single letter . Like what
[[email protected]] $echo "hello123" | Sed ' s/hello/hello/' #把hello用HELLO替换HELLO123
In the above example, S is the replacement command, and S is followed by a split symbol, and everything (usually '/'), such as the above example is equivalent to the following command:
[[email protected]] $echo "hello123" | Sed ' s*hello*hello* ' HELLO123
sed Most commands allow to add an address in front . This address is used to specify which line of the input stream is to be edited, and if omitted, all rows are edited by default. For example
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
[[email protected]] $cat del2hello1hello2 HELLOHELLO3 [[email protected]] $cat Del2 | Sed ' 2s/hello/hello/' #替换第2行hello1HELLO2 hellohello3 [[email protected]deldir] $cat Del2 | Sed ' 2,3s/hello/hello/' #替换第2到3行hello1HELLO2 helloHELLO3 [[email protected]] $cat Del2 | Sed ' s/hello/hello/' #替换所有行 (no address, is default) Hello1hello2 helloHELLO3
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
2. SED basic editing commands
Command |
function abbreviation |
Function description |
I |
Front plug |
Insert in front of the current line |
A |
Rear Plug |
Inserts after the current line |
D |
By deleting |
Delete When moving forward |
P |
Check |
Prints the current line. By default, all rows are printed and the qualifying rows are printed. If you block default, add parameter-n |
s/regex/replacement/ |
Change |
Replace the regex with replacement |
Y/set1/set2 |
Change |
Replace the characters in the Set1 with the characters in the corresponding Set2 (you must ensure that the number of characters in the two sets is equal) |
= |
|
Outputs the line number of the current line |
Q |
|
Exit sed when the current line is finished processing |
Q |
|
Exit SED directly |
Plug (i-front, a rear plug)
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
[[email protected]] $cat Del123[[email protected]] $sed ' 2i mm ' del1mm23[[email protected]] $sed ' 2a mm ' Del12mm3[[email Pro Tected]] $sed ' 2,3i mmm ' del1mmm2mmm3[[email protected]] $sed ' 2,3a mmm ' del12mmm3mmm
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
By deleting
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
[[email protected]] $cat del2hello1hello2 HELLOHELLO3 [[email protected]] $sed ' 1d ' del2 #删除第一行hello2 hell OHELLO3 [[email protected]] $sed ' $d ' Del2 #删除最后一行hello1hello2 hello[[email protected]] $sed ' 2,3d ' Del2 #删除第2到3行hello1
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
Check
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
[[Email protected]]$-n-n
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
Swap--string substitution
650) this.width= 650; "Src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/
[[email protected]] $cat del2hello1hello2 hellohello3 [[email protected]] $sed ' 1,2s/ hello/no/' del2 #第1到2行hello替换成NO (one line is replaced only once) No1no2 hellohello3 [[email protected]] $sed ' 1,2s/hello/no/g ' del2 # 1 to 2 rows of Hello all replaced with NO (parameter G = Replace All) no1no2 nohello3 [[email protected]] $sed ' 1,2c no ' del2 #把1到2行用NO替换NOhello3 [[ Email protected]] $sed ' s/hello/hello/' del2 #把所有行的 " Hello "replaced with" Hello ", equivalent to sed ' 1, $s/hello/hello/' del2hello1 hello2 hellohello3
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
Swap--hash substitution
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
[[email protected]] $cat del2 hello1hello2 Hellohello3 [[email protected]] $cat Del2 | Sed ' y/hel/hel/' #把 ' hel ' corresponding to ' hel ' Hello1hello2 HELLoHELLo3
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
Other
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
[[email protected]] $cat Del2hello1hello2 hellohello3[[email protected]] $cat Del2 | Sed ' = ' #输出当前行的行号1hello12hello2 hello3hello3 [[email protected]] $cat Del2 | Sed ' q ' #输出当前行就结束sedhello1 [[email protected]] $cat Del2 | Sed ' Q ' #立马结束sed
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
3. Sed common parameters
Parameters |
Parameter meaning |
-N |
Quiet mode, do not add-n will output the input stream to the terminal, plus only the output of eligible |
-E |
Multi-point |
-F |
-f filename Executes the sed command in the filename file |
-I. |
Directly modify the contents of the read file (discreet) |
-N
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
[[email protected]] $cat Del2 | Sed ' 2p ' hello1hello2 hellohello2 hellohello3 [[email protected]] $cat Del2 | Sed-n ' 2p ' #只输出符号条件的行hello2 hello
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
-E
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
[[email protected]] $cat Del2 | Sed-e ' 2p '-e ' 3p ' #多重命令同时hello1hello2 hellohello2 hellohello3 hello3 [[email protected]] $cat Del2 | Sed-n-E ' 2p '-e ' 3p ' Hello2 Hellohello3
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
-F
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
[[email protected]] $cat DELS/HEL/HEL/2, $p [[email protected]] $sed-F DEL del2 #按着DEL文件里的awk命令修改HELlo1HEL Lo2 HelloHELlo2 HelloHELlo3 HELlo3 [[email protected]] $sed-n-F DEL Del2hello2 HelloHELlo3
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
-I.
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
[[email protected]] $cat del2hello1hello2 HELLOHELLO3 [[email protected]] $sed-i ' s/hel/hel/' Del2 #直接修改del2 [[E Mail protected]] $cat Del2hello1hello2 HelloHELlo3
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
Linux Regular Expression--sed