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

相关推荐

  • 内核参数修改 内核编译 第14天

    Linux内核:单内核,模块化 内核的某些模块 编译进内核本体 [*] 编译成内核模块 [M] 不选择使用     [ ] 内核的组成部分 /boot/vmlinuz-VERSION /lib/modules/VERSION/ *.ko 模块间有可能有依赖关系 内核模块管理 lsmod:显…

    Linux干货 2016-01-18
  • 阿里云修改hostname主机名的一点小技巧

    CentOS 7以后修改主机名一般使用: hostnamectl set-hostname newhostname 如果仍然无效,使用vim打开/etc/cloud/cloud.cfg,将 preserve_hostname=fale 改为 preserve_hostname=true 即可。 以上在阿里云ECS上亲测有效,使用了网上查阅…

    Linux干货 2017-01-09
  • Http请求流程

    1、http请求流程 1.连接 当我们输入这样一个请求时,首先要建立一个socket连接,因为socket是通过ip和端口建立的,所以之前还有一个DNS解析过程,把www.mycompany.com变成ip,如果url里不包含端口号,则会使用该协议的默认端口号。 DNS的过程是这样的:首先我们知道我们本地的机器上在配置网络时都会填写DNS,这样本机就会把这个…

    Linux干货 2017-02-13
  • 20161028第9天作业

    20161028第9天作业 1、编写脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问” 2、编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率率,如果超过80%,就发广播…

    Linux干货 2016-10-30
  • 实验:配置静态路由2

    实验:配置静态路由2 IP地址规划如下: VMware实验环境下给网卡分配不同的VMnet以实现物理分隔广播域: Client1-VMnet1: R1-VMnet1-VMnet2: R2-VMnet2-VMnet3: R3-VMnet3-VMnet4: Client2-VMnet4: 注意,环境准备: 1、MAC地址不要有冲突,如果是复制的虚拟机,对于cen…

    2017-03-26
  • Linux发行版的基础目录名称、命名法则及功能规定

    Linux发行版的基础目录名称、命名法则及功能规定 / 主层次的根,也是整个文件系统层次结构的根目录  /bin 存放系统的命令。  /boot 存放系统的启动文件,及其内核。  /dev 系统设备文件主目录。  /etc 系统主要配置文件主目录。  /home 普通用户家目录。  /lib 系统库…

    Linux干货 2016-10-31