Sed delete and add the up and down data of the specified row

Topic 1: delete the previous row and the next row data of the matching row

1. Delete the previous line and the next line of the matching line:

sed -i -e '/string/{n;d}' -e '$!N;/\n.*string/!P;D' filename 

2. Delete the previous line that matches the line of a specific character

sed -i -e '$!N;/\n.*string/!P;D' filename

3. Delete the next line that matches the line of a specific character

sed -i -e '/string/{n;d}' filename

4. Variables are used in sed to delete the previous line and the next line of the matching line:

AA=string     #Variable specifies the matching string
sed -i -e '/'"$AA"'$/{n;d}' -e '$!N;/\n.*'"$AA"'$/!P;D' file

Topic 2: matching to the previous line or inserting a data in the next line of the string

A append content sed '/ match words / a \ content to be added' example.file
Iinsert content sed '/ match words / i \ content to be added' example.file insert content to the previous line position of the matching row target)
Example:
#I want to add a line before or after the line containing the keyword "chengyongxu.com" to the file. The content is "allow chengyongxu.cn"

1   #Pre plus
2   sed -i '/allow chengyongxu.com/i\allow chengyongxu.cn' the.conf.file
3   #Before and after row
4   sed -i '/allow chengyongxu.com/a\allow chengyongxu.cn' the.conf.file

Insert a line of data for the next line that matches a specific string in the file:

sed -i '/listen/a\   listen 80\;'   filename

To batch insert multiple rows of data after a row that matches a specific string:

sed -i '/syncsendmsg.php/a\#013. The platform advertising hour plan is written into the main platform XXX 2019-07-04 every 5 minutes \ 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

Insert a row of data for the previous row that matches a specific string in the file:

sed -i '/listen/i\   listen 80\;' filename

Topic 3: delete the previous line or the next line of data matched to the specified string

1. Delete the previous line of the specified line

sed -i -e :a -e '$!N;s/.*\n\(.*ServerName abc.com\)/\1/;ta' -e 'P;D' file
//For example:
[root@VM_82_178_centos vhost]# grep listen m.afpfpm.cn443.conf 
   listen 80;
   listen       443;

//Delete listen 443; the uplink content of this line: listen 80;
sed -i -e :a -e '$!N;s/.*\n\(.*listen       443\)/\1/;ta' -e 'P;D' m.afpfpm.cn443.conf

2. Delete the content between specified strings

sed -i '/ServerName abc.com/,/\/VirtualHost/d' $file

http://www.linuxso.com/shell/17542.html

nginx configuration file replacement characters in production

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

Today's demo is here. Welcome to leave a message to exchange and learn

Tags: Linux PHP Unix shell Nginx

Posted on Fri, 01 Nov 2019 11:35:46 -0700 by prue_