Linux系统维护过程中,常通过自动脚本来处理运维方面的工作,而作为运维最常用语言即SHELL来完成脚本。而涉及到替换时我们常使用SED来完成替换工作。
这里简单归纳一下SED的功能,以方便后续脚本中的应用
SED最基本的格式遵循以下形式:
sed [OPTION]...{script-only-if-no-other-script} [input-file]...
[OPTION]中常用参数:
-n 打印出改动的行,不适用该参数时则打印会将整个文件内容显示出来。
-e 通过指定-f读取脚本中的多条表达或者指定多个-e进行对多条表达式的连续操作。
-i 则直接替换被修改的对象,一旦修改无法回退。
我通过范例展示:
[root@021Y-SH-BKAP logs]# sed -e 's/\(kernel.shmmax = \).*/\1123/' /etc/sysctl.conf 该命令执行后,在命令行显示kernel.shmmax被修改后的样子。 # Controls the maximum number of shared memory segments, in pages kernel.shmmax = 123 而实际查看的结果,说明该参数并诶有修改 [root@021Y-SH-BKAP logs]# grep 'kernel.shmmax' /etc/sysctl.conf kernel.shmmax = 8589934592 当使用-i参数以后,该文件中相应的对象则直接被修改 [root@021Y-SH-BKAP logs]# sed -i 's/\(kernel.shmmax = \).*/\1123/' /etc/sysctl.conf [root@021Y-SH-BKAP logs]# grep 'kernel.shmmax' /etc/sysctl.conf kernel.shmmax = 123
以上是SED命令最基本的用法,现在我们在进阶讨论一下它的匹配功能
SED{script-only-if-no-other-script}中带匹配功能,可以是字符匹配,也可以是行匹配,具体用法如下
[root@021Y-SH-BKAP logs]# sed -e '/password/ s/include/exclude/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth include system-auth account required pam_nologin.so account include system-auth password exclude system-auth
可以看到匹配了password的行中的include被转化为了exclude,通过数值指定匹配的行
[root@021Y-SH-BKAP logs]# sed -e '1,6 s/include/exclude/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth exclude system-auth account required pam_nologin.so account exclude system-auth password exclude system-auth
有人问,如果我不想使用替换,想直接插入到指定的位置呢?
[root@021Y-SH-BKAP logs]# sed -e '5,6 iusername exclude system-auth' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth include system-auth account required pam_nologin.so username exclude system-auth
可以看到把引号中的s改为i,即为插入,上面的语句说明我在5和6行后分别插入一行语句。
另外,匹配插入的方式如下,sed通过//对输入的内容进行匹配,i\后面则跟插入的内容,表示匹配当前内容所在的行前插入内容,a\则是在行后插入。
sed -i "/^#ServerName/i\ServerName $ipadr:8080" $path/$pkgv/conf/httpd.conf sed -i "/^#ServerName/a\ServerName $ipadr:8080" $path/$pkgv/conf/httpd.conf
我现在要删除5,6两行记录
1 #%PAM-1.0 2 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so 3 auth include system-auth 4 account required pam_nologin.so 5 account include system-auth 6 password include system-auth [root@021Y-SH-BKAP logs]# sed -e '5,6d' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth include system-auth account required pam_nologin.so
其它的匹配模式:
^锚定行的开始,$锚定行的结尾 [root@021Y-SH-BKAP logs]# sed -e '/^a/ s/include/exclude/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth exclude system-auth account required pam_nologin.so account exclude system-auth password include system-auth [root@021Y-SH-BKAP logs]# sed -e '/h$/ s/include/exclude/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth exclude system-auth account required pam_nologin.so ......
匹配单个或多个任意字符
.单个非换行字符,*匹配多个任意字符(我这里试过锚定行首不成功),[]匹配字符或数值范围([^]与之相反表示不在范围内的),\(char\) 保存char的内容以供后续调用,&代替替换的内容 \<,\>锚定单词的开头和结尾,x\{2\},x\{2,\},x\{2,3\},分别为匹配X字符2次,至少匹配X字符2次,匹配X字符2到3次。 [root@021Y-SH-BKAP logs]# sed -e '/^a..h/ s/include/exclude/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth exclude system-auth account required pam_nologin.so account include system-auth [root@021Y-SH-BKAP logs]# sed -e '/a*h/ s/include/exclude/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth exclude system-auth account required pam_nologin.so account exclude system-auth password exclude system-auth [root@021Y-SH-BKAP logs]# sed -e '/^a[a-z][a-z]h/ s/include/exclude/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth exclude system-auth account required pam_nologin.so account include system-auth [root@021Y-SH-BKAP logs]# sed -e '/^[^b-st-z]..h/ s/include/exclude/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth exclude system-auth account required pam_nologin.so account include system-auth password include system-auth [root@021Y-SH-BKAP logs]# sed -e '/^[^b-st-z]..h/ s/in\(clude\)/ex\1/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth exclude system-auth account required pam_nologin.so [root@021Y-SH-BKAP logs]# sed -e '/^[^b-st-z]..h/ s/clude/ex&/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth inexclude system-auth account required pam_nologin.so [root@021Y-SH-BKAP logs]# sed -e '/\<a.*h/ s/include/exclude/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth exclude system-auth account required pam_nologin.so [root@021Y-SH-BKAP logs]# sed -e '/c\{2,\}/ s/required/unrequired/g' /etc/pam.d/login #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so auth include system-auth account unrequired pam_nologin.so
我们已知s/是替换,a\,i\插入匹配行后行前,此外sed中的其它参数的意义:
h 拷贝匹配的内容到内存中的缓冲区。
H 追加匹配的内容到内存中的缓冲区。
g 获取内存缓冲区的内容,并替代当前匹配的内容
G 获取内存缓冲区的内容,并追加到当前匹配的内容。
n 读取下一个输入行,用下一个命令处理该行。
N 追加下一个输入行到匹配中,并在两行间嵌入一个新行。
p 打印匹配行内容
P 打印匹配行的第一行
q 退出sed
r 从文件中读行
w 追加内容到文件末尾
sed -n '/^ServerName/w/tmp/chinaman' /usr/local/apache2/conf/httpd.conf # cat /tmp/chinaman ServerName 10.x.x.x:80
W 追加内容的第一行到文件末尾
= 打印当前行号
sed -ne '/^ServerName/p' -e '/^ServerName/=' /usr/local/apache2/conf/httpd.conf ServerName 10.x.x.x:80 169
sed匹配空格或\t
sed -n 's/\(^[ \t]*CONFFILE=.*\)/\1/p' /etc/init.d/httpd