Linux之sed命令的使用
Linux系统维护过程中,常通过自动脚本来处理运维方面的工作,而作为运维最常用语言即SHELL来完成脚本。而涉及到替换时我们常使用SED来完成替换工作。
这里简单归纳一下SED的功能,以方便后续脚本中的应用
SED最基本的格式遵循以下形式:
sed [OPTION]...{script-only-if-no-other-script} [input-file]...
[OPTION]中常用的是-e -i
-e 表示被修改后的对象在命令行展示出来,但对象本身实际没有进行修改。你可以理解为仅展现修改后的效果。
-i 则直接替换被修改的对象,一旦修改无法回退。
我通过范例展示:
[root@021Y-SH-BKAP logs]# sed -e ‘s/kernel.shmmax = .*/kernel.shmmax = 123/‘ /etc/sysctl.conf 该命令执行后,在命令行显示kernel.shmmax被修改后的样子。 # Controls the maximum number of shared memory segments, in pages kernel.msgmnb = 6553600 kernel.msgmax = 6553600 kernel.shmmax = 123 kernel.shmall = 2097152 kernel.shmmni = 4096 而实际查看的结果,说明该参数并诶有修改 [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 = .*/kernel.shmmax = 123/‘ /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 account include system-auth username exclude system-auth password include system-auth
可以看到把引号中的s改为i,即为插入,上面的语句说明我在5和6行后分别插入一行语句。我现在要删除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 account exclude system-auth password exclude system-auth
匹配单个或多个任意字符
.单个非换行字符,*匹配多个任意字符(我这里试过锚定行首不成功),[]匹配字符或数值范围([^]与之相反表示不在范围内的),\(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
本文出自 “LinuxOracle” 博客,请务必保留此出处http://onlinekof2001.blog.51cto.com/3106724/1621080
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。