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 16:16
下一篇 2016-08-08 16:16

相关推荐

  • 马哥教育网络班28期—第1、2天知识点概略

    没做随堂笔记,根据记忆手打,看看自己能记住多少。

    Linux干货 2017-12-03
  • 启动和内核管理

    启动和内核管理 :      CentOS 5和6的启动流程     服务管理     Grub管理     自制Linux     启动排错 &nb…

    Linux干货 2016-09-19
  • Linux Service and Security(Part 1)

    一、知识整理 1、不安全的传统协议: telnet、FTP、POP3等:不安全的密码; http、smtp、NFS等:不安全信息; Ldap、NIS、rsh等:不安全验证。 设计基本原则: 使用成熟的安全系统; 以小人之心度输入数据; 外部系统是不安全的; 最小授权; 减少外部接口; 缺省使用安全模式; 在入口处检查; 安全不是似是而非; 从管理上保护好系统…

    Linux干货 2016-10-09
  • Linux系统启动流程与内管管理(上)

    在讲linux系统启动流程之前,来讲讲linux的组成,这样能能帮助我们深入了解系统的启动流程,废话不多说直接上系统启动流程图 linux组成 linux:kernel+rootfs kenrel的作用:进程管理、内存管理、网络管理、驱动程序、文件系统、安全管理等   rootfs:程序和glibc 库:函数结合,function,调用接口(头文件…

    系统运维 2016-09-14
  • 磁盘管理及文件系统

    磁盘及文件系统管理初步与进阶(重点内容) 磁盘分区及文件系统 linux系统管理 磁盘分区及文件系统管理:分区工具 linux磁盘及文件系统管理 整个操作系统的硬件组成部分,最底层是硬件设备,计算能力得以运行的最根本的基础。 计算机的五大基本部件:cpu,运算器,控制器被整合到一起,由一个硬件部件来提供。 存储器(主存rom可编址的存储单元)。主板上有cpu…

    Linux干货 2016-08-30
  • tcpdump输出详解

    在这里不得不再吐槽下国内整个IT粗糙浮躁,度娘下来的中文文档几尽抄袭~google下来的文档英文文档质量远高于国内中文文档.用ie或没有安装插件的浏览器访问,不然可能会有其它访问请求数据干扰分析 IP数据包结构 TCP数据包结构 // tcpdump需root权限 # tcpdump -x -i eth1 i…

    Linux干货 2015-04-15