当前位置:网站首页>Use of shell sed command

Use of shell sed command

2022-04-23 17:06:00 Magic Flute love

sed

sed Is a flow editor , It processes one line at a time . When dealing with , Store the currently processed rows in a temporary buffer , be called “ Mode space ”, Then use sed Command processing buffer contents , After processing , Send the contents of the buffer to the screen . Next line , This is repeated , Until the end of the file . The contents of the document have not changed , Unless you use redirected storage output .

Basic usage

sed [ Option parameters ]  'command'  filename

Option parameter description

Option parameters function
-e Directly in command line mode sed Action editor .

Common command function description

command Function description
a newly added ,a You can use a string after , On the next line
d Delete
s Find and replace

Case practice

#  Data preparation 
[bd@localServer ~]$ touch sed.txt
[bd@localServer ~]$ vim sed.txt
fu xia
zhou men
wo  wo
lai  lai

le  le

###  take “wo shi” This word is inserted into sed.txt Second elements , Print .
[bd@localServer ~]$ sed '2a wo shi' sed.txt 
fu xia
zhou men
wo shi
wo  wo
lai  lai

le  le

####  The source file has not changed 
[bd@localServer ~]$ cat sed.txt 
fu xia
zhou men
wo  wo
lai  lai

le  le

###  Delete sed.txt All files contain wo The line of 
[bd@localServer ~]$ sed '/wo/d' sed.txt
fu xia
zhou men
lai  lai

le  le

###  take sed.txt In file wo Replace with ni,g Express global For global operations , If not, only the first one 
[bd@localServer ~]$ sed 's/wo/ni/g' sed.txt 
fu xia
zhou men
ni  ni
lai  lai

le  le

###  take sed.txt The second line in the file is deleted and the wo Replace with ni
[bd@localServer ~]$ sed -e '2d' -e 's/wo/ni/g' sed.txt 
fu xia
ni  ni
lai  lai

le  le

版权声明
本文为[Magic Flute love]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231704450922.html