系统基础之文件管理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

相关推荐

  • Linux基础知识之软件包管理(二)

    (1)CentOS7 yum dnf  yum repository: yum repo 存储了众多rpm包,以及包的相关的元数据文件(放置于特定目录下,repodata) 文件服务器: ftp:// http:// nfs:// file:/// (2)yum客户端: 配置文件: /etc/yum…

    Linux干货 2016-08-24
  • 第五周作业

    1、简述rpm与yum命令的常见选项,并举例 rpm 命令 linux的的包管理器,用于对rpm软件包,进行安装、升级、卸载、查询、校验等操作。 使用语法: rpm [OPTIONS] [PACKAGE_FILE] 常用选项: 安装: -i,–install 升级: -U,–update,-F,–freshen 卸载: -…

    Linux干货 2017-12-14
  • 马哥教育网络班22期+第2周课程练习

    一、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。     Linux上文件管理类命令常用的有:pwd、ls、cd、cp、touch、mv、rm、rmdir (1)pwd:显示当前路径,打印工作目录(Print Working Directory) (2)ls:查看指定目录下的内容 参数 说明 -a 列举目录中的…

    Linux干货 2016-08-29
  • free命令的介绍及参数的详细分析

           通过对free命令的深入学习,我觉得有必要整理一个文档,供自己和小伙伴们今后的复习使用。   free 命令的详细剖析      参数有:-b/k/m/g    分别以bytes/kilobytes/megabyte…

    2017-07-17
  • LAMP平台基于NFS实现web服务器负载均衡

    前言 随着访问量的逐渐增大,一台web服务器可能已经无法满足需求,这就需要增加web服务器的数量了,于是问题就来了:如何保证两台服务器数据同步呢?本文将讲解如何基于NFS实现web服务器的负载均衡及数据同步。 NFS服务介绍 NFS(Network File System)网络文件系统,允许一个系统通过网络共享目录和文件,通过使用NFS,用户和程序可以像访问…

    Linux干货 2015-04-22
  • first

    计算机可分为硬件(CPU ,存储器 ,输入设备 ,输出设备),软件(操作系统 ) 两大部份; CPU:Central Processing Unit 中央处理器 ,内部由运算器,控制器,寄存器,缓存器组成 ; 存储器 :(RAM:Random Access Memory 随机存储器)进行数据 暂存,断电后其内信息会丢失,如内存 ;(ROM:Read Only…

    Linux干货 2016-12-05