egrep小练习

1、显示/proc/meminfo文件中以大写s开头的行,要求使用两种方式

2、显示/etc/passwd文件中不以/bin/bash结尾的行

3、显示/etc/passwd文件中ID号最大的用户的用户名

4、如果root用户存在,显示其默认的shell程序

5、显示/etc/passwd文件中两位或三位数

6、显示/etc/rc.d/rc.sysinit文件中,至少以一个空白字符开头的且后面存在非空白字符的行

7、找出'netstat -tab'命令的结果中以'LISTEN'后跟0、1或多个空白字符结尾的行

8、添加用户bash,testbash,basher以及nologin,要求nologin的shell为/sbin/nologin,而后找出/etc/passwd文件中用户名同shell名的行

9、 显示当前系统root,centos或user1用户的默认shell和uid

10、 找出/etc/rc.d/init.d/functions文件中(CentOS 6),某个单词后面跟一个小括号的行

11、 echo 输出一个路径,使用egrep取出其基名,

12、 找出ifcofnig命令结果中1-255中间的任意数值

13、 找出ifconfig命令结果中的ip地址。


1、显示/proc/meminfo文件中以大写s开头的行,要求使用两种方式

1
2
# egrep '^(s|S)' /proc/meminfo
# egrep -i '^s' /proc/meminfo

2、显示/etc/passwd文件中不以/bin/bash结尾的行

1
# egrep -v '/bin/bash$' /etc/passwd

3、显示/etc/passwd文件中ID号最大的用户的用户名

4、如果root用户存在,显示其默认的shell程序

1
2
# id root > /dev/null 2>&1 && egrep '^root\b' /etc/passwd | cut -d':' -f1,7 --output-delimiter='^_^'
root^_^ /bin/bash

5、显示/etc/passwd文件中两位或三位数

1
# egrep -o '\<[[:digit:]]{2,3}\>' /etc/passwd

6、显示/etc/rc.d/rc.sysinit文件中,至少以一个空白字符开头的且后面存在非空白字符的行

1
# egrep '^[[:space:]]+[^[:space:]]' rc.sysinit

7、找出'netstat -tab'命令的结果中以'LISTEN'后跟0、1或多个空白字符结尾的行

1
# netstat -tan | egrep 'LISTEN[[:space:]]*'

8、添加用户bash,testbash,basher以及nologin,要求nologin的shell为/sbin/nologin,而后找出/etc/passwd文件中用户名同shell名的行

1
2
3
4
5
# egrep '(^[[:alnum:]]+\>).*\1$' /etc/passwd
sync :x:5:0: sync : /sbin : /bin/sync
shutdown :x:6:0: shutdown : /sbin : /sbin/shutdown
halt:x:7:0:halt: /sbin : /sbin/halt
nologin:x:2018:2018:: /home/nologin : /sbin/nologin

9、 显示当前系统root,centos或user1用户的默认shell和uid

1
2
3
4
# egrep '^(centos|root|user1)\>' /etc/passwd | cut -d':' -f1,7 --output-delimiter=' '
root  /bin/bash
centos  /bin/bash
user1  /bin/sh

10、 找出/etc/rc.d/init.d/functions文件中(CentOS 6),某个单词后面跟一个小括号的行

1)cat文件:有 字母 或 _ 符 和 () ,字符用 [ ]

1
# grep -E -o '[[:alpha:]_]+\(\)' /etc/rc.d/init.d/functions
1
# grep -o '[[:alpha:]_]\+()' /etc/rc.d/init.d/functions

11、 echo 输出一个路径,使用egrep取出其基名,

文件名的特性:<255, 非/ , . 开头为隐藏文件,区分大小写。

     [^/] :文件名,一定不能用/表示

1)取基名

1
# echo '/mnt/sdc' | egrep -o '[^/]+/?$'

2)取目录名

1
# echo '/mnt/sdc' | egrep -o '/[^/]+/'

12、 找出ifcofnig命令结果中1-255中间的任意数值

1
# ifconfig | egrep '\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'

13、 找出ifconfig命令结果中的ip地址。

1
# ifconfig | egrep '\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'
1
] # ifconfig | egrep '(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'

wKiom1mAFSjCGl5xAAAJgG6T_go924.png










本文转自 lccnx 51CTO博客,原文链接:http://blog.51cto.com/sonlich/1952631,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值