国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學習啦>學習電腦>操作系統(tǒng)>Linux教程>

    Linux grep指令的詳解

    時間: 志藝942 分享

      Linux下grep指令跟find指令一樣的強大,find主要查找文件,而grep則是內(nèi)容,兩者配合相得益彰,接下來是小編為大家收集的Linux grep指令的詳解,歡迎大家閱讀:

      Linux grep指令的詳解

      1.作用

      Linux系統(tǒng)中g(shù)rep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹配的行打印出來。grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權(quán)限是所有用戶。

      2.主要參數(shù)

      [options]主要參數(shù):

      -c:只輸出匹配行的計數(shù)。

      -I:不區(qū)分大小寫(只適用于單字符)。

      -h:查詢多文件時不顯示文件名。

      -l:查詢多文件時只輸出包含匹配字符的文件名。

      -n:顯示匹配行及 行號。

      -s:不顯示不存在或無匹配文本的錯誤信息。

      -v:顯示不包含匹配文本的所有行。

      pattern正則表達式主要參數(shù):

      \: 忽略正則表達式中特殊字符的原有含義。轉(zhuǎn)義

      ^:匹配正則表達式的開始行。

      $: 匹配正則表達式的結(jié)束行。

      \<:從匹配正則表達式的行開始。

      \>:到匹配正則表達式的行結(jié)束。

      [ ]:單個字符,如[A]即A符合要求 。

      [ - ]:范圍,如[A-Z],即A、B、C一直到Z都符合要求 。

      。:所有的單個字符。

      * :有字符,長度可以為0。

      3.grep命令使用簡單實例

      注意:在輸入要搜索的字符串時最好使用雙引號/而在模式匹配使用正則表達式時,注意使用單引號

      $ grep ‘test' d* -r <==> grep test d* -r 即'test' <==> test

      顯示所有以d開頭的文件中包含 test的行。

      $ grep ‘test' aa bb cc -r

      顯示在aa,bb,cc文件中匹配test的行。

      $ grep ‘[a-z]\{5\}' aa

      顯示所有包含每個字符串至少有5個連續(xù)小寫字符的字符串的行。

      默認情況下,'grep'只搜索當前目錄。如果此目錄下有許多子目錄,明確要求搜索子目錄:grep -r

      4、舉例 more size.txt(主要copy人家的,站在高手的肩膀上)

      # more size.txt

      b124230

      b034325

      a081016

      m7187998

      m7282064

      a022021

      a061048

      m9324822

      b103303

      a013386

      b044525

      m8987131

      B081016

      M45678

      B103303

      BADc2345

      # more size.txt | grep '[a-b]' //范圍 :如[A-Z]即A,B,C一直到Z都符合要求 即打印有a-b的行--忽略大小寫(可能是系統(tǒng)相關(guān)性)

      b124230

      b034325

      a081016

      a022021

      a061048

      b103303

      a013386

      b044525

      # more size.txt | grep '[a-b]'* //與上一指令結(jié)果相同--測試結(jié)果

      b124230

      b034325

      a081016

      m7187998

      m7282064

      a022021

      a061048

      m9324822

      b103303

      a013386

      b044525

      m8987131

      B081016

      M45678

      B103303

      BADc2345

      # more size.txt | grep 'b' //單個字符;如[A] 即A符合要求 輸出有b的行

      b124230

      b034325

      b103303

      b044525

      # more size.txt | grep '[bB]' //輸出有B或b的行

      b124230

      b034325

      b103303

      b044525

      B081016

      B103303

      BADc2345

      # grep 'root' /etc/group //輸出/etc/group目錄下有root的行

      root::0:root

      bin::2:root,bin,daemon

      sys::3:root,bin,sys,adm

      adm::4:root,adm,daemon

      uucp::5:root,uucp

      mail::6:root

      tty::7:root,tty,adm

      lp::8:root,lp,adm

      nuucp::9:root,nuucp

      daemon::12:root,daemon

      # grep '^root' /etc/group //^:匹配正則表達式的開始行-->以root開始的行

      root::0:root

      # grep 'root$' /etc/group //$: 匹配正則表達式的結(jié)束行-->以root結(jié)束的行

      root::0:root

      mail::6:root

      # more size.txt | grep -i 'b1..*3' // -i :忽略大小寫

      b124230

      b103303

      B103303

      # more size.txt | grep -iv 'b1..*3' //-v :查找不包含匹配項的行

      b034325

      a081016

      m7187998

      m7282064

      a022021

      a061048

      m9324822

      a013386

      b044525

      m8987131

      B081016

      M45678

      BADc2345

      # more size.txt | grep -in 'b1..*3' //-n:顯示匹配行及 行號。

      1:b124230

      9:b103303

      15:B103303

      # more size.txt

      the test file

      their are files

      The end

      # grep 'the' size.txt

      the test file

      their are files

      # grep '\

      the test file

      their are files

      # grep 'the\>' size.txt //行結(jié)束 \>

      the test file

      # grep '\' size.txt //不知道如何表達,呵呵。

      the test file

      # grep '\<[Tt]he\>' size.txt //并且輸出有The或the的行

      the test file

      The end

      # grep '[239].' size.txt //輸出所有含有以2,3或9開頭的,并且是兩個數(shù)字的行

      # grep '^[^the]' size.txt //不匹配行首是the的行

      # grep -E 'The|test' size.txt //顯示含有The或test的行

      
    看了“Linux grep指令的詳解”還想看:

    1.深入Linux grep指令的詳解

    2.linux grep搜索命令的使用方法

    3.Linux下如何使用grep命令搜索多個單詞

    4.Linux文本匹配命令grep與fgrep使用全解

    5.linux grep命令詳解

    2990882