Home > php教程 > PHP开发 > body text

Detailed explanation of grep

高洛峰
Release: 2016-12-13 15:08:45
Original
1358 people have browsed it

OK, after learning regular expressions, sed, awk, and the three text musketeers, grep is all that’s left. Let’s take it together

Let’s take a look at what grep is first

Baidu it

English: global search regular expression(RE) and print out the line

Chinese: comprehensive search regular expression(RE) and print out the line

grep is a powerful text search tool that uses regular expressions to search text and prints matching lines.

The grep family includes grep, egrep and fgrep

Principle

Tell me about the principle of grep?

After giving a file list or standard input, grep will search for text that matches one or more expressions, and only output lines or text that match

(or do not match)

In fact, to put it bluntly, The process of grep is not much different from the addressing process of sed.

Refer to the sed processing process, we summarize the grep processing process

Read a line of text into the pattern space

Search the line of text based on the specified regularity to see if there is matching content

If so, the entire line matches at once None, the line will not be processed further, delete the line and read the next line. If the match is successful, print the line and read the next line.

It seems that grep is actually a simplified version of sed. Successfully matched lines are automatically printed. However, sed needs to specify the p command to print, otherwise the entire text will be printed. Of course, grep's option provides a lot of parameters and also has many functions that sed does not have.


OK, now that this is the case, there are only two pieces of content left in grep, one is the parameters of grep, and the other is regular addressing

Speaking of regularity, the use of regularity and sed in grep, awk is a little different, sed or awk Use // to encircle the middle. In grep, there is no need for


grammar format

grep [option] pattern [file...]

grep [options] [-e pattern] -f []


grep -E is equivalent to egrep , grep -F is equivalent to fgrep, grep -r is equivalent to rgrep

Let’s take a look at grep’s [options]

In the process of introducing [options], we will give some small examples to practice metacharacters and grep, egrep , the use of fgrep.

The author uses colors for parameters that he thinks may be frequently used, and the marks are the same


grep -V This can be seen in the grep version


Test text

John Daggett, 341 King Road, Plymouth MA

Alice Ford, 22 East Broadway, Richmond VA

Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury MA
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA
ansible is ansible
saltstcak is more powerful than (ansible),hehe
ansibl are ansible


Selection of matchers

Detailed explanation of grepMatching control

Detailed explanation of grepOutput control

Detailed explanation of grepOutput line prefix control

Detailed explanation of grepContent control

Detailed explanation of grepFile and directory options

Detailed explanation of grepOther options

Detailed explanation of grepExit Status

The matching is successful, the exit status code is 0, the matching fails, the exit status code is 1, the matching error occurs, the exit code is 2

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template