系统基础之文件管理grep练习题

1、显示/proc/meminfo文件中以大小s开头的行; (要求:使用三种方式)

[root@wen-7 ~]# grep -i "^s" /proc/meminfo 
SwapCached:           40 kB
SwapTotal:       2097148 kB
SwapFree:        2096840 kB
[root@wen-7 ~]# egrep  "^(s|S)" /proc/meminfo 
SwapCached:           40 kB
SwapTotal:       2097148 kB
SwapFree:        2096840 kB
[root@wen-7 ~]# egrep  "^[sS]" /proc/meminfo 
SwapCached:           40 kB
SwapTotal:       2097148 kB
SwapFree:        2096840 kB

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

[root@wen-7 ~]# grep -v "bash$"  /etc/passwd
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

3、显示用户rpc默认的shell程序

[root@wen-7 ~]# getent passwd rpc| egrep -o "[^:]+$"
/sbin/nologin

   

4、找出/etc/passwd中的两位或三位数

[root@wen-7 ~]# cat /etc/passwd| grep "\<[[:digit:]]\{2,3\}\>"
root:x:0:0:root,123,123,12345:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

     

5、显示/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行

[root@wen-7 ~]# cat  /etc/grub2.cfg | grep  "^[[:space:]]\+[^[:space:]]"
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry

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

[root@wen-7 ~]# netstat -tan| grep "LISTEN[[:space:]]*"
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN     
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN

 

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

[root@wen-7 ~]# cat /etc/passwd | egrep "^([^:]+\>).*\1$"
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

[root@wen-7 ~]# cat /etc/passwd | grep "\<\(.*\)\>.*/\1$"
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

[root@wen-7 ~]# cat /etc/passwd | egrep "^(.*)?:.*\1$"
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

9、显示当前系统root、 mage或wang用户的UID和默认shell

 [root@wen-7 ~]# cat  /etc/passwd |grep -E "(^root|wang|mage)\b"| cut -d: -f 1,3,7
  root:0:/bin/bash
  mageia:1100:/bin/bash
  rooter:0:/bin/bash
  wangcai:6011:/bin/bash

         

10、找出/etc/rc.d/init.d/functions文件中行首为某单词(包 括下划线)后面跟一个小括号的行

