Linux Basic Learning Series--grep and regular expressions

Source: Internet
Author: User
Tags grep regular expression lowercase

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

The grep command's options are used to supplement the search process. The grep command has a flexible pattern, can be a string, a variable, or a regular expression.

Regardless of the form of the pattern, you need to use double quotes or single quotes to cause the pattern to be used, as long as the pattern contains spaces .

The ' search string ' is a regular expression, and note that in order to avoid the effect of the shell's metacharacters on the regular expression, enclose it in single quotes ("") and never enclose it in double quotes ("") or not.
A regular expression is just a notation, and as long as the tool supports this notation, the tool can handle the string of regular expressions. Vim, grep, awk, and SED support regular expressions, and it is because they support the regular that they appear strong;

1 Base Regular Expressions

grep tool, described previously.
grep-[ACINV] ' search content string ' filename
-A binary document is processed as text
- C calculates the number of matched rows found
-I ignores case
- n to output the line number in passing
- v reverse selection, which shows all rows that do not contain matching text

-H does not display file names when querying multiple files.
-L outputs only 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.
grep command plus-e parameter, which allows the extended pattern to match.

Where the search string can be a regular expression!
---
Let's use an example to illustrate the problem:
The following are most of the functions of the collation grep regular expression, detailed in the man
grep: To use a good grep this tool, in fact, is to write a regular expression, so this is not all the functions of grep to explain, only a few examples, to explain a regular expression of the wording.
$ ls-l | grep ' ^a ' filters the contents of the LS-L output through a pipe, showing only the rows that start with a.
$ grep ' test ' d* shows all the rows that contain test in a file that starts with D.
$ grep ' test ' AA bb cc shows the line matching test in the aa,bb,cc file.
$ grep ' [a-z]/{5/} ' AA shows all lines that contain at least 5 consecutive lowercase characters for each string.
$ Grep ' w/(es/) t.*/1 ' AA if West is matched, then es is stored in memory and labeled as 1 and then searches for any character (. *), followed by another ES (/1), and the line is displayed. If you use Egrep or GREP-E, you do not use the "/" number to escape, directly written ' W (es) T.*/1 ' on it.

grep regular Expression meta-character set (base set)
^ Anchor Line start like: ' ^grep ' matches all rows beginning with grep.
$ anchor row end like: ' grep$ ' matches all rows with grep ending.
. Match a newline character Furu: ' GR.P ' matches the GR followed by an arbitrary character followed by P.
* Match 0 or more previous characters Furu: ' *grep ' matches all one or more spaces followed by the grep line.
. * Used to represent any character together.
[] matches a specified range of characters, such as ' [Gg]rep ' matches grep and grep.
[^] matches a character that is not in the specified range, such as: ' [^a-fh-z]rep ' match does not contain a letter beginning with A-r and T-z, followed by the rep line.
/(.. /) tag matching characters, such as '/(love/) ', Love is marked as 1.
/< anchor the beginning of the word,
/> Anchors the end of a word, such as ' grep/> ' to match a line containing a word that ends with grep.
x/{m/} repeats characters x,m times, such as: ' o/{5/} ' matches rows containing 5 O. x/{m,/} repeats the character x, at least m times, such as: ' o/{5,/} ' to match at least 5 o rows.
x/{m,n/} repeats the character x, at least m times, not more than n times, such as: ' o/{5,10/} ' matches a row of 5--10 O.
/w matches literal and numeric characters, that is, [a-za-z0-9_], such as: ' G/w*p ' matches with G followed by 0 or more text or number characters, followed by P.
/W/W, which matches one or more non word characters, such as the dot number period.
/b Word lock, such as: '/bgrep/
B' matches grep only.

