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
[[Email protected]]$CatDel2hello1hello2 Hellohello3 [[Email protected]]$CatDel2 |sed '2s/hello/hello/' #替换第2行Hello1hello2 Hellohello3 [[Email protected]]$CatDel2 |sed '2,3s/hello/hello/' #替换第2到3行Hello1hello2 helloHELLO3 [[Email protected]]$CatDel2 |sed 's/hello/hello/' #替换所有行 (no address, is default) Hello1hello2 helloHELLO3
2. SED basic editing commands
Command |
function abbreviation |
Function description |
A |
Increase |
Append text 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 |
I |
Interpolation |
Insert text before the current line |
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 |
|
Direct Introduction to SED |
Increase
[[Email protected]]$CatDel2hello1hello2 Hellohello3 [[Email protected]]$sed '1a HELLO'Del2 #在第一行后增加一行, the content is "HELLO"hello1hellohello2 hellohello3 [[Email protected]]$sed '2,3a HELLO'Del2 #从第2到3行, add a row after each line, with the content "HELLO"Hello1hello2 HelloHELLOhello3 hello[[email protected]]$sed '2,3a Hello\nhellotwo'Del2 #从第2到3行, each line to add some content "Hello\nhello", pay attention to the added inside there is a carriage return Hello1hello2 HelloHELLOHELLOTWOhello3 hellohellotwo
By deleting
[[Email protected]]$CatDel2hello1hello2 Hellohello3 [[Email protected]]$sed '1d'del2 #删除第一行hello2 Hellohello3 [[Email protected]]$sed '$d'del2 #删除最后一行hello1hello2 hello[[email protected]]$sed '2,3d'del2 #删除第2到3行hello1
Check
[[Email protected]]$CatDel2hello1hello2 Hellohello3
[[Email protected]]$sed '2p'Del2 #查看第2行, without the-n parameter, the original data will also output Hello1hello2 Hellohello2 Hellohello3
[[Email protected]]$sed-N'2p'del2 #查看第2行Hello2 Hello
[[Email protected]]$sed-N'2, $p'del2 #查看2到最后一行Hello2 Hellohello3
Interpolation
[Email protected]]$cat Del2hello1hello2 Hellohello3 [[email protected]]$catsed ' IMM: ' #所有的行插入 ' IMM: 'MM:hello1MM:hello2 hellomm:hello3 [[email protected]]$catSED '2,3imm:' #从第23行插入 ' IMM: 'Hello1mm:hello2 Hellomm:hello3
Swap--string substitution
[[Email protected]]$CatDel2hello1hello2 Hellohello3 [[Email protected]]$sed '1,2s/hello/no/'del2 #第1到2行hello替换成NO (replace one line only)No1no2 Hellohello3 [email protected]]$sed '1,2s/hello/no/g'del2 #地1到2行的hello全部替换成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 by "Hello", equivalent to sed ' 1, $s/hello/hello/' del2HELLO1 HELLO2 helloHELLO3
Swap--hash substitution
[Email protected]]$cat del2 hello1hello2 Hellohello3 [[email protected]]$catsed ' y/hel/hel/ #把 ' hel ' corresponding to ' hel ' Hello1hello2 HELLoHELLo3
Other
[[Email protected]]$CatDel2hello1hello2 hellohello3[[email protected]]$CatDel2 |sed '=' #输出当前行的行号1Hello12Hello2 Hello3Hello3 [[Email protected]]$CatDel2 |sed 'Q' #输出当前行就结束sed Hello1[[email protected]]$CatDel2 |sed 'Q' #立马结束sed
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
[Email protected]]$catsed '2p'Hello1hello2 Hellohello2 Hellohello3 [[Email protected]]$catsed '2p' #只输出符号条件的行 Hello2 Hello
-E
[[Email protected]]$catsed '2p'3p' #多重命令同时 Hello1hello2 Hellohello2 Hellohello3 hello3 [email protected]]$ Cat sed -n '2p'3p'Hello2 Hellohello3
-F
[Email protected]]$cat DELs/hel/hel/2, $p [[email protected]]$sed - F DEL Del2 #按着DEL文件里的awk命令修改 Hello1hello2 HelloHELlo2 HelloHELlo3 HELlo3 [[email protected]]$sed -N-
-I.
[Email protected]]$cat del2hello1hello2 hellohello3 [email protected]]$sed' s/hel/hel/ ' Del2 #直接修改del2 [Email protected]]$cat Del2hello1hello2 HelloHELlo3
The SED of Linux