強大的grep,sed和awk--用案例來說解

準備工做:html

  先簡單瞭解grep,sed和awk功能  node

  1) grep 顯示匹配特定模式的內容正則表達式

    grep -v 'boy' test.txt 過濾掉test.txt文件的boy,顯示其他內容ui

    grep 'boy' test.txt 顯示test.txt文件中,和boy匹配的內容this

    -E 同時過濾多個"a|b"spa

    -i 不區分大小寫htm

    --color=auto 設置顏色blog

  2)sed 取各類內容,以行爲單位取內容ip

    -n取消默認輸出字符串

    p=print

    d=delete 

  3)awk 取列

    -F 指定分割符 如對「I am a student」 以空格爲分割符,其將被分爲4列,awk裏有參數能夠去任意列

    NF 表示當前行記錄域或列的個數

    NR 顯示當前記錄號或行號

    $1第一列 $2第二列 $0整行 $NF 最後一列        

案例一:如何過濾出em1的ip地址

[zhaohuizhen@localhost Test]$ ifconfig em1
em1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254
inet6 fe80::b283:feff:fed9:6a9a prefixlen 64 scopeid 0x20<link>
ether b0:83:fe:d9:6a:9a txqueuelen 1000 (Ethernet)
RX packets 13908772 bytes 4072069839 (3.7 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 982482 bytes 86260856 (82.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 40

步驟一:

  首先應該過濾出第二行inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254內容

  方法一:grep命令  

    [zhaohuizhen@localhost Test]$ ifconfig em1 | grep 'inet '
      inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254

  方法二:用sed命令 

    [zhaohuizhen@localhost Test]$ ifconfig em1 | sed -n '2p'
      inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254

  方法三:用awk命令

    [zhaohuizhen@localhost Test]$ ifconfig em1 | awk NR==2

      inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254

  方法四:用head,tail命令

    [zhaohuizhen@localhost Test]$ ifconfig em1 | head -2 | tail -1
      inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254

步驟二:

  過濾出第二行後,在過濾出ip地址

  方法一:用cut命令   

    [zhaohuizhen@localhost Test]$ ifconfig em1 | sed -n '2p' | cut -c 14-25
      10.0.0.8

    [zhaohuizhen@localhost Test]$ ifconfig em1 | grep 'inet ' | cut -d" " -f10
      10.0.0.8

  方法二:用awk命令

    [zhaohuizhen@localhost Test]$ ifconfig em1 | grep 'inet ' | awk -F '[ ]+' '{print $3}'
      10.0.0.8

    用awk命令能夠直接處理第二行,不用先將其過濾出來    

    [zhaohuizhen@localhost Test]$ ifconfig em1 | awk -F '[ ]+' 'NR==2 {print $3}'
      10.0.0.8

  方法三:用sed命令

    [zhaohuizhen@localhost Test]$ ifconfig em1 | sed -n '/inet /p' | sed 's#^.*et ##g' | sed 's# net.*$##g'
      10.0.0.8

    此處用到了正則表達式(見http://www.cnblogs.com/ZGreMount/p/7656365.html),匹配的目標前面的字符串通常以^.*開頭,表明以任意字符開頭,結尾寫上要匹配的字符前面的幾個字符,        如"^.*addr "就匹配"                              inet addr ",而處理的目標後的內容則是開頭寫上要匹配字符後幾個字符,加上以.*$。如,「 Bcast:.*$」就匹配「 Bcast:10.0.0.254 Mask:255.255.255.」

  注:sed小括號分組功能

    sed ‘s/********/......./標籤’  #斜線能夠被其它字符替換

    前兩條斜線中間部份內容********,能夠使用正則表達式,後兩條斜線中間內容.......不能使用正則表達式。

    ()是分組,在前面部分使用()括起來的內容,在後面部分能夠使用\1調用前面括號內內容。

    若是有多個括號,那麼依次是\1,\2,\3,以此類推。

    例如,直接取em1ip地址,不先過濾出第二行

    [zhaohuizhen@localhost Test]$ ifconfig em1 | sed -n 's#^.*inet \(.*\) net.*$#\1#gp'
      10.0.0.8

    直接取出ip地址和子網掩碼

    [zhaohuizhen@localhost Test]$ ifconfig em1 | sed -n 's#^.*inet \(.*\) n.*k \(.*\) bro.*$#\1 \2#gp'
      10.0.0.8 255.255.255.0

案例二:輸出文件a對應權限664

    [zhaohuizhen@localhost Test]$ ll a
      -rw-rw-r--. 1 zhaohuizhen zhaohuizhen 98 Oct 12 20:24 a

    方法一:用awk命令

    [zhaohuizhen@localhost Test]$ ll a | awk '{print $1}'|tr rwx- 4210|awk -F "" '{print $2+$3+$4 $5+$6+$7 $8+$9+$10}'
      664

    解析:

      1)ll a 長格式顯示文件a    

      [zhaohuizhen@localhost Test]$ ll a
        -rw-rw-r--. 1 zhaohuizhen zhaohuizhen 98 Oct 12 20:24 a

      2)用awk命令,以空格爲分隔符,取出第一列

      [zhaohuizhen@localhost Test]$ ll a | awk '{print $1}'
        -rw-rw-r--.

      3)用tr命令將rwx- 替換爲4210

      [zhaohuizhen@localhost Test]$ ll a | awk '{print $1}'|tr rwx- 4210
        0420420400.

      4)用awk將上面的結果分割,而後相加得出結果

      [zhaohuizhen@localhost Test]$ ll a | awk '{print $1}'|tr rwx- 4210|awk -F "" '{print $2+$3+$4 $5+$6+$7 $8+$9+$10}'
        664

    方法二:用stat命令

      [zhaohuizhen@localhost Test]$ stat a
      File: ‘a’
      Size: 98 Blocks: 8 IO Block: 4096 regular file
      Device: fd02h/64770d Inode: 203491 Links: 1
      Access: (0664/-rw-rw-r--) Uid: ( 1002/zhaohuizhen) Gid: ( 1002/zhaohuizhen)
      Context: unconfined_u:object_r:user_home_t:s0
      Access: 2017-10-14 09:20:34.337529787 +0800
      Modify: 2017-10-12 20:24:27.512609708 +0800
      Change: 2017-10-12 20:24:27.536609708 +0800
      Birth: -

    1)命令stat a結果包含文件a對應權限644,能夠用前面的方法直接過濾出來

    [zhaohuizhen@localhost Test]$ stat a | awk -F '[(/]' 'NR==4 {print $2}'
      0664

    2)stat命令包含須要結果,考慮stat命令是否有參數能夠直接得到咱們須要的結果

    [zhaohuizhen@localhost Test]$ stat -c %a a
      664

案例三:輸出文件a內容,不帶空行,文件a內容以下:

    [zhaohuizhen@localhost Test]$ cat a
    "hello,this is a test"
    I am a studeng My QQ is 1534612574

    computer

    book

    river
    tree

    man
    computer

    book
    river
    tree
    man

  方法一:grep命令

    [zhaohuizhen@localhost Test]$ grep -v '^$' a
    "hello,this is a test"
    I am a studeng My QQ is 1534612574
    computer
    book
    river
    tree
    man
    computer
    book
    river
    tree
    man

    註釋:-v 即排除;^$,開頭和結尾間沒有任何東西,即空行

  方法二:用sed命令

[zhaohuizhen@localhost Test]$ sed '/^$/d' a
"hello,this is a test"
I am a studeng My QQ is 1534612574
computer
book
river
tree
man
computer
book
river
tree
man

註釋:^$表明空行,d即delete

方法三:用awk命令

[zhaohuizhen@localhost Test]$ awk /[^$]/ a
"hello,this is a test"
I am a studeng My QQ is 1534612574
computer
book
river
tree
man
computer
book
river
tree
man

    註釋:^$表明空行,放在[]中表明非,即不匹配空行

相關文章
相關標籤/搜索