Summary of common grep usage

Source: Internet
Author: User
Tags perl regular expression egrep
Grep-elease 2. kernel 2.txt grep-Eh (ell | kernel displays grep-E ^ (h | y) tes not developed by h.

Grep-E l \ {2, \} 2.txt grep-E h (ell | a) o test.txt grep [a-z] \ {5, \} test.txt grep-xf a.txt B .txt // query the row grep-vxf B .txt a.txt that a and B exist together. // query that row a is not in file B, is the line grep-v ^ h test.txt that a has more than B. // displays grep-E ^ (h | y) tes not developed by h.

Grep-E 'l \ {2, \} '2.txt grep-E' h (ell | a) o 'test.txt grep '[a-z] \ {5, \} 'test.txt grep-xf a.txt B .txt // query the row grep-vxf B .txt a.txt that a and B exist together. // The row that queries a is not in file B, is the line grep-v '^ H' test.txt that a has more than B. // displays grep-E' ^ (h | y) not developed by h) 'test.txt //-E supports extended regular expressions, equivalent to egrep grep-A 1 'hello' test.txt // In addition to displaying matched rows, it also shows the grep-B 1 'hello' test.txt // In addition to displaying matched rows it also displays the previous line grep-C 1 'hello' test.txt // In addition to displaying the matched rows, It also displays the previous and subsequent lines grep-r "hello" Xx // search for grep-c in the xx directory // count the number of matched rows grep-n // display the row number grep-v '^ hello 'test.txt // display rows not starting with hello -- clever writing, the first digit is not developed by h, and the second digit is not developed by e... Grep '^ [^ h] \ | ^. [^ e] \ | ^ .. [^ l] \ | ^... [^ l] \ | ^ .... [^ o] '[options] Main Parameter:-c: only counts matching rows are output. -I: It is case-insensitive (only applicable to single characters ). -H: When querying multiple files, the file name is not displayed. -L: When querying multiple files, only names containing matching characters are output. -N: displays matching rows and row numbers. -S: the error message that does not exist or does not match the text is not displayed. -V: displays all rows that do not contain matched text. Command Regular Expression :. match a single character such .. X yiX can find out ^ match the beginning of A line ^ d the first character in each line is d $ match the end of A line T $ the last character in each line is T * match any string \ shield the meaning of special characters \ {2 \} B Letter A appears twice A \ {2, \} B appears at least twice A \ {2, 4 \} B appears 2 to 4 times [0-9] \ {4 \} xx [0-9] \ {4 \} the first four are numbers, in the middle is xx, and the last four are the main parameters of the number pattern Regular Expression: \: Ignore the original meaning of special characters in the regular expression. ^: Match the start line of the regular expression. $: Matches the end row of the regular expression. \ <: Starts from the row that matches the regular expression. Anchor specifies the start of a word, for example :/\
 
  
: Ends with the row matching the regular expression. []: A single character. For example, [A] indicates that A meets the requirements. [-]: Range, such as [A-Z], that is, A, B, C Until Z all meet the requirements ..: All single characters. *: There are characters. The length can be 0 [^] to match a character not in the specified range, for example: /[^ A-RT-Z] ed/match a line that starts with a letter that does not contain the A-R and T-Z, followed by ed. + Match the previous subexpression once or multiple times. For example, 'Zo + 'can match "zo" and "zoo", but cannot match "z ". + Is equivalent to {1 ,}.? Match the previous subexpression zero or once. For example, "do (es )? "Can match" do "in" do "or" does ".? It is equivalent to {0, 1 }. (? = Pattern) Forward pre-query: matches the search string at the beginning of any string that matches pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (? = 95 | 98 | NT | 2000) 'can match "Windows" in "Windows 2000", but cannot match "Windows" in "Windows 3.1 ". Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters. (?! Pattern) Negative pre-query, match the search string at the start of any string that does not match Negative lookahead matches the search string at any point where a string not matching pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (?! 95 | 98 | NT | 2000) 'can match "Windows" in "Windows 3.1", but cannot match "Windows" in "Windows 2000 ". Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters (?: Pattern) matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use. This is useful when you use the "or" character (|) to combine each part of a pattern. For example, 'industr (?: Y | ies) is a simpler expression pcregrep 'Windows (? = 2000 | 95 | 98 | NT) 'test.txt // Windows is followed by 2000 or 95 .. pcregrep 'Windows (?! 2000 | 95 | 98 | NT) 'test.txt // pcregrep for Windows (?: 95 | 98 | NT | 2000) 'test.txt // same as the first type. egrep: an extended version of grep, which improves many operations that traditional grep cannot or are inconvenient. for example: Not Supported Under-grep? And + modifier, but egrep can. -grep does not support comparison between a, B, and (abc | xyz), but egrep does. fgrep: The expression is only used for string processing, and all meta functions are disabled. pcregrep: pcregrep-a grep with Perl-compatible regular expressions, perl regular expression, is the most popular and standard
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.