One. grep
grep (Globally search a Regular Expression and Print) is a powerful text search tool , it can use regular expressions to search for text, print matching rows, but does not affect the contents of the original file , and by the return value to notify the search results, if the command exit code of 0 means that the search succeeds , the return value is 1 means that the search is unsuccessful , and if the searched file does not exist, it returns 2, so that the return value can be used for the corresponding text processing.
For grep, there are two modes of operation: The basic mode , in which the regular expression for the command parameter is only normal for the characters ' () ', ' {} ', ' | ', '? ', ' + ' , for the five characters. If you need to use the special meaning of the escape character ' \ ' to escape it, in addition, if you do not want to escape, plus the- e option to change it to extended mode, and for another The extended pattern , which represents the extension of grep, is interpreted as the special meaning of the representation, and similarly, if you want to take its ordinary literal meaning, you can also use the escape character ' \ ' to escape ; using the Egrep command is equivalent to using grep plus the-e option;
Two. Regular expressions
Since grep is used for text search, if it is clear what to search for, you can immediately follow a definite string of characters, but if you are not very specific about what you are searching for, that is, you need to find something that satisfies some or some condition in the specified file. For example, I need to find out all the strings that meet the correct IP address, the format of course, like "XXX.XXX.XXX.XXX", then need to give grep a ' condition ', this condition tells Grep that I need what kind of content you have to find out for me, and this ' condition ', Use some special syntax to represent a character class, a quantifier and a positional qualifier, and then use these special syntax to represent a pattern together with ordinary characters, which is the regular expression .
A character class represents a character in a range of characters;
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/32/wKioL1ds4VOSfZ7nAAIRi5LBvyg003.png "title=" Character class. png "alt=" Wkiol1ds4vosfz7naairi5lbvyg003.png "/>
Examples are as follows:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/38/wKiom1ds40bh7I9AAABCYoIDZAQ352.png "title=" Character class Test.png "alt=" Wkiom1ds40bh7i9aaabcyoidzaq352.png "/>
The quantifier indicates the number of characters to be qualified;
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/83/36/wKiom1ds4e7Rkt2SAAK95VzObOU656.png "style=" float: none; "Title=" Qty Qualifier 1.png "alt=" Wkiom1ds4e7rkt2saak95vzobou656.png "/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/35/wKioL1ds4e7QqX-_AADovbheRKk722.png "style=" float: none; "Title=" Qty Qualifier 2.png "alt=" Wkiol1ds4e7qqx-_aadovbherkk722.png "/>
Examples are as follows:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/49/wKioL1dvcDTwMOEiAABmoKWIq_M945.png "title=" + + + + + + +. PNG "alt=" Wkiol1dvcdtwmoeiaabmokwiq_m945.png "/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/49/wKioL1dvcYmzWPzWAAApQvcSwwE635.png "title=" {}.png "alt=" Wkiol1dvcymzwpzwaaapqvcswwe635.png "/>
A positional qualifier represents a qualification for the occurrence of a character;
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/37/wKiom1ds4hTBKDuiAAIO7HL4vfM319.png "title=" Position qualifier. png "alt=" Wkiom1ds4htbkduiaaio7hl4vfm319.png "/>
Examples are as follows:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/3A/wKiom1ds6wzwJ2tuAABKNa3G17Q326.png "title=" Position qualifier Test.png "alt=" Wkiom1ds6wzwj2tuaabkna3g17q326.png "/>
Other special characters:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/3A/wKiom1ds7CLixY-GAAHhaXiLMoY358.png "title=" Other special characters. png "alt=" Wkiom1ds7clixy-gaahhaxilmoy358.png "/>
Examples are as follows:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/83/3A/wKioL1ds9wXDCBp7AAAn4pmCnUw279.png "title=" Other special characters test.png "alt=" Wkiol1ds9wxdcbp7aaan4pmcnuw279.png "/>
Three. grep Common command options
grep usage:grep [options] ... PATTERN [FILE] ...
Find pattern in each file or standard input, the default pattern is a basic regular expression (abbreviated as BRE);
The following chestnuts all use file files:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/47/wKioL1dvKRKRYSSbAABPb6G939k812.png "title=" File.png "alt=" Wkiol1dvkrkryssbaabpb6g939k812.png "/>
- G indicates that pattern is a basic regular expression, which is the default.
- e indicates that pattern is an extensible regular expression, that is, '? +| {} () ' These five types of characters have their special meanings;
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/48/wKiom1dvKm-xidc6AAAjxCZtHBo168.png "title="-e.png "alt=" Wkiom1dvkm-xidc6aaajxczthbo168.png "/>
- F indicates that the regular expression is not available, and pattern is a set of fixed-length strings separated by a break character;
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/83/47/wKioL1dvKsKzGhW6AAAd3LG4oec799.png "title="-f.png "alt=" Wkiol1dvkskzghw6aaad3lg4oec799.png "/>
- F indicates that the pattern is obtained from file, that is, specifying a matching file, so that grep finds the contents of the file conforming to the matching pattern;
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/47/wKioL1dvM6uBWcpxAAA9wKWqggE802.png "title="- Ff.png "alt=" Wkiol1dvm6ubwcpxaaa9wkwqgge802.png "/>
- I. ignoring case-matching patterns;
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/83/48/wKiom1dvNAGgCheLAAAatvXlemY598.png "title="-i.png "alt=" Wkiom1dvnaggchelaaaatvxlemy598.png "/>
- W indicates that pattern only matches words exactly, that is, only the pattern is considered a complete word to match;
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/47/wKioL1dvNWbDxpweAAAYyfzmbTc493.png "title="-w.png "alt=" Wkiol1dvnwbdxpweaaayyfzmbtc493.png "/>
- x forcing pattern only matches exactly one line, that is, the content of pattern satisfies a line;
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/48/wKiom1dvNhbj7JRcAAAacanXfa8466.png "title="-x.png "alt=" Wkiom1dvnhbj7jrcaaaacanxfa8466.png "/>
- S do not display error messages with no matches or files;
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/83/48/wKiom1dvORSg8J_eAAAdWnSyBAk167.png "title="-s.png "alt=" Wkiom1dvorsg8j_eaaadwnsybak167.png "/>
- v Show all content that does not meet the matching criteria;
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/83/47/wKioL1dvOgWxW-DWAAAr1yjvFDg077.png "title="-v.png "alt=" Wkiol1dvogwxw-dwaaar1yjvfdg077.png "/>
- v displays the version number;
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/48/wKiom1dvOkqR32tQAABCKsRjPPA269.png "title="- Vv.png "alt=" Wkiom1dvokqr32tqaabcksrjppa269.png "/>
- M represents the maximum number of matches from the input;
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/49/wKiom1dvQXywznglAAAkQ8mRndA861.png "title="-m.png "alt=" Wkiom1dvqxywznglaaakq8mrnda861.png "/>
- b prints the digit number of the first character of the output matching line;
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/48/wKioL1dvQwbxX5OAAAAocB0vS2Y606.png "title="-b.png "alt=" Wkiol1dvqwbxx5oaaaaocb0vs2y606.png "/>
- N displays the matching line and its line number;
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/83/49/wKiom1dvQ3myxZYEAAAT1v6BLss871.png "title="- Nn.png "alt=" Wkiom1dvq3myxzyeaaat1v6blss871.png "/>
- H indicates that the prefix displays the file name when searching for multiple files;
- H indicates that the file name prefix is not displayed when searching multiple files;
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/83/49/wKiom1dvREDjs3TzAAAleYvOId0588.png "title="- H-h.png "alt=" Wkiom1dvredjs3tzaaaleyvoid0588.png "/>
- o only the matching content is displayed;
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/48/wKioL1dvRKOgMfzBAAAcsT4kpkM349.png "title="-o.png "alt=" Wkiol1dvrkogmfzbaaacst4kpkm349.png "/>
- Q the match is successful or not, and no content is displayed, only the status code to tell the matching result ;
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/83/48/wKioL1dvRTaSptH1AAAlYTvowfc956.png "title="-q.png "alt=" Wkiol1dvrtaspth1aaalytvowfc956.png "/>
- D for the processing of devices such as pipes or sockets, such as read and Skip;
- D for the processing of files or folders, such as read, Recurse, skip;
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/83/48/wKioL1dvZy-A-5cxAAAzwbtPVk8744.png "title="-d.png "alt=" Wkiol1dvzy-a-5cxaaazwbtpvk8744.png "/>
-r-r recursive invocation;
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/48/wKioL1dvZ8jAwga4AAAmqJEh2DU128.png "title="- R-r.png "alt=" Wkiol1dvz8jawga4aaamqjeh2du128.png "/>
- L displays only the file names that match successfully;
- L displays only the filenames that did not match successfully;
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/49/wKiom1dvaGigDUDRAAAfRkuUuDg426.png "title="- L-l.png "alt=" Wkiom1dvagigdudraaafrkuuudg426.png "/>
- C statistics the number of matching rows;
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/83/48/wKioL1dvaO7Tbq4yAAAb4-ze9aQ732.png "title="-c.png "alt=" Wkiol1dvao7tbq4yaaab4-ze9aq732.png "/>
-A print out the matching lines and their previous specific lines;
- b print out the matching rows and their subsequent specific rows;
- C print out the matching rows and their respective specific lines;
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/49/wKioL1dvakPhpEvuAAAz7H09Dgo247.png "title="- Abc.png "alt=" Wkiol1dvakphpevuaaaz7h09dgo247.png "/>
Of course, if you want to see the options and use of some of the more specific commands of grep, you can view them through the grep--help command, and--color indicate that the matches are colored to make it easier to observe;
Finish
This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1792984
The grep tool under the shell