grep和egrep练习

1、显示/etc/rc.d/rc.sysinit文件中以不区分大小的h开头的行;

[root@node1 a1]# egrep -i '^h' /etc/rc.d/rc.sysinit
[root@node1 a1]# egrep 'h|H' /etc/rc.d/rc.sysinit
[root@node1 a1]# egrep '[hH]' /etc/rc.d/rc.sysinit

2、显示/etc/passwd中以sh结尾的行;

[root@node1 a1]# grep -E 'sh$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
fox:x:1000:1000::/home/fox:/bin/bash

3、显示/etc/fstab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行

[root@node1 a1]# grep -E '^#\s+\S*' /etc/fstab 
# /etc/fstab
# Created by anaconda on Thu Sep  7 05:25:48 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.
[root@node1 a1]# egrep '^#[[:space:]]+[^[:space:]]*' /etc/fstab 
# /etc/fstab
# Created by anaconda on Thu Sep  7 05:25:48 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.
[root@node1 a1]# grep -E '^# +[^ ]*' /etc/fstab 
# /etc/fstab
# Created by anaconda on Thu Sep  7 05:25:48 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.

4、查找/etc/rc.d/rc.loca]中包含“以to开始并以to结尾”的字串行:

[root@node1 a1]# egrep '\bto.*to\b' /etc/rc.d/rc,local

5、查找/etc/inittab中含有“以s开头,并以d结尾的单词”模式的行;

[root@node1 a1]# grep -w 's.*d' /etc/inittab 
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:

6、查找ifconfig命令结果中的1-255之间的整数;

[root@node1 a1]# ifconfig| egrep -w '[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]'
        inet 192.168.17.129  netmask 255.255.255.0  broadcast 192.168.17.255
        inet6 fe80::20c:29ff:fee5:4a1d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:e5:4a:1d  txqueuelen 1000  (Ethernet)
        RX packets 6413  bytes 558548 (545.4 KiB)
        TX packets 4359  bytes 415704 (405.9 KiB)
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        RX packets 17  bytes 2039 (1.9 KiB)
        TX packets 17  bytes 2039 (1.9 KiB)

7、显示/var/log/secure文件中包含“Failed”或“FAILED”的行

[root@node1 a1]# grep -i 'Failed'  /var/log/secure

8、在/etc/passwd中取出默认she11为bash

[root@node1 a1]# grep -w 'bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
fox:x:1000:1000::/home/fox:/bin/bash

9、以长格式列出/etc/目录下以ns开头、.conf结尾的文件信息

[root@node1 a1]# ls /etc | grep '\bns.*.conf\b'
nsswitch.conf
nsswitch.conf.bak

10、高亮显示passwd文件中冒号,及其两侧的字符

[root@node1 a1]# egrep '.:.' /etc/passwd
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值