grep练习题

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 ~]# 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

y_zilong

一分钱的肯定

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值