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.www58058.com/30110

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

相關推薦

  • 第二周作業

    一Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。 which命令:顯示(shell)命令全部的路徑 which [option] programmame […] –skip-alias:忽略別名   whereis命令:搜索二進制程序,源碼,和幫助手冊頁的命令 whereis [option] na…

    Linux干貨 2017-02-18
  • OpenSSL:實現創建私有CA、簽署證書請求詳解

    一、OpenSSL:CA默認配置信息     1.證書簽發機構CA:公共信任CA、私有CA                建立私有CA方式如下: 小范圍測試使用openssl、 大…

    Linux干貨 2016-04-30
  • CentOS6/7下不關機識別新添加的scsi硬盤

    1)需求說明 在虛擬機中,我們在服務器開機的狀態下添加新的磁盤或者說從存儲上映射某個LUN區域給服務器,不重啟系統的情況下,往往不能夠直接識別到磁盤,在遇到這種情況的時候,我們可以讓系統重新掃描讓服務器重新識別到磁盤。 2)處理步驟 下面看一下在系統不重啟的情況,如何讓系統認識新的磁盤,并能對其分區與格式化 1、在開機狀態下新增磁盤 2、執行下面的命令 ec…

    Linux干貨 2016-07-22
  • 第二周練習題

    創建/tmp目錄下的:a_c,a_d,b_c,b_d; ~]# mkdir -pv /tmp/{a_{c,d},b_{c,d}} 創建如下目錄: ~]# mkdir -pv /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,…

    Linux干貨 2016-11-01
  • N_28 正則表達式的一些基本用法

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 ~]# cp -r /etc/skel /home/tuser1 ~]# chmod -R -g— -o— /home/tuser1 2、編輯/etc/group文件,添加組hadoop。 ~]#vi…

    Linux干貨 2017-12-23
  • LVS集群類型

     lvs:Linux Virtual Server         l4:四層路由、四層交換          根據請求報文的目標IP和目標PORT將其調度轉發至后端的某主機;      IPTABLES:  …

    Linux干貨 2017-01-10
欧美性久久久久