First, Introduction
grep (abbreviated from globally search a Regular expression and print) is a powerful text-search tool that can use regular expressions to search for text and print matching lines. The grep family of Unix includes grep, Egrep, and Fgrep.
grep works in such a way that it searches for a string template in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as filenames. The results of the search are sent to the screen without affecting the contents of the original file. grep can be used for Shell scripting, because grep describes the status of the search by returning a status value, or 0 if the template search succeeds, or 1 if the search is unsuccessful, or 2 if the searched file does not exist. We can use these return values to do some automated text processing work. The ability to specify a string statement in the grep command is a regular expression, a method that allows the use of a specified string of certain special keyboard characters, which can be used to represent other characters or to further define how pattern matching works. For example: grep ". *hood" Essay1. The command searches the file essay1 for each line that contains the word with the string hood. The point in the command line indicates that Hood can have any character before it, and the asterisk refers to any character that is represented by a dot before the string (where double quotes are optional, but must be enclosed when the statement contains a phrase or a space) [1]Second, usage(1)expression Set1, ^ The start of the anchor line, such as ' ^google ' match so the line starts with Google2, $ anchor line end, such as ' goolge$ ' match so end with Google line3,. Match a non-newline (' \ n ') character, such as ' GR.P ' match gr followed by an arbitrary character4. * Match 0 or more previous characters5, [] match a specified range of characters, such as ' [Gg]rep ' match grep and grep6.[^] matches a character that is not within the specified range, such as: ' [^a-fh-z]rep ' matches a letter that does not contain a-f and h-z, immediately following the line of the Rep. 7. \< anchors the beginning of the word, such as: ' \<grep ' matches the line containing the word that begins with grep. 8. \> anchors the end of the word, such as ' grep\> ' matches the line containing the word ending with grep. 9, x\{m\} Repeat characters x,m times, such as: ' O\{5\} ' matches rows containing 5 O. 10, x\{m,\} repeats the character x, at least m times, such as: ' O\{5,\} ' matches a line with at least 5 O. 11, x\{m,n\} repeats the character x, at least m times, not more than n times, such as: ' O\{5,10\} ' matches rows of 5--10 O. 12. \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 literal or numeric characters, then p. 13, \w \w the reverse form, matching one or more non-word characters, such as the dot period and so on. 14, \b Word lock, such as: ' \bgrep\b ' only match grep. [2] 15, \+ matches one or more previous characters. such as: ' [a-z]\+able ', matching one or more lowercase letters followed by able strings, such as loveable,enable,disable, etc. 16, \? Match 0 or one of the previous characters. such as: ' Gr\?p ' matches the GR followed by one or no characters, then the line of P. 17, A\|b\|c match A or B or C. such as: grep|sed matching grep or sed18, \ (\) Grouping symbols, such as: Love\ (ab\le\|rs\) ov\+ match loveable or lovers, matching one or more ov.
(2) Command options
1 、-? show matching lines up and down? Line, such as: grep-2 pattern filename Displays the top and bottom 2 rows of a matching row. 2,-A,--text equivalent to match text for (Binary file (standard input) matches) Error 3,-b,--byte-off Set Print the block number where the line is printed before the line. 4,-C,--count to print only the number of matched lines, no matching content is displayed. 5,-F file,--file=file extract template from file. An empty file contains 0 templates, so nothing matches. 6,-h,--no-filename the matching filename prefix is not displayed when searching multiple files. 7,-i,--ignore-case Ignore case difference. 8,-O,--only-matching displays only the parts of the regular expression match. (Show only in a line matching PATTERN) 9,-q,--quiet cancel display, return only exit status. 0 indicates that a matching row was found. 10.-l,--files-with-matches Print the file list of the matching template. 11.-l,--files-without-match Print a list of files that do not match the template. 12.-n,--line-number print line numbers in front of matching lines. 13,-s,--silent does not display error messages about nonexistent or unreadable files. 14,-v,--revert-match anti-retrieval, show only unmatched rows. 15,-w,--word-regexp if be \< and \> refer to the expression as a single word search. 16,-R,-R,--recursive recursively read all files in the directory, including subdirectories. Like what
grep-r ' pattern ' testMatches the pattern in all files in the test and its subdirectories. 17,-v,--version display software version information. 18,-a6 find some characters of the content, and the next extension of 6 lines 19,-b6 find some characters of the content, and extend 2 lines 20,-c1 find some characters of the content, and up and down each extension 1 lines after the number of lines directly affect the number of extensions, and with--character Number of results of the split search row three, instance 1, $ ls-l | grep ' ^a ' filters the contents of the Ls-l output through a pipeline, displaying only the lines that begin with a. 2, $ grep ' test ' d* shows all the lines in the file that begin with D that contain test. 3, $ grep ' test ' AA bb cc shows the line matching test in the aa,bb,cc file. 4, $ grep ' [a-z]\{5\} ' AA displays all lines containing strings with 5 consecutive lowercase characters for each string. 5, $ grep ' w\ (es\) t.*\1 ' AA if West is matched, then es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (\1), which is found to display the row. If you use Egrep or GREP-E, you do not have to escape the "\" number, directly written as ' W (es) t.*\1 ' on it. 6. Ps-ef|grep Clustal2 Find the specified process "Clustal2" Ps-ef|grep clustal2|wc-l find the number of runs of the specified process "Clustal2"
Ps-ef| grep clustal2longpen+ 27347 26704 0 11:13 pts/5 00:00:00 grep--color=auto clustal2
Ps-ef|grep clustal2|wc-l
1
7. Cat Test.txt | Grep-f Test2.txt
[[email protected] test] # hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatredhatlinuxmint[root@localhost Test]# linuxredhat[root@localhost Test]# cat test.txt | grep-f test2.txt Hnlinuxubuntu linuxredhatlinuxmint[root@localhost Test]#
8. Cat Test.txt |grep-e "ed|at" displays content lines containing ed or at characters
9. grep ' [a-z]\{7\} ' *.txt displays all lines, 1, and in regular expressions that contain at least 7 consecutive lowercase characters of each string in a file that ends in the current directory, and note the use of escape characters. If grep "$a" file #引用变量a, look for the value of variable a
grep ' $a ' file #查找 ' $a ' string
grep ' \ \ ' file #查找 ' \ ' character grep "\\\\" File # because it is double quotes, the shell first to escape \ \ \,grep received only \\\,\ is a special character, followed by the need to escape the character, that is, \ \, so if directly use "// "Then there was a mistake.
[1] from Baidu Encyclopedia
A day of the shell command: grep