[root@wen-7 ~]# cat /etc/rc.d/init.d/functions | egrep "[[:alnum:]_]+\(\)"
checkpid() {
__pids_var_run() {
__pids_pidof() {

         

11、使用egrep取出/etc/rc.d/init.d/functions中其基名

[root@wen-7 ~]# echo "/etc/rc.d/init.d/functions" | grep -so "[^/]*.$"
functions

12、使用egrep取出上面路径的目录名

[root@wen-7 ~]# echo "/etc/rc.d/init.d/functions" | egrep -s "[^/]*./"
/etc/rc.d/init.d/functions

13、统计以root身份登录的每个远程主机IP地址的登录次数

[root@wen-7 ~]# last| egrep -o "^root\>.*"|tr -s " -r"|cut -d" " -f3| sort -n | egrep "([0-9]){2,5}"| uniq -c
     13 10.1.250.47
     83 172.18.19.1

14、利用扩展正则表达式分别表示0-9、 10-99、 100-199、200-249、 250-255

[0-9][1-9][0-9] 1[0-9][0-9] 2[0-9][0-9] 25[0-5]

              

15、显示ifconfig命令结果中所有IPv4地址

[root@wen-7 ~]# ifconfig | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
172.18.19.219
255.255.255.0
172.18.19.255

[root@wen-7 ~]#  ifconfig | egrep -o '([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}
|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.
([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
172.18.19.219
255.255.255.0
172.18.19.255
127.0.0.1

16、取本机ip地址

[root@wen-7 ~]# ifconfig eno16777736| grep "inet\>"|tr -s "[[:space:]]" ":"| cut -d: -f3

172.18.19.219

17、取各分区利用率的数值

[root@wen-7 ~]# df -h|grep  "^/dev/"| tr -s " " ":"| cut -d: -f1,5
/dev/mapper/centos-root:29%
/dev/sda1:32%
/dev/sr0:100%

18、统计/etc/init.d/functions 文件中每个单词出现的次数,并按频率从高到低显示

  [root@wen-7 ~]# cat /etc/rc.d/init.d/functions |tr -sc "(^[[:alpha:]].*)" "\n"| sort|uniq -c| sort -n
     50 echo
     52 file
     53 if
     59 pid

19、正则表达式表示身份证号

[root@wen-7 ~]# echo  "13040619950709187x"|  egrep -o "[1-9][0-9]{14}|[1-9][0-9]{17}|[1-9][0-9]{16}([0-9]|x)"
13040619950709187x
[root@wen-7 ~]# echo  "130406199507091870"|  egrep -o "[1-9][0-9]{14}|[1-9][0-9]{17}|[1-9][0-9]{16}([0-9]|x)"
130406199507091870
[root@wen-7 ~]# echo  "130406199507091870"|  egrep -o "[1-9][0-9]{14}|[1-9][0-9]{17}|[1-9][0-9]{16}([0-9]|x)"

20、正则表达式表示手机号

[root@wen-7 ~]# echo "15010570755" | grep "1[0-9]\{10\}"
15010570755

21、正则表达式表示邮箱

[root@wen-7 ~]# echo "1434421421qqsasf@wew.com" | grep "[[:alnum:]].*\@[[:alnum:]].*\.[[:alnum:]].*"
1434421421qqsasf@wew.com
[root@wen-7 ~]# echo "143294343432QQ@163.com" | grep "[[:alnum:]].*\@[[:alnum:]].*\.[[:alnum:]].*"
143294343432QQ@163.com

22、正则表达式表示QQ号

[root@wen-7 ~]# echo "43243243243" | grep "[0-9]\{5,11\}"
43243243243

23、查出分区空间使用率的最大百分比值

[root@wen-7 ~]# df -h | tr -s "[:space:]"| cut -d' ' -f1,5| grep "/dev" |sort -n| tail -n1
/dev/sr0 100%

24、查出用户UID最大值的用户名、 UID及shell类型

[root@wen-7 ~]# getent passwd| sort -n -t: -k3|tail -n1| cut -d: -f1,3,7
nfsnobody:65534:/sbin/nologin

25、查出/tmp的权限,以数字方式显示

[root@wen-7 ~]# stat -c %a /tmp/
 777
[root@wen-7 ~]# stat /tmp/| head -4| tail -1| cut -d" " -f1 | grep -o [0-9]|tr "\n" " "
0 7 7 7

 

26、统计当前连接本机的每个远程主机IP的连接数,并按从大 到小排序

[root@wen-7 ~]# nets6t -nt|tr -s " "| cut -d" " -f5|grep "[0-9]"| cut -d: -f1| uniq -c| sort
1 172.18.19.1

原创文章,作者:wencx,如若转载,请注明出处:http://www.178linux.com/30145

(0)
上一篇 2016-08-07 22:06
下一篇 2016-08-07 22:06

相关推荐

  • Linux用户和组管理

    进程以其发起者的身份运行; 进程对文件的访问权限,取决于发起此进程的用户的权限;   系统用户:为了能够让那后台进程或服务类进程以非管理员的身份运行,通常需要为此创建多个普通用户;这类用户从不用登录系统;     groupadd命令:添加组   groupadd [选项] group_name   -g G…

    Linux干货 2016-11-21
  • nginx

    1.Nginx的程序架构:        master/worker            一个master进程:     &nb…

    2017-06-19
  • 从Linux小白到大牛——与狼共舞的日子3

    马哥教育网络班21期+第3周课程练习 1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# who root :0   2016-07-23 08:55 (:0) wangsining pts…

    Linux干货 2016-07-26
  • mongodb及NoSQL入门学习总结

    分布式系统理论 一、 CAP: 分布式系统只能够,满足其中两个  1. Consistency : all nodes see the same data at the same time 一个节点修改后,需马上复制到第二个节点。如果网络故障,第二个节点将不能同步第一个节点更新的数据。这就是不能满足一致性。 2. Availibility:a guarant…

    2017-09-14
  • shell三剑客之grep

    正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。 给定一个正则表达式和另一个字符串,我们可以达到如下的目的: 1. 给定的字符串是否符合正则表达式的过滤逻辑(称作“匹配”); 2. 可以通过正则表达式,从字符串中获取我们想要的特定部分。 正…

    Linux干货 2016-08-08
  • 如何实现在命令行输入pwd时显示出ifconfig的效果

    1、使用type ifconfig 查看   2、使用type pwd 查看 如果还没有使用过pwd则显示如下,表示pwd属于内部命令,然后输入enable -n pwd 禁用这个内部命令   如果已经使用过,就会显示hash,已经缓存过   此时就不仅需要禁用内部命令,还需要使用hash -d pwd 清除pwd的缓存,使其在…

    2017-07-13