sed command usage skills

sed stands for Stream EDitor, a line editor, which is a stream editor that processes one line of content at a time. When processing, store the currently processed line in a temporary buffer, called "pattern space", and then use the sed command to process the contents of the buffer. After the processing is complete, send the contents of the buffer to the screen. Then read in the lower line and execute the next loop. Without a special command such as 'D', that would clear the pattern space between loops, but not the reserve space. This is repeated until the end of the file. The file contents are not changed unless you use redirection to store the output.
Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, write conversion programs, etc.

Use the format: sed [OPTION]...{script}[input-file]...

Option description:
-n Do not output the content of the pattern space to the screen
-r Support the use of extended regular expressions
-e Multi-point editing, that is, you can edit the content of the pattern space multiple times
-f /Path/script_file Read the editing script from the specified path
-i.bak Simple -i is to modify the file directly, plus .bak or any .xxx means to back up the source file before modifying

Script description:
It is used for address definition, and the following address definition forms are as follows:
1 Do not write address definition to indicate full-text search;
2 #,# where # represents the line number, from which line to which line; #,+# Indicates to retrieve +# lines from a certain line;
3 /part/ All lines that can be matched by regular expression part; You can also use #, /part/ in conjunction with the line number to match from a certain line until it matches the part pattern. Note that there is no delimitation format such as /part/,#
4 $ means the last line, used in conjunction with the above delimiter #, $ means the #th line to the last line;
5 ~ means stepping, such as 1~2 odd lines, 2~ 2 even-numbered lines, 3~5 are taken every 5 lines starting from the third line

Edit command:
After the address definition is completed, you need to specify how to process the defined line. The specific processing instructions are as follows
d: Delete the line matching the pattern space and immediately enable the next cycle
p: Print the content of the current pattern space and append it to the default After output
a[]text: Append text after the specified line Supports multi-line appending with \n
i[]text: Insert text before the line
c[]text: Replace line with single or multi-line text
w /path/somefile: Save Pattern-matched lines to specified file
r /path/somefile: read the text of the specified file into the pattern space after the matched line
=: insert the line number before each line delimited
!: match line in the pattern space negate processing
n read the next input line, process the new line with the next command instead of the first command

The above options or commands can be formed in the following format,
sed -option ' address delimiter editing command ' is completed with the edited text
address delimiter followed by the editing command ,
for example:
sed -n '10,$aHello Sed' /etc/passwd
means to take /etc /passwd From the 10th to the last line, add a line of content Hello Sed after each line obtained by editing the command a, but the matching line, that is, the content of the pattern space, will not be output to the screen, so only 10 to the last line will be output after each line Added Hello Sed line, here is the result:

[root@centos7 16:14:30 ~]#sed  -n '45,$aHell Sed' /etc/passwd
Hell Sed
Hell Sed
Hell Sed
Hell Sed
Hell Sed

If the -n option is not used, the following effects will be obtained:

[root@centos7 16:15:57 ~]#sed   '45,$aHell Sed' /etc/passwd | tail -12
mongodb:x:1003:1005::/home/mongodb:/bin/bash
base:x:1004:1006::/home/base:/bin/bash
bash:x:1005:1007::/home/bash:/bin/bash
Hell Sed
testbash:x:1006:1008::/home/testbash:/bin/bash
Hell Sed
nologin:x:1008:1010::/home/nologin:/sbin/nologin
Hell Sed
sh:x:1009:1011::/home/sh:/bin/bash
Hell Sed
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
Hell Sed
[root@centos7 16:16:02 ~]#

Assuming that the requirements for file modification are met here, you can cache the parameter option -i to make the modification take effect immediately in the file

For another example, if you need to delete some rows, you can handle it like this:

[root@centos7 16:25:00 ~]#sed  '1,+40d'  /etc/passwd
docker:x:1002:1004::/home/docker:/bin/bash
mongodb:x:1003:1005::/home/mongodb:/bin/bash
base:x:1004:1006::/home/base:/bin/bash
bash:x:1005:1007::/home/bash:/bin/bash
testbash:x:1006:1008::/home/testbash:/bin/bash
nologin:x:1008:1010::/home/nologin:/sbin/nologin
sh:x:1009:1011::/home/sh:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@centos7 16:25:19 ~]#
表示删除从1第行到第41行如果这里加上-i 选项则对/etc/passwd生效

The above is the use of address definition and editing commands. The following describes the use of the search and replace command s///:
s/// Or replace the / symbol with other delimiters. For example, s@@@ s###
is in search and replace The command s/// can be followed by some replacement markers, for example
s///g means global replacement in the line, that is, all characters matching the pattern in each line will be replaced
s///p After finding and replacing, print
w /PATH/TO /SOMEFILE: Save successfully replaced lines to a file

