AIX中grep命令介紹

來源:互聯網
上載者:User

AIX中grep命令介紹 [plain]   www.2cto.com  grep三種變形  1.grep:標準命令,尋找文本,可使用Regex。  2.egrep:擴充grep,支援基本及擴充的Regex,但不支援\p模式範圍的應用。  3.fgrep:快速grep,允許尋找字串而不是一個模式。    在grep命令中,輸入字串參數時,最好將其用雙引號括起來。  grep命令選項:  -c:只輸出匹配行數  -i:不區分大小寫(只適用單字元)  -h:查詢多個檔案時,不顯示檔案名稱  -l:查詢多檔案時,只輸出包含匹配字元的檔案名稱  -n:顯示匹配行及行號  -s:不顯示不存在或無匹配文本的錯誤資訊  -v:顯示不包含匹配檔案的所有行    查詢多個檔案:grep "sort" *.doc  在所有檔案中,查詢"sort it":grep "sort it" *    查看data.f檔案的內容  pg data.f  48    Dec     3BC1997   LPSX    68.00   LVX2A  138  483   Sept    5AP1996   USP     65.00   LVX2C  189  47    Otc     3Z1998    LPSX    43.00   KVM9D  512  219   DEC     2CC1999   CAD     23.00   PLV2C  68  484   nov     7PA1998   CAD     49.00   PLV2C  234  483   may     5PA1998   USP     37.00   KVM9D  644  216   sept    3ZL1998   USP     86.00   KVM9E  234    包含“48”的行數:  grep -c "48" data.f  4    包含“48”的所有行:  grep "48" data.f  48    Dec     3BC1997   LPSX    68.00   LVX2A  138  483   Sept    5AP1996   USP     65.00   LVX2C  189  484   nov     7PA1998   CAD     49.00   PLV2C  234  483   may     5PA1998   USP     37.00   KVM9D  644    顯示匹配行的行號:  grep -n "48" data.f  1:48    Dec     3BC1997   LPSX    68.00   LVX2A  138  2:483   Sept    5AP1996   USP     65.00   LVX2C  189  5:484   nov     7PA1998   CAD     49.00   PLV2C  234  6:483   may     5PA1998   USP     37.00   KVM9D  644    顯示不包含“48”的行:  grep -v "48" data.f  47    Otc     3Z1998    LPSX    43.00   KVM9D  512  219   DEC     2CC1999   CAD     23.00   PLV2C  68  216   sept    3ZL1998   USP     86.00   KVM9E  234    忽略大小寫:  grep -i "sept" data.f  483   Sept    5AP1996   USP     65.00   LVX2C  189  216   sept    3ZL1998   USP     86.00   KVM9E  234    grep和Regex:  1.不匹配行首:  grep '^[^48]' data.f  219   DEC     2CC1999   CAD     23.00   PLV2C  68  216   sept    3ZL1998   USP     86.00   KVM9E  234  2.模式範圍:  grep '48[34]' data.f  483   Sept    5AP1996   USP     65.00   LVX2C  189  484   nov     7PA1998   CAD     49.00   PLV2C  234  483   may     5PA1998   USP     37.00   KVM9D  644  3.大小寫  grep '[Ss]ept' data.f  483   Sept    5AP1996   USP     65.00   LVX2C  189  216   sept    3ZL1998   USP     86.00   KVM9E  234  4.匹配任一字元    K後面3個字元,然後接著D:  grep 'K...D' data.f  47    Otc     3Z1998    LPSX    43.00   KVM9D  512  483   may     5PA1998   USP     37.00   KVM9D  644    K後面跟著2個字元,然後接著9:  grep 'K.\{2\}9' data.f  47    Otc     3Z1998    LPSX    43.00   KVM9D  512  483   may     5PA1998   USP     37.00   KVM9D  644  216   sept    3ZL1998   USP     86.00   KVM9E  234    頭2個字元為大寫字母,以C結尾,中間2個任一字元:  grep '[A-Z][A-Z]..C' data.f  483   Sept    5AP1996   USP     65.00   LVX2C  189  219   DEC     2CC1999   CAD     23.00   PLV2C  68  484   nov     7PA1998   CAD     49.00   PLV2C  234      grep '5..199[6,8]' data.f  483   Sept    5AP1996   USP     65.00   LVX2C  189  483   may     5PA1998   USP     37.00   KVM9D  644    匹配一行中的任意位置:  grep '[0-9][0-5][0-6]' data.f  47    Otc     3Z1998    LPSX    43.00   KVM9D  512  484   nov     7PA1998   CAD     49.00   PLV2C  234  483   may     5PA1998   USP     37.00   KVM9D  644  216   sept    3ZL1998   USP     86.00   KVM9E  234    匹配行首的位置:  grep '^[0-9][0-5][0-6]' data.f  216   sept    3ZL1998   USP     86.00   KVM9E  234    至少出現n次:  grep '4\{2,\}' data.f  483   may     5PA1998   USP     37.00   KVM9D  644  5.使用grep匹配“與”或者“或”模式  -E:允許使用擴充模式比對  grep -E '219|216' data.f  219   DEC     2CC1999   CAD     23.00   PLV2C  68  216   sept    3ZL1998   USP     86.00   KVM9E  234    空行:  [開發]/usr/xxxx/ytcclb>grep '^$' info.txt    [開發]/usr/xxxx/ytcclb>    匹配IP地址:  grep '[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}' *.address    類  類名及其等價的Regex  —————————————————————————————      類               等價的Regex  [[:upper:]]         [A-Z]  [[:lower:]]             [a-z]  [[:digit:]]             [0-9]  [[:alnum:]]         [0-9a-zA-Z]  [[:space:]]         空格或tab鍵  [[:alpha:]]         [a-zA-Z]  ——————————————————————————————    系統grep命令  結合管道命令,|,將左邊的輸出結果作為右邊命令的輸入  尋找目錄列表中的列表:ls -l | grep '^d'  查詢目錄中不包含目錄的所有檔案:ls -l | grep '^[^d]'  [/home/xxxx/ytcxx]> ls -l | grep '^d.....x..x'  drwxr-xr-x    4 xxxx   db2iadm1        256 Nov 15 2010  cdxx  drwxr-xr-x    2 xxxx   db2iadm1       4096 Dec  6 2006  incl  drwxr-xr-x    2 xxxx   db2iadm1        256 Nov 25 2010  src  drwxr-xr-x    2 xxxx   db2iadm1        256 Nov 25 2010  tbin  drwxr-xr-x    3 xxxx   db2iadm1       4096 Jul  5 2011  test  drwxr-xr-x    5 xxxx   db2iadm1        256 Sep 10 2010  tmp    ps命令,查詢洗頭膏上啟動並執行進程。  [/home/xxxx/ytcclb]> ps ax | grep "named"   376944  pts/0 A     0:00 grep named  [/home/xxxx/ytcclb]> ps -ef | grep "named"    xxxx 573474 557246   0 10:20:42  pts/0  0:00 grep named  字串使用grep命令:  STR="Mary Joe Peter Pauline"  echo $STR | grep "Mary"  Mary Joe Peter Pauline    egrep:expression或extended grep,接受所有的Regex。  egrep '(3ZL|2CC)' data.f  219   DEC     2CC1999   CAD     23.00   PLV2C  68  216   sept    3ZL1998   USP     86.00   KVM9E  234   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.