In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "grep and regular expressions how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "grep and regular expressions how to use" this article.
A regular expression is just a representation, and as long as the tool supports this representation, the tool can handle strings of regular expressions. Vim, grep, awk, and sed all support regular expressions, and it is precisely because they support regularities that they are powerful. In previous companies, because they were web-based service-oriented websites (nginx), they had a greater demand for regularities, so they also spent some time studying regularities, so I would like to share with you:
1 basic regular expression
Grep tool, which has been introduced before.
Grep-[acinv] 'search content string' filename
-a search as a text file
-c calculates the number of matching lines found
-I ignore case
-n output line number by the way
-v reverse selection, that is, find lines that do not have a search string
Where the search string can be a regular expression!
one
Search for lines with the and output the line number
$grep-n 'the' regular_express.txt
Search for lines without the and output the line number
$grep-nv 'the' regular_express.txt
2 use [] to search for collection characters
[] indicates one of the characters, such as [ade] for an 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!
You can use the ^ symbol as a prefix in [] to indicate characters other than the characters in [].
For example, the line where there is no string of g before searching 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 is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!
Ranges can be used in [], for example, [amurz] for lowercase letters, [0-9] for numbers of 0,9, and [Amurz] for uppercase letters. [a-zA-Z0-9] represents all numbers and English characters. Of course, you can also work with ^ 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 characters ^ $at the beginning and end of the line. ^ represents the beginning of the line, $represents the end of the line (not a character, but position), then'^ $'represents a blank line, because only
The beginning and end of a line.
The meaning of ^ here is different from that used in []. It indicates that the string after ^ is at the beginning of the line.
For example, search for lines at the beginning of the
Woody@xiaoc:~/tmp$ grep-n'^ the' regular_express.txt
12:the symbol'*'is represented as star.
Search for lines that begin with lowercase letters
Woody@xiaoc:~/tmp$ grep-n'^ [amurz] '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.
Woody@xiaoc:~/tmp$
Search for lines that do not begin with English letters
Woody@xiaoc:~/tmp$ grep-n'^ [^ a-zA-Z] 'regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
21:#I am VBird
Woody@xiaoc:~/tmp$
$indicates that the string before it is at the end of the line, such as'\.' It means. At the end of a line
The search ends with. The line of
Woody@xiaoc:~/tmp$ grep-n'\. $'regular_express.txt /. Is a special symbol for regular expressions, so escape with\
1: "Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
5:However, this dress is about $3183 dollars.
6:GNU is free air not free beer.
.
Note that the text file generated under MS's system will be wrapped with a ^ M character. So the last character will be the hidden ^ M, dealing with Windows
Pay special attention to the following text!
You can use cat dos_file | tr-d'\ r'> unix_file to delete the ^ M symbol. ^ masked =\ r
Then'^ $'means only the blank line at the beginning and the end of the line!
Search for blank lines
Woody@xiaoc:~/tmp$ grep-n'^ $'regular_express.txt
22:
23:
Woody@xiaoc:~/tmp$
Search for non-blank lines
Woody@xiaoc:~/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 is not use feet only.
4:this dress doesn't fit me.
.
Any character. And repeating characters *
In bash, * represents a wildcard, which is used to represent any character, but in a regular expression, it has a different meaning. * means there are 0 or more of a character.
For example, oo*, means that the first o must exist, and the second o can have one or more or none, so it represents at least one o.
Point. Represents an arbitrary character and must exist. It can be denoted by'g.. d'. Good, gxxd, gabd. It all fits.
Woody@xiaoc:~/tmp$ grep-n'g.. d 'regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good!
16:The world is the same with 'glad'.
Woody@xiaoc:~/tmp$
Search for more than two o strings
Woody@xiaoc:~/tmp$ grep-n 'ooo*' regular_express.txt / / the first two o must exist, the third o may not exist, or there can be more than one.
1: "Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good!
18:google is the best tools for search keyword.
19:goooooogle yes!
Search for the beginning and end of g, with at least one o string in the middle, that is, gog, goog....gooog... Etc.
Woody@xiaoc:~/tmp$ grep-n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
Search for the line where the string at the beginning and end of g is located
Woody@xiaoc:~/tmp$ grep-n 'g.pragg' regular_express.txt / /. * represents 0 or more arbitrary characters
1: "Open Source" is a good mechanism to develop programs.
14:The gd software is a library for drafting programs.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! Go! Let's go.
Limit the range of consecutive repeating characters {}
. * you can only limit 0 or more characters. If you want to limit the number of character repeats exactly, use {range}. The range is for numbers, separated by 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5.
2 means 2, 2 means 2 or more
Note that because {} has a special meaning in SHELL, it should be escaped with\ when used as a regular expression.
Search for a line containing a string of two os.
Woody@xiaoc:~/tmp$ grep-n'o\ {2\} 'regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good!
18:google is the best tools for search keyword.
19:goooooogle yes!
Search for g followed by 2'5 o, followed by a string of g.
Woody@xiaoc:~/tmp$ grep-n'go\ {2jue 5\} g 'regular_express.txt
18:google is the best tools for search keyword.
Search for lines that contain g followed by more than 2 o, followed by g.
Woody@xiaoc:~/tmp$ grep-n'go\ {2,\} g 'regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
Note that the ^-in concession [] does not have a special meaning and can be placed after the content in [].
'[^ a murz\.! ^ -]' means there are no lowercase letters, no. No! no spaces, no-strings, notice that there is a small space in [].
In addition, the reverse selection in shell is [! range], and in the rule is [^ range]
2 extended regular expression
Extended regular expressions add several special constructs to the underlying regular expressions.
It makes some operations more convenient.
For example, if we want to remove a blank line and a line that begins with #, we would use:
Woody@xiaoc:~/tmp$ grep-v'^ $'regular_express.txt | grep-v' ^ #'
"Open Source" is a good mechanism to develop programs.
Apple is my favorite food.
Football game is not use feet only.
This dress doesn't fit me.
.
However, it is much more convenient to use egrep that supports extended regular expressions and extended special symbols |.
Note that grep only supports basic expressions, while egrep supports extensions, but egrep is actually an alias for grep-E. Therefore, grep-E supports extended regularization.
So:
Woody@xiaoc:~/tmp$ egrep-v'^ $| ^ # 'regular_express.txt
"Open Source" is a good mechanism to develop programs.
Apple is my favorite food.
Football game is not use feet only.
This dress doesn't fit me.
.
Here | indicates the relationship between or. A string that satisfies ^ $or ^ #.
Here are a few extended special symbols:
+, Yu. * works similarly, indicating one or more repeating characters.
Yu. * has a similar effect, indicating 0 or one character.
| |, indicates or relationship, for example,'gd | good | dog' indicates a string with gd,good or dog |
() to synthesize parts of the content into a unit group. For example, to search for glad or good, you can go like'g (la | oo) d'.
The advantage of () is that you can use +? * for the team.
For example, to search for the beginning and end of An and C, with at least one (xyz) string in the middle, you can go like this: a (xyz) + C'
The above is all the content of the article "how to use grep and regular expressions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.