1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
[root@y_zilong ~]# grep -i '^S.*' /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 9272 kB
Slab: 375052 kB
SReclaimable: 278660 kB
SUnreclaim: 96392 kB
或者:[root@y_zilong ~]# cat /proc/meminfo |grep -i '^S.*'
[root@y_zilong ~]# grep -E "^(S|s)" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 9272 kB
Slab: 375052 kB
SReclaimable: 278660 kB
SUnreclaim: 96392 kB
或者:[root@y_zilong ~]# cat /proc/meminfo |grep -E '^(S|s)'
2、显示/etc/passwd文件中不以/bin/bash结尾的行
[root@y_zilong ~]# cat /etc/passwd |grep -v "/bin/bash$"
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
或者:[root@y_zilong ~]# grep -v "/bin/bash$" /etc/passwd
[root@y_zilong ~]# cat /etc/passwd |grep -nv "/bin/bash"
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
或者:[root@y_zilong ~]# grep -nv "/bin/bash" /etc/passwd
3.使用egrep取出/etc/rc.d/init.d/functions中其基名
知识点1:考察[]^表示除指定字符之外
知识点2:egrep中,除词首词尾符外,各符号无需使用\转义符
[root@y_zilong ~]# echo "/etc/rc.d/init.d/functions" |egrep -o [^/]+$
functions
或者:[root@y_zilong ~]# echo "/etc/rc.d/init.d/functions" |grep -E -o [^/]+$
4、使用egrep取出上面路径的目录名
知识点1:*字符是贪婪算法,若有多个斜杠,会匹配到最后一个为止
[root@y_zilong ~]# echo "/etc/rc.d/init.d/functions" |grep -o "^.*/"
/etc/rc.d/init.d/
[root@y_zilong ~]#
5、显示ifconfig命令结果中所有IPv4地址
知识点:正则表达式匹配ip
[root@y_zilong ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.121 netmask 255.0.0.0 broadcast 10.255.255.255
inet6 fe80::7e7e:c681:465:5aa6 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d3:72:51 txqueuelen 1000 (Ethernet)
RX packets 31347 bytes 2661689 (2.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 19734 bytes 6309826 (6.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@y_zilong ~]# ifconfig | grep -oE "((\<1?[0-9]{1,2}\>|\<2[0-4][0-9]\>|\<25[0-5]\>)\.){3}(\<1?[0-9]{1,2}\>|\<2[0-4][0-9]\>|\<25[0-5]\>)"
10.0.0.121
255.0.0.0
10.255.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
[root@y_zilong ~]# ifconfig |grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
10.0.0.121
255.0.0.0
10.255.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
[root@y_zilong ~]# ifconfig |grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" |head -1
10.0.0.121
[root@y_zilong ~]#
6、统计last命令中以root登录的每个主机IP地址登录次数
知识点1:正则表达式匹配ip
知识点2:使用sort排序,uniq -c计数
知识点3:使用|表示或时,需要使用括号,不然会与计数符联动产生错误
[root@y_zilong ~]# last | last | grep -Eo "((1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])" | sort | uniq -c | sort -nr
10 10.0.0.1
[root@y_zilong ~]# last |last |grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | sort |uniq -c |sort -nr
10 10.0.0.1
7、将此字符串:Nice to meet you 中的每个字符去重并排序,重复次数多的排到前面
知识点:使用grep -o 可以获取每个字母
[root@y_zilong ~]# echo "Nice to meet you" |grep -o [a-z] |sort|uniq -c |sort -nr
3 e
2 t
2 o
1 y
1 u
1 m
1 i
1 c
[root@y_zilong ~]#