Linux grep Regular Expression

Source: Internet
Author: User

grep regular Expression meta-character set:
^ The start of the anchor line, such as: ' ^grep ' matches all lines that begin with grep.
The end of the anchor line is as follows: ' grep$ ' matches all rows ending with grep.
. Match a non-newline character such as: ' GR.P ' matches gr followed by an arbitrary character followed by P.
* Match 0 or more previous characters such as: ' *grep ' matches all of one or more spaces followed by the grep line. * Together with any character represented.
[] matches a specified range of characters, such as ' [Gg]rep ' matches grep and grep.
[^] matches a character that is not within the specified range, such as: ' [^a-fh-z]rep ' matches a letter that does not contain a-r and t-z, immediately following the line of the Rep.
\(.. \) tag matching characters, such as ' \ (love\) ', Love is marked as 1.
\ Anchors the beginning of a word, such as: ' \ matches a line containing a word that begins with grep.
\> anchors the end of the word, such as ' grep\> ' matches the line that contains the word that ends with grep.
X\{m\} repeats characters x,m times, such as: ' 0\{5\} ' matches rows containing 5 O.
X\{m,\} repeats a character x, at least m times, such as: ' O\{5,\} ' matches at least 5 rows of O.
X\{m,n\} repeats the character x, at least m times, not more than n times, such as: ' O\{5,10\} ' matches rows of 5--10 O.
\w matches literal and numeric characters, that is, [a-za-z0-9], such as: ' G\w*p ' matches with a G followed by 0 or more literal or numeric characters, followed by P.
\b Word lock, such as: ' \bgrep\b ' only matches grep. The common grep options are:
-C outputs only the number of matching rows.
-I is case-insensitive (only for single-character).
-H does not display a file name when querying multiple files.
-L Only output file names that contain matching characters when querying multiple files.
-N Displays matching lines and line numbers.
-S does not display error messages that do not exist or have no matching text.
-V Displays all lines that do not contain matching text.
-V Displays software version information
It is best to use double quotation marks when using grep matching to prevent the system from mistaken for parameters or special commands, or to match multiple words.

About matching instances:
Grep-c "Test.txt" counts all lines that start with a "48" character
Grep-i "may" test.txt case-insensitive Find all rows
Grep-n "test.txt" displays line numbers; Displays the line and line number of the matching character "48", same as NL Test.txt |grep 48)
Grep-v "test.txt" display output no characters "48" all rows)
grep "471" test.txt displays the line where the output character "471" is located)
grep "Test.txt" the display output starts with the character "48" and is the line where the TAB key is after the character "48"
grep "48[34]" test.txt display output starts with the character "48", the third character is "3" or "4" for all rows)
grep "^[^48]" test.txt display the line at the beginning of the output is not the character "48"
grep "[Mm]ay" Test.txt set case lookup: Display output The first character starts with "M" or "M", and the line ends with the character "Ay"
grep "K ... D "test.txt display output the first character is" K ", combined second is any character, the fifth character is the line where" D "is located
grep "[a-z][9]d" test.txt display output the first character range is "a-d", the second character is "9", and the third character is all rows of "D"
grep "[35].. 1998 "Test.txt shows that the first character is 3 or 5, and the 23rd character is any line that ends with 1998.
grep "4\{2,\}" test.txt mode chance to look for: Display output character "4" at least repeat two times all rows
grep "9\{3,\}" test.txt mode chance to look for: Display output character "9" at least repeat three times all rows
grep "9\{2,3\}" test.txt mode chance to find: Display output character "9" repeated occurrences of the number of times in a certain range, repeated 2 or 3 times all rows
Grep-n "^$" Test.txt shows the line number of the output blank line
Ls-l |grep "^d" If you want to query directories in the directory list with: Ls-d *
Ls-l |grep "^d[d]" querying a directory for all files that do not contain a directory
Ls-l |grpe "^d.....x. X "Querying other user and user group members for directory collections that have executable permissions

Linux grep Regular Expression

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.