题目:
- 找出"netstat -tan”命令的结果中,以‘LISTEN’后跟0或多个空白字符结尾的行
- 在/etc/fstab文件中不以#开头的行的行首增加#号
- 删除/etc/fstab文件中所有以#开头的#字符
0x00
[root@localhost ~]# netstat -tan | grep "LISTEN *\>" #匹配LISTEN后空格0个或多个空格字符结尾的行
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp6 0 0 ::1:6011 :::* LISTEN
tcp6 0 0 ::1:6010 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::80 :::* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::443 :::* LISTEN
0x01
分析:
- 首先过滤出不以#开头的行------^[^#]
- 匹配目标行的第一个字符------^[^#].
- 使用s将第一个字符替换为#加第一个字符(使用&代替匹配到的字符)------s/^[^#]./#&/
[root@localhost ~]# sed 's/^[^#]./#&/p' /etc/fstab -n
#/dev/mapper/rhel-root / xfs defaults 0 0
#UUID=8f13f33d-5a7d-4cf9-8216-244dcf68f1e7 /boot xfs defaults 0 0
#/dev/mapper/rhel-swap none swap defaults 0 0
#/dev/sr1 /mnt iso9660 defaults,_netdev 0 0
0x02
分析:
首先过滤出以#开头的行-----^#
匹配目标行的第一个字符-----^#.
使用s将第一个字符替换为空------s/^#//
[root@localhost test]# sed 's/^#//p' /etc/fstab -n
/etc/fstab
Created by anaconda on Fri Mar 17 14:58:09 2023
Accessible filesystems, by reference, are maintained under '/dev/disk/'.
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
After editing this file, run 'systemctl daemon-reload' to update systemd
units generated from this file.