系统基础之文件管理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
下一篇 2016-08-07

相关推荐

  • rsyslog日志存储到mysql数据库中并利用loganalyzer进行web图形化分析管理

    概述     系统日志的重要性,相信大家都深有体会,当发生故障后,第一时间就是查看相关报错信息和日志信息,以定位问题所在,还可以基于日志,进行日志的分析,从而获取系统运行状态的一些规律,本篇就介绍关于系统日志的先关内容,具体分为:     1、rsyslog相关概念的介绍 &nb…

    Linux干货 2016-10-18
  • 周期性计划任务Cron详解

    概述 cron 是Linux 或者类 Unix 系统的作业调度程序。利用它,我们可以配置某些命令或者脚本,让命令和脚本在某个设定的时间内周期性地运行。本文主要以描述了 CentOS 6的cron命令的详细知识。 了解cron组件 cron程序由程序包cronie提供,我们可以通过rpm -qi cronie查看cronie的信息。 cron 的组件包括: 1…

    Linux干货 2016-09-18
  • iptables实验2

    IPTABLES   实验2       实验大体结构图       相关概念及公式 源地址转换:    SNAT   POSTROUTING 目标地址转换:DNAT  PREROUTING   每次重启网络后…

    Linux干货 2016-10-24
  • 一切皆文件

    Linux Linux内一切皆文件;表现之一:硬件设备也通过文件表示 物理终端:/dev/console 虚拟终端: /dev/tty#[1,6] 串行终端:/dev/ttyS# 伪终端: /dev/pts/# 注意:在启动设备之后,在其上关联一个用户接口程序,即可实现与用户交互,交互式程序有两类:GUI(图形化界面)和CLI(命令行界面)。 查看终端设备:…

    Linux干货 2017-04-01
  • Linux网络管理之网卡别名及网卡绑定配置

    在日常的运维工作当中,有时候需要在一块物理网卡上配置多个IP地址,这就是网卡子接口的概念,以及多块网卡实现网卡的绑定,通俗来讲就是多块网卡使用的一个IP地址,下面我来详细说明实现的过程。 &创建网卡子接口 在CentOS系统当中网络是由NetworkManager这个服务来管理的,它提供了一个图形的界面,但此服务不支持物理网卡子接口的设置,所以在配置网卡子接…

    系统运维 2016-09-02