Topic 1: delete matching rows of data on one line and the next line
1. Remove the upper and lower lines of matching lines:
sed -i -e '/string/{n;d}' -e '$!N;/\n.*string/!P;D' filename
2. Delete matched to the specific character of the row row
sed -i -e '$!N;/\n.*string/!P;D' filename
3. Delete the next line matching to a specific row containing the character
sed -i -e '/string/{n;d}' filename
4.sed use variables, delete the matching rows on one line and the next line:
AA=string #变量指定匹配字符串
sed -i -e '/'"$AA"'$/{n;d}' -e '$!N;/\n.*'"$AA"'$/!P;D' file
Problem two: the rows to match the character string of the next line or to insert a data
a additional contents sed '/ matching word / a \ was added to the contents of the' example.file (the additional content to match the position of the next target row)
I Inserts sed '/ matching word / i \ content to be added' example .file the content into the target matching row on row position)
example:
# I want the file contains "chengyongxu.com" the keyword is added before the line or row after row, says "allow chengyongxu.cn"
1 #行前加
2 sed -i '/allow chengyongxu.com/i\allow chengyongxu.cn' the.conf.file
3 #行前后
4 sed -i '/allow chengyongxu.com/a\allow chengyongxu.cn' the.conf.file
Matched to the specific file to the next line of the character string to insert a row:
sed -i '/listen/a\ listen 80\;' filename
Bulk insert multiple rows of data in a row after matched to specific character string:
sed -i '/syncsendmsg.php/a\#013.平台广告小时计划每5分钟写入主平台 xxx 2019-07-04\n*/5 * * * * /usr/bin/php /data/cron/ptask/countjs_syc_plan_main.php\n*/5 * * * * /usr/bin/php /data/cron/ptask/countjs_syc_plan_h_main.php' filename
In particular to the matching character string to a file on line insert a row:
sed -i '/listen/i\ listen 80\;' filename
Issue three: to delete the rows matching the specified string located either next row
1, delete the specified line on the line
sed -i -e :a -e '$!N;s/.*\n\(.*ServerName abc.com\)/\1/;ta' -e 'P;D' file
例如:
[root@VM_82_178_centos vhost]# grep listen m.afpfpm.cn443.conf
listen 80;
listen 443;
删除 listen 443;这一行的上行内容: listen 80;
sed -i -e :a -e '$!N;s/.*\n\(.*listen 443\)/\1/;ta' -e 'P;D' m.afpfpm.cn443.conf
2, delete the contents between the specified string
sed -i '/ServerName abc.com/,/\/VirtualHost/d' $file
http://www.linuxso.com/shell/17542.html
Replace the characters in the production of nginx configuration file
sed -i 's/#fastcgi_pass/fastcgi_pass/g;s/fastcgi_pass unix:\/dev\/shm\/php-cgi.sock/#fastcgi_pass unix:\/dev\/shm\/php-cgi.sock/g' m.6saeq.cn443.conf
To demonstrate here today, welcome message exchange of learning together