linux命令系列 grep

grep, egrep, fgrep - print lines matching a patternlinux

  SYNOPSIS
    grep [OPTIONS] PATTERN [FILE...]
    grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]git

 

linux系統中grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來,grep全稱是Global Regular Expression Print正則表達式

1. 經常使用選項:express

  -E, --extended-regexp: Interpret PATTERN as an extended regular expression.   # 開啓擴展(Extend)的正則表達式工具

  -i, --ignore-case: Ignore case distinctions in both the PATTERN and the input files.   # 忽略大小寫spa

  -v, --invert-match: Invert the sense of matching, to select non-matching lines.  # 反過來,只打印沒有匹配的,而匹配的反而不打印regexp

       -n, --line-number: Prefix each line of output with the 1-based line number within its input file.  # 顯示行號orm

  -w, --word-regexp  # 被匹配的文本只能是單詞,而不能是單詞中的某一部分,如文本中有liker,而我搜尋的只是like,就能夠使用-w選項來避免匹配liker字符串

    Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-wordunderscore

    constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the
    underscore.

  -c, --count: Suppress normal output; instead print a count of matching lines for each input file.  # 顯示總共有多少行被匹配到了,而不是顯示被匹配到的內容,注意若是同時使用-cv選項是顯示有多少行沒有被匹配到。

  -o, --only-matching: Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.  # 只顯示被模式匹配到的字符串。

  -A NUM, --after-context=NUM: Print NUM lines of trailing context after matching lines.   # 顯示匹配到的字符串所在的行及其後NUM行

  -B NUM, --before-context=NUM: Print NUM lines of leading context before matching lines.  # 顯示匹配到的字符串所在的行及其前NUM行

  -C NUM, -NUM, --context=NUM: Print NUM lines of output context.  # 顯示匹配到的字符串所在的行及其先後各NUM行

2. 模式部分:

  (a)基本正則表達式

    匹配字符

      .  : 任意一個字符

      [abc] : 表示匹配[abc]中任意一個字符

      [a-zA-Z] : 匹配a-z或A-Z之間任意一個字符

      [^123] : 匹配123以外的任意一個字符

  

      對於經常使用的字符集,系統定義以下:

        [a-zA-Z] <=> [[:alpha:]]

        [0-9] <=> [[:digit:]]

        [a-zA-Z0-9] <=> [[:alnum:]]

        tab,space <=> [[:space:]]

        [A-Z] <=> [[:upper:]]

        [a-Z] <=> [[:lower:]]

        標點符號 <=> [[:punct:]]      

    

    匹配次數:

      \{m,n\} : 匹配其前出現的字符至少m次,至多n次

      \? : 匹配其前出現的內容0次或1次,等價於\{0,1\}

      * : 匹配其前出現的內容任意次,等價於\{0,\} ,因此".*"表示任意字符任意次 

 

    位置錨定:

      ^ : 錨定行首

      $ : 錨定行尾。 經常使用技巧 "^$"匹配空白行

      \b或者\<: 錨定單詞的詞首。 如"\blike" 不會匹配alike,可是會匹配liker

      \b或者\> : 錨定單詞的詞尾。 如\blike\b 不會匹配alike或者liker,只會匹配like

      \B : 與\b做用相反

 

    分組及引用:

      \(string\) : 將string做爲一個總體方便後面引用

      \n : 引用第n個左括號及其對應的右括號所匹配的內容

 

3. 擴展的(Extend)正則表達式:

  匹配字符: 這部分和基本正則表達式一致

  匹配次數: 

    * : 和基本正則表達式一致

    ? :  相比基本正則表達式沒有\

    {m,n} : 相比基本正則沒有\

    + : 匹配其前面的字符至少一次,至關於{1,} 

  位置錨定: 這部分和基本正則表達式一致

  分組及引用: 

    (string) : 相比基本正則表達式沒有\

    \n : 和正則表達式同樣

  或者:

    a|b : 匹配a或者b, 注意a是指 | 的左邊的總體,b也同理,好比C|cat,表示的是C或者cat,而不是Cat或者cat,若是要表示Cat或者cat,則應該寫爲(C|c)at。(String)除了用於引用還用於分組

    

注1:默認狀況下,正則表達式的匹配工做在貪婪模式下,也就是說它會盡量長地去匹配,好比某一行有字符串 abacb,若是搜索內容爲 "a.*b" 那麼會直接匹配 abacb這個串,而不會只匹配ab或acb。

注2:全部的正則字符,如 [ 、* 、( 等,若要搜索 * ,而不是想把 * 解釋爲重複先前字符任意次,能夠使用 \* 來轉義。

相關文章
相關標籤/搜索