[CentOS7]sed 指定字符前后、下一行添加内容、匹配行内容

1. 指定字符前面添加

sed -i 's/指定的字符/需要添加的字符&/' 文件名称

  • 修改Linux启动配置

    • 将 GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
    • 改为 GRUB_CMDLINE_LINUX="ipv6.disable=1 rd.lvm.lv=centos/root rd.lvm.lv=centos/swap net.ifnames=0 biosdevname=0 rhgb quiet"
  • 在rhgb之前添加 net.ifnames=0 biosdevname=0

# 备份配置文件
\cp /etc/default/grub /etc/default/grub.bak
sed -i 's/ rhgb/ net.ifnames=0 biosdevname=0&/' /etc/default/grub

# cat /etc/default/grub
......
GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap net.ifnames=0 biosdevname=0 rhgb quiet"

2. 指定字符后面添加

sed -i 's/指定的字符/&需要添加的字符/' 文件名称

  • 在GRUB_CMDLINE_LINUX=" 后面添加 ipv6.disable=1
# 备份配置文件
\cp /etc/default/grub /etc/default/grub.bak
sed -i 's/GRUB_CMDLINE_LINUX="/&ipv6.disable=1 /' /etc/default/grub

# cat /etc/default/grub
......
GRUB_CMDLINE_LINUX="ipv6.disable=1 rd.lvm.lv=centos/root rd.lvm.lv=centos/swap net.ifnames=0 biosdevname=0 rhgb quiet"
sed -i '/<匹配内容>/d' 文件路径
# 删除nginx.conf ::行
sed -i '/::/d' /etc/nginx/nginx.conf

3. 指定字符下一行添加

sed -i '/指定的字符/a\需要添加的字符' 文件名称

[root@ftp nsxbackup]# cat /etc/pam.d/vsftpd
#%PAM-1.0
session    optional     pam_keyinit.so    force revoke
auth       required	pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
auth       required	pam_shells.so
auth       include	password-auth
account    include	password-auth
session    required     pam_loginuid.so
session    include	password-auth
[root@ftp nsxbackup]# sed "/pam_shells.so/a \*****" /etc/pam.d/vsftpd
#%PAM-1.0
session    optional     pam_keyinit.so    force revoke
auth       required	pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
auth       required	pam_shells.so
*****
auth       include	password-auth
account    include	password-auth
session    required     pam_loginuid.so
session    include	password-auth

4. 指定字符上一行添加

sed -i '/指定的字符/i \需要添加的字符' 文件名称

[root@ftp nsxbackup]# cat /etc/pam.d/vsftpd
#%PAM-1.0
session    optional     pam_keyinit.so    force revoke
auth       required	pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
auth       required	pam_shells.so
auth       include	password-auth
account    include	password-auth
session    required     pam_loginuid.so
session    include	password-auth
[root@ftp nsxbackup]# sed "/pam_shells.so/i \*****" /etc/pam.d/vsftpd
#%PAM-1.0
session    optional     pam_keyinit.so    force revoke
auth       required	pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
*****
auth       required	pam_shells.so
auth       include	password-auth
account    include	password-auth
session    required     pam_loginuid.so
session    include	password-auth

5. 指定行首未添加

sed -i /<匹配内容>/{s/查找/替换/g;s/查找/替换/g} <文件路径>

[root@ftp nsxbackup]# cat /etc/pam.d/vsftpd
#%PAM-1.0
session    optional     pam_keyinit.so    force revoke
auth       required	pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
auth       required	pam_shells.so
auth       include	password-auth
account    include	password-auth
session    required     pam_loginuid.so
session    include	password-auth
[root@ftp nsxbackup]# sed "/pam_shells.so/{s/^/#&/g;s/$/&@/g}" /etc/pam.d/vsftpd
#%PAM-1.0
session    optional     pam_keyinit.so    force revoke
auth       required	pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
#auth       required	pam_shells.so@
auth       include	password-auth
account    include	password-auth
session    required     pam_loginuid.so
session    include	password-auth

END

posted @ 2020-02-23 12:45  LeoShi2020  阅读(6391)  评论(0编辑  收藏  举报