The following typical examples are listed below to help individuals understand (centos6 system):

1. Copy the /etc/rc.d/rc.sysinit file to the /tmp directory, and add # to the beginning of the line starting with at least one blank character in the /tmp/rc.sysinit file.

[root@centos6 15:08:14 ~]#cp /etc/rc.d/rc.sysinit /tmp
[root@centos6 15:16:08 ~]#sed -n '13,13p' /etc/rc.sysinit 
    . /etc/sysconfig/network
[root@centos6 15:16:25 ~]#sed  's/\(^[[:space:]]\)/#\1/g' /tmp/rc.sysinit | sed -n '13,13p'
#    . /etc/sysconfig/network
[root@centos6 15:17:05 ~]#
这里只是为了方便展示出结果因此只打印出更改后生效的第13行

2 Modify the last 0 of all enabled=0 or gpgcheck=0 in the /etc/yum.repos.d/repobak/CentOS-Media.repo file to 1

[root@centos6 15:23:31 ~]#cat !$
cat /etc/yum.repos.d/repobak/CentOS-Media.repo
# CentOS-Media.repo
#
#  This repo can be used with mounted DVD media, verify the mount point for
#  CentOS-6.  You can use this repo and yum to install items directly off the
#  DVD ISO that we release.
#
# To use this repo, put in your DVD and use it with the other repos too:
#  yum --enablerepo=c6-media [command]
#  
# or for ONLY the media repo, do this:
#
#  yum --disablerepo=\* --enablerepo=c6-media [command]

[c6-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
        file:///media/cdrom/
        file:///media/cdrecorder/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
[root@centos6 15:23:41 ~]#
可以看到两个参数的值都是0,使用sed修改:
[root@centos6 15:23:41 ~]#sed   -e 's/enabled=0/enabled=1/' -e 's/gpgcheck=0/gpgcheck=1/'  /etc/yum.repos.d/repobak/CentOS-Media.repo | tail -3
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
也可以写成:
[root@centos6 15:25:17 ~]#sed 's/enabled=0/enabled=1/;s/gpgcheck=0/gpgcheck=1/'  /etc/yum.repos.d/repobak/CentOS-Media.repo | tail -3
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

3 Print odd lines and even lines and
even lines:

[root@centos6 15:33:17 ~]#sed -n 'n;p' /etc/passwd
[root@centos6 15:35:00 ~]#sed -n '2~2p' /etc/passwd
奇数行:
[root@centos6 15:35:55 ~]#sed -n '1~2p' /etc/passwd
[root@centos6 15:36:00 ~]#sed -n 'p;n' /etc/passwd

4 The role of the n command

[root@centos7 17:18:18 ~]#cat passwd.out 
docker:x:1002:1004::/home/docker:/bin/bash
mongodb:x:1003:1005::/home/mongodb:/bin/bash
base:x:1004:1006::/home/base:/bin/bash
bash:x:1005:1007::/home/bash:/bin/bash
testbash:x:1006:1008::/home/testbash:/bin/bash
nologin:x:1008:1010::/home/nologin:/sbin/nologin
sh:x:1009:1011::/home/sh:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@centos7 17:18:27 ~]#sed  '/docker/n;s/mongodb/manguo/g;' passwd.out 
docker:x:1002:1004::/home/docker:/bin/bash
manguo:x:1003:1005::/home/manguo:/bin/bash
base:x:1004:1006::/home/base:/bin/bash
bash:x:1005:1007::/home/bash:/bin/bash
testbash:x:1006:1008::/home/testbash:/bin/bash
nologin:x:1008:1010::/home/nologin:/sbin/nologin
sh:x:1009:1011::/home/sh:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@centos7 17:18:29 ~]#
这里先使用匹配模式匹配到docker行,即第一行,n命令表示继续读取docker行的后一行并将读取到的行交给s查找替换来处理,查找替换将mongodb替换成manguo

In addition, you need to pay attention to the greedy mode of sed matching
sed command usage skills
. The first command in the figure below does not quite explain the problem. In the second matching result, there are two groups. sed first matches the first line at the beginning of root and saves it, then go to Match bin, at this time, read from the second line and find that it also matches bin, so this time /root/, /bin/ pattern matching is completed. But sed will not stop but will continue to match down. Therefore, it is found that the third line (the line at the beginning of the operator) contains root and then saves it, and then continues to the next line to match the bin, and saves it after finding it.
At this time, there is a second group of /root/, /bin/ modes. sed will continue to search further down but no longer match the /root/,/bin/ pattern and end. Print out the matched two sets of results!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324729987&siteId=291194637