系统基础之文件管理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)
wencxwencx
上一篇 2016-08-07 22:06
下一篇 2016-08-07 22:06

相关推荐

  • nginx基础模块

    目录: nginx基础模块: 1.ngx_http_access_module模块: 2.ngx_http_auth_basic_module模块 3.ngx_http_stub_status_module模块 4.ngx_http_log_module模块 5.ngx_http_gzip_module: 6.ngx_http_ssl_module模块: 7…

    Linux干货 2017-08-08
  • N22-第四周作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。      2、编辑/etc/group文件,添加组hadoop。 3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为…

    Linux干货 2016-09-05
  • 根DNS域名解析的实现

    一、实现从根,com,rj.com 模拟互联网的DNS架构 DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串。 接下来就一起开始搭建吧 首先,我们需要计划好实验环境,包括实验的步骤思路 1)实验环境(最好是画图展示,能使思路清…

    2015-02-10
  • 链接的“软”与“硬”

    前言     类似Windows系统,Linux系统在进行文件管理时,也会引入链接概念。而链接又分为软链接和硬链接,两种链接适用于不同场合、不同用途,各有优缺点。在介绍软硬两种链接之前,需要先引入inode的概念。 Inode:     系统在管理文件时,为了有序寻址,会将元数据(metadata)和用户数据…

    Linux干货 2016-10-20
  • 如何练好yum的一招一式

      工作时间越久,就越有这样一个深刻体会,一个新知识或者新的技能一个人学习起来并不会觉得吃力,反而要想把你学习的新知识或者新技能给讲清楚让普通人听得懂才是最难的,之前我写过一篇博客讲述linux下的RPM包管理器,今天写的练好yum的一招一式,同样都是都是在linux系统的软件包的安装、卸载、升级等功能的,为什么我们还需要yum那,记得一位大哲学家…

    Linux干货 2015-11-10
  • 系统启动流程

    linux系统启动流程 内核的设计结构单内核:linux(线程–lwp轻量级进程)微内核:windows(支持真正意义上的多线程) 单内核:很多功能驱动都集成在一起 微内核:内核很小,功能单一。模块化 linux为了适应众多用户的不同硬件需求,linux内核在设计上采用模块化设计。可以动态加载模块。核心模块:ko 内核所独有的。共享对象:so 红…

    Linux干货 2016-09-19