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 VAOrville 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
Matching control
Output control
Output line prefix control
Content control
File and directory options
Other options
Exit 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