grep usage explained: grep and regular expressions [go]

Source: Internet
Author: User
Tags grep regular expression

Regular expressions are not the same as wildcard characters, and they represent different meanings.

The options for the grep command are used to supplement the search process. The pattern of the grep command is flexible, which can be a string, a variable, or a regular expression.

no matter what the pattern is, you need to use double or single quotation marks to enclose the pattern whenever the pattern contains spaces.

The ' search string ' is a regular expression, note that in order to avoid the effect of the shell's metacharacters on regular expressions, enclose it in single quotation marks (""), and never enclose it in double quotation marks ("").
A regular expression is just a notation, and the tool can handle a string of regular expressions as long as the tool supports that notation. Vim, grep, awk, and sed all support regular expressions, and it is because they support the regular that they are powerful;

1 Basic Regular Expressions

grep tool, previously introduced.
grep-[ACINV] ' search content string ' filename
-A binary document is processed in text mode
- C calculates the number of rows found to match
-I ignores case
- n output line number
- v Invert selection, which displays all lines that do not contain matching text

-H does not display a file name when querying multiple files.
-L Only output file names that contain matching characters when querying multiple files.
-S does not display error messages that do not exist or have no matching text.
The grep command plus-e parameter, which allows extended pattern matching to be used.

Where the search string can be a regular expression!

---
First illustrate the problem with an example:
The following are the most features of the collation of grep regular expressions, see Man in detail
grep: To use grep this tool, in fact, is to write good regular expression, so here does not have all the functions of grep to explain, only a few examples, explain a regular expression of the wording.
$ ls-l | grep ' ^a ' filters the contents of the Ls-l output through a pipeline, displaying only the lines that begin with a.
The $ grep ' test ' d* shows all the lines in the file that begin with D that contain test.
The $ grep ' test ' AA bb cc Displays the line in the aa,bb,cc file that matches the test.
The $ grep ' [a-z]/{5/} ' AA displays all lines that contain a string of at least 5 consecutive lowercase characters per string.
$ Grep ' w/(es/) t.*/1 ' AA if West is matched, then es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (/1), which is found to display the line. If you use Egrep or GREP-E, do not use the "/" number to escape, directly written as ' W (es) T.*/1 ' on it.

grep regular Expression meta-character set (base 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 one or more spaces followed by the line of grep.
. * Use together to represent any character.
[] 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.
/(.. /) Mark matching characters, such as '/(love/) ', Love is marked as 1.
/< anchoring the beginning of the word,
/> 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: ' o/{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.
/w/w the inverse of the form, matching one or more non-word characters, such as the dot period and so on.
/b Word lock, such as: '/bgrep/
b ' match grep only.

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" Display line number; display line and line number of match character "48", same as NL test.txt |grep)
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

For more examples:
1
search for the line that has the, and lose the travel number
$grep-n ' the ' Regular_express.txt
search without the line, and lose the travel number
$grep-nv ' the ' Regular_express.txt

2 using [] to search for collection characters
[] denotes one of these characters, for example [ADE] denotes a or D or e
[email protected]:~/tmp$ grep-n ' t[ae]st ' regular_express.txt
8:i can ' t finish the test.
9:oh! The soup taste good!

you can use the ^ symbol to do a prefix within [], representing characters other than the characters in the [].
For example, search for a line with a string that has no G before OO. Use ' [^g]oo ' as the search string
[email protected]:~/tmp$ grep-n ' [^g]oo ' Regular_express.txt
2:apple is my favorite food.
3:football game isn't use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!

[] can be expressed in the range, such as [A-z] for lowercase letters, [0-9] for 0~9, [A-z] is capital letters. [a-za-z0-9] denotes all numbers and English characters. Of course, you can also match ^ to exclude characters.
search for rows that contain numbers
[email protected]:~/tmp$ grep-n ' [0-9] ' Regular_express.txt
5:however, this dress was about $3183 dollars.
15:you is, the best is menu, the the best.

The beginning of the line with the trailing character ^ $. ^ Represents the beginning of the line, $ means the end of the line (not the character, is the position) then ' ^$ ' represents a blank line, because only
The beginning and end of the line.

here ^ differs from the ^ meaning used inside []. It indicates that the following string is the beginning of the row.
For example, search for the line at the beginning
[email protected]:~/tmp$ grep-n ' ^the ' regular_express.txt
12:the symbol ' * ' is represented as star.

search for lines that start with lowercase letters
[email protected]:~/tmp$ grep-n ' ^[a-z] ' regular_express.txt
2:apple is my favorite food.
4:this dress doesn ' t fit me.
10:motorcycle is cheap than car.
12:the symbol ' * ' is represented as star.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let ' s go.
[Email protected]:~/tmp$

search for lines that begin with a letter that is not English
[email protected]:~/tmp$ grep-n ' ^[^a-za-z] ' regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
: #I am Vbird
[Email protected]:~/tmp$

$ indicates that the string in front of it is at the end of the line, such as '/. '. At the end of a row
the line at the end of the search is.
[email protected]:~/tmp$ grep-n '/.$ ' regular_express.txt//. is a special symbol for regular expressions, so use/escape
1: "Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:football game isn't use feet only.
4:this dress doesn ' t fit me.
5:however, this dress was about $3183 dollars.
6:GNU is free air isn't free beer.
.....

Note The text file generated under the MS System, with a ^m character added. So the last character will be the hidden ^m, in the processing Windows
pay special attention to the following text!
you can use Cat Dos_file | tr-d '/R ' > Unix_file to remove the ^m symbol. ^M==/R

then ' ^$ ' means only the empty line at the end of the line!
Search for empty lines
[email protected]:~/tmp$ grep-n ' ^$ ' regular_express.txt
:
At :
[Email protected]:~/tmp$

Search for non-empty rows
[email protected]:~/tmp$ grep-vn ' ^$ ' regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:football game isn't use feet only.
4:this dress doesn ' t fit me.

grep usage explained: grep and regular expressions [go]

Related Article

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.