Linux grep Regular Expression

Source: Internet
Author: User
Tags grep regular expression
Grep Regular Expression metacharacters:
^ Start of the anchor row: '^ grep' matches all rows starting with grep.
$ The End Of The Anchor row is as follows: 'grep $ 'matches all rows ending with grep.
. Match a non-linefeed character such as: 'gr. P' match gr followed by any character, followed by P.
* Match zero or multiple previous characters, for example, '* grep'. Match All one or more spaces followed by the grep line. . * Represents any character.
[] Matches a character in a specified range, for example, '[Gg] rep' matches grep and grep.
[^] Match a character that is not within the specified range, for example, '[^ A-FH-Z] rep' match a line that does not start with a letter that does not contain the A-R and T-Z, followed by Rep.
\ (.. \) Mark matching characters, such as '\ (love \)', and love is marked as 1.
\ Specifies the start of a word, for example, '\ matches a row that contains a word starting with grep.
\> Anchor specifies the end of a word. For example, 'grep \> 'matches the row containing the word ending with grep.
The characters x \ {M \} are repeated for X and m times. For example, '0 \ {5 \} 'matches the rows containing 5 o.
The characters x \ {M, \} are repeated at least m times, for example, 'O \ {5, \} 'matches rows with at least 5 o.
The characters x \ {M, N \} are repeated at least m times, and must not be more than N times. For example, the line 'o \ {5, 10 \} 'matches 5--10 O.
\ W matches characters and numbers, that is, [A-Za-z0-9], for example, 'g \ W * P' matches 0 or more characters or numbers after G, then p.
\ B word lock, for example, '\ bgrep \ B' only matches grep. Common grep options include:
-C only outputs the number of matched rows.
-I is case insensitive (only applicable to single characters ).
-H: When querying multiple files, the file name is not displayed.
-L only names containing matching characters are output when multiple files are queried.
-N: the matching row and row number are displayed.
-S does not display the error message that does not exist or does not match the text.
-V: displays all rows that do not contain matched text.
-V: displays the software version.
When using grep for matching, it is best to use double quotation marks to prevent the system from mistaken for parameters or special commands and matching multiple words.

Matching instances:
Grep-c "48" test.txt counts the number of rows starting with "48"
Grep-I "may" test.txt searches all rows of "may" in case insensitive)
Grep-n "48" test.txt shows the row number, and shows the row and row number matching the character "48", same as NL test.txt | grep 48)
Grep-V "48" test.txt shows that all rows with no character "48" are output)
Grep "471" test.txt shows the row where the output character "471" is located)
Grep "48;" test.txt shows that the output starts with the character "48" and is the row of a tab key after the character "48"
Grep "48 [34]" test.txt shows that the output starts with the character "48", and the third character is "3" or "4)
Grep "^ [^ 48]" test.txt shows that the first line of the output line is not the line with the character "48)
Grep "[mm] Ay" test.txt set case-insensitive search: displays the rows whose first character is output starting with "M" or "M" and ending with "ay)
Grep "K... D "test.txt indicates that the first character of the output is" K ", the second, third, and fourth are arbitrary characters, and the fifth character is the row where" D "is located)
Grep "[A-Z] [9] D" test.txt shows that the output range of the first character is "A-D" and the second character is "9 ", the third character is all the rows of "D ".
Grep "[35] .. 1998" test.txt shows the first character is 3 or 5, the second three characters are arbitrary, all rows ending with 1998
Grep "4 \ {2, \}" test.txt mode appears probability search: show the output character "4" at least two repeated rows
Grep "9 \ {3, \}" test.txt mode: show all rows with at least three times of output character "9"
Grep "9 \ {2, 3 \}" test.txt mode appears probability search: show the number of times that the output character "9" appears repeatedly within a certain range, repeat two or three times all rows
Grep-n "^ $" test.txt shows the row number of the empty output line
Ls-L | grep "^ d" if you want to query the directories in the directory list *
Ls-L | grep "^ d [d]" query all files in a directory that do not contain a directory
Ls-L | GRPE "^ d ..... X. x "queries the directory set that other users and user group members have executable permissions

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.