jackcui0804作业

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

[root@centos7 ~]# cat /proc/meminfo | grep -e "^s.*" -e "^S.*"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             13472 kB
Slab:             117520 kB
SReclaimable:      69404 kB
SUnreclaim:        48116 kB
[root@centos7 ~]# cat /proc/meminfo | grep -e "^[sS].*"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             13472 kB
Slab:             117520 kB
SReclaimable:      69404 kB
SUnreclaim:        48116 kB
[root@centos7 ~]# cat /proc/meminfo | grep -E "^(s|S).*"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             13472 kB
Slab:             117520 kB
SReclaimable:      69404 kB
SUnreclaim:        48116 kB

 

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

[root@centos7 ~]# grep  -v  "/bin/bash$" /etc/passwd

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

[root@centos7 ~]# grep "^rpc\>.*" /etc/passwd |cut -d: -f 7
/sbin/nologin

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

[root@centos7 ~]# grep -Eo  "[1-9][[:digit:]]{1,2}" /etc/passwd  //o选项只打印匹配到的内容 
12
11
12
100
14
50
99

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

[root@centos7 ~]# grep "^[[:space:]][^[:space:]].*" /etc/grub2.cfg
         load_video
         set gfxpayload=keep
         insmod gzio
         ……

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

[root@centos7 ~]# netstat -tan | grep -e "LISTEN[[:space:]]*$"
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@centos7 ~]# grep -E "(^[[: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
bash:x:1008:1008::/home/bash:/bin/bash
basher:x:1010:1010::/home/basher:/bin/bash

7)找出/etc/rc.d/init.d/functions 文件中行首为某单词(

括下划线)

grep -e  "[[:alpha:]_]*().*"  /etc/rc.d/init.d/functions

 

8)使用egrep 取出/etc/rc.d/init.d/functions基名,和目录名:

 

echo /etc/rc.d/init.d/functions | grep -E  "[^/][[:alpha:]]+$" -o
echo /etc/rc.d/init.d/functions | grep -E  "[[:alpha:]/]+.*/"  -o

 

 

 

9)利用扩展正则表达式分别表示0-9 10-99 100-199

200-249 250-255

 

[root@cnode6_8 ~]# grep -we "[[:digit:]]\{1,1\}" a.log  //0-9
0 1 2 22 34 4 5 10 11 111 23 100 123 234 244 250 10000 999
[root@cnode6_8 ~]# grep -we "[1-9][[:digit:]]" a.log  //10-99
01  0 1 2 22 34 4 5 10 11 111 23 100 123 234 244 250 10000 999
 
[root@cnode6_8 ~]# grep -we "1[[:digit:]][[:digit:]]" a.log  //100-199
 
[root@cnode6_8 ~]# grep -we "2[0-4][[:digit:]]" a.log  //200-249
[root@cnode6_8 ~]# grep -we "25[[0-5]]" a.log

 

10)显示ifconfig命令结果中ipv4地址

 
[root@cnode6_8 ~]# ifconfig| grep 'inet\>'|cut -d: -f2|tr -s " " |cut -d " " -f1

 

统计/etc/rc.d/init.d/function中单词出现的次数

cat /etc/rc.d/init.d/functions |tr -c  "[[:alnum:]]" "\n"|tr "[]"  "\n"|tr -s "[[:space:]]"|sort |uniq –c

 

11)用正则表达式表示手机号11 13 17 15  18

grep -e "1[13578][[:digit:]]\{9\}[^[:digit:]]

用正则表达式表示身份证号18

[root@centos7 ~]# grep  -E  "\<[1-9][[:digit:]]{16}[[:digit:]x]\>" aa
123456789012345678

 

12)用正则表达式表示邮箱

[root@centos7 ~]# grep -E "\<[[:alnum:]_]{1,16}@[[:alnum:]]{1,20}.com"  mail_test.txt

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

(0)
jack_cuijack_cui
上一篇 2016-08-08
下一篇 2016-08-08

相关推荐

  • Storm集群安装详解

    storm有两种操作模式: 本地模式和远程模式。 本地模式:你可以在你的本地机器上开发测试你的topology, 一切都在你的本地机器上模拟出来;  远端模式:你提交的topology会在一个集群的机器上执行。 本文以Twitter Storm官方Wiki为基础,详细描述如何快速搭建一个Storm集群,其中,项目实践中遇到的问题及经验总结,在相应章…

    Linux干货 2015-04-04
  • Ansible浅谈

    ansible特性:         模块化,调用特定的模块,完成特定的任务;         基于Python语言实现,由Paramiko、PyYAML和Jinja2三个关键模块;         部署简单,agentless; &nbs…

    Linux干货 2016-12-15
  • LVM(逻辑卷管理)

    LVM 逻辑卷管理 概述:      LVM是将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性。与传统的磁盘与分区相比,LVM为计算机提供了更高层次的磁盘存储。它使系统管理员可以更方便的为…

    Linux干货 2016-09-05
  • 马哥教育网络19期+第五周练习博客

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;   grep "^[[:space:]]\+.*" /boot/grub/grub.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; &n…

    Linux干货 2016-06-19
  • 网络接口bonding的设置、网卡别名的设置

    网络接口bonding的设置、网卡别名的设置 网络接口bonding的设置 1.bonding的原理 Bonding就是将多块网卡绑定同一IP 地址对外提供服务,可以实现高可用或者负载均衡。当然,直接给两块网卡设置同一IP 地址是不可能的。通过bonding ,虚拟一块网卡对外提供连接,物理网卡的被修改为相同的MAC 地址。 2.Bonding 的工作模式 …

    Linux干货 2016-09-05
  • Linux文本处理工具grep,egrep

    简介:     grep即(Global search REgular expression and Print out the line)全局的搜索正则表达式并且打印显示出来。     通俗点讲:根据用户指定的文本模式(搜索条件)对目标文件进行逐行搜索,显示能匹配到的行。 &n…

    Linux干货 2015-05-11