1. grep Introduction
Grep (Global search Regular expression (RE) and print out of the line, a comprehensive search for regular expressions and print out rows) is a powerful text search tool that uses regular expressions to search for text. and print out the matching rows. The grep family of Unix includes grep, Egrep, and Fgrep. Egrep and Fgrep commands are only a small difference from grep. Egrep is an extension of Grep that supports more re metacharacters, Fgrep is fixed grep or fast grep, which regards all letters as words, that is, the metacharacters in the regular expression returns to its own literal meaning and is no longer special. Linux uses the GNU version of grep. It is more powerful and can use the EGREP and FGREP functions through the-G,-E,-F command-line Options.
grep works like this by searching 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 file names. The results of the search are sent to the screen without affecting the contents of the original file.
grep is available for shell scripts because grep indicates the state of the search by returning a status value, returns 0 if the template search succeeds, or 1 if the search is unsuccessful, and returns 2 if the searched file does not exist. We can use these return values to do some automated text processing work.
2. grep regular expression meta-character set (base set)
The beginning of the anchoring line, such as: ' ^grep ' matches all rows beginning with Grep.
$
The end of the anchoring line, such as: ' grep$ ', matches all rows that end with Grep.
Match a newline character Furu: ' GR.P ' matches the GR followed by an arbitrary character followed by P.
*
Match 0 or more previous characters Furu: ' *grep ' matches all one or more spaces followed by the Grep row ... * together to represent any character.
[]
Matches a specified range of characters, such as ' [Gg]rep ' matches grep and grep.
[^]
Matches a character that is not in the specified range, such as: ' [^a-fh-z]rep ' match does not contain a letter beginning with A-r and T-z, followed by the rep line.
\(.. \)
Tag matching characters, such as ' \ (love\ '), Love is marked as 1.
\<
Anchor the beginning of a word, such as: ' \\>
Anchors the end of a word, such as ' grep\> ' to match a line containing a word that ends with grep.
X\{m\}
Repeat characters x,m times, such as: ' 0\{5\} ' matches rows containing 5 O.
X\{m,\}
Repeat character X, at least m times, such as: ' O\{5,\} ' matches rows with at least 5 O.
X\{m,n\}
Repeat character X, at least m times, no more than n times, such as: ' O\{5,10\} ' matches 5--10 O's line.
\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 text or number characters, followed by P.
\w
\w, which matches one or more non word characters, such as the dot number period.
\b
Word locks, such as: ' \bgrepb\ ' only match grep.