About matching instances:
Grep-c "Test.txt" Statistics all the lines that start with "48" characters
Grep-i "may" test.txt find all rows of "may" without case sensitivity
Grep-n "test.txt" shows line numbers, lines and line numbers that match the character "48", same as NL Test.txt |grep 48)
Grep-v "test.txt" Display output without character "48" all Lines)
grep "471" test.txt display the line where the output character "471" is located
grep test.txt display output begins with the character "48" and is the line where the TAB key is after the character "48"
grep "48[34]" test.txt display output begins with the character "48", and the third character is all rows of "3" or "4".
grep "^[^48]" Test.txt shows that the output header is not a line with the character "48"
grep [Mm]ay test.txt set Case lookup: Display output the first character that starts with "M" or "M" and 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 of" D "
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 "D" of all rows
grep [35]. 1998 "Test.txt shows that the first character is 3 or 5, the 23rd character is arbitrary, all lines ending with 1998
grep "4/{2,/}" test.txt mode appearance probability lookup: Display output character "4" at least two occurrences of all rows
grep "9/{3,/}" test.txt mode appearance probability lookup: Display output character "9" at least three occurrences of all rows
grep "9/{2,3/}" test.txt mode appearance probability lookup: Show output character "9" repeat occurrences in a range, repeat 2 or 3 times all rows
Grep-n "^$" test.txt displays the line number of the output blank line
Ls-l |grep "^d" If you want to query directories in the directory list in the same: ls-d *
Ls-l |grep "^d[d]" queries all files in a directory that do not contain a directory
Ls-l |grpe "^d.....x. X "Query the collection of directories with executable permissions for other users and members of the user group

More examples:
1
Search for the line and the trip number
$grep-n ' the ' Regular_express.txt
Search without the line, and the trip number
$grep-nv ' the ' regular_express.txt

2 Search for collection characters using []
[] denotes one of these characters, for example [Ade] represents a or D or E
woody@xiaoc:~/tmp$ grep-n ' t[ae]st ' regular_express.txt
8:i can ' t finish the test.
9:oh! The Soup taste good!

A prefix in [] can be used to represent a character other than a character in [].
For example, search for the line that contains the string with no G before Oo. Use ' [^g]oo ' as a search string
woody@xiaoc:~/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 scope, such as [A-z] for lowercase letters, [0-9] for 0~9 digits, [A-z] for uppercase letters. [A-za-z0-9] represents all numbers and English characters. Of course, you can also match ^ to exclude characters.
Search for rows that contain numbers
woody@xiaoc:~/tmp$ grep-n ' [0-9] ' regular_express.txt
5:however, this dress is about $3183 dollars.
15:you are the "Best is" menu you are the No.1.

The beginning of the line and the End-of-line character ^ $. ^, which indicates the end of the line (not the character, is the position) so ' ^$ ' means a blank line, because only
The beginning and end of a line.

here ^ with [] inside used ^ meaning is different. It means that the string behind the ^ is the beginning of the row.
Like searching the line at the beginning.
woody@xiaoc:~/tmp$ grep-n ' ^the ' regular_express.txt
12:the symbol ' * ' is represented as star.

Search for a line that starts with a lowercase letter
woody@xiaoc:~/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 the 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.
woody@xiaoc:~/tmp$

Search for lines with no English letters at the beginning
woody@xiaoc:~/tmp$ grep-n ' ^[^a-za-z] ' regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
#I am Vbird
woody@xiaoc:~/tmp$

$ indicates that the string before it is the end of the line, such as '/. '. At the end of a row
The line at the end of the search is.
woody@xiaoc:~/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 is about $3183 dollars.
6:gnu is free air beer.
.....

Note the text file generated under the MS System, with a ^m character. So the last character is going to be hidden ^m in the process of Windows
Pay special attention to the following text.
can use Cat Dos_file | Tr-d '/R ' > Unix_file to remove the ^m symbol. ^m==/r

then ' ^$ ' means that there is only a blank line at the end of the line.
Search for Blank Lines
woody@xiaoc:~/tmp$ grep-n ' ^$ ' regular_express.txt
22:
23:
woody@xiaoc:~/tmp$

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.