grep(The global Search Regular expression (RE) and print out of the line, which searches for regular expressions and prints the rows) is a powerful text search tool that uses regular expressions to search for text and print out matching rows. of Unix
grepFamily includes
grep, Egrep and Fgrep. Egrep and Fgrep's orders are only with
grepThere is a very small difference. Egrep is
grepExtensions that support more re meta characters, Fgrep is the fixed
grepor fast
grep, they treat all letters as words, that is, the metacharacters in regular expressions return to their literal meaning and are no longer special. Linux uses the GNU version of the
grep。 It is more powerful and can use the EGREP and FGREP functions through the-G,-E,-F command-line Options.
grepThe way it works is to search 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.
grepcan be used with shell scripts because
grepIndicates 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.
grepRegular expression meta-character set (base set)
^
Anchor Line start as: ' ^grep ' matches all to
grepThe beginning of the line.
$
Anchor line end like: ' grep$ ' matches all to
grepThe line at the end.
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
grepLine.. * Use to represent any character together.
[]
Matches a specified range of characters, such as ' [Gg]rep ' match
GrepAnd
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.
/(.. /)
Tags match characters, such as '/(love/) ', and Love is marked as 1.
/<
Anchor the beginning of a word, such as: '//>
Anchoring the end of a word, such as ' grep/> ' match, contains a
grepThe line of the ending word.
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/' matches only
grep。
3. For Egrep and
grep-e meta character extension set
+
Matches one or more of the previous characters. such as: ' [a-z]+able ', match one or more lowercase letters followed by the able string, such as loveable,enable,disable.
?
Matches 0 or more previous characters. For example: ' Gr?p ' matches a gr followed by one or no characters, then a line of P.
A|b|c
Match A or B or C. such as: grep|sed matching
grepor SED
()
Group symbols, such as: Love (able|rs) ov+ match loveable or lovers, matching one or more ov.
X{m},x{m,},x{m,n}
function with x/{m/},x/{m,/},x/{m,n/}
4. POSIX character class
POSIX (The Portable operating System Interface) adds special character classes, such as [: Alnum:], to keep one to the character encodings in different countries. To put them inside the [] number, you can become regular expressions, such as [a-za-z0-9] or [[: Alnum:]]. Under Linux.
grepThe POSIX character classes are supported in addition to FGREP.
[: Alnum:]
Literal numeric character
[: Alpha:]
Literal characters
[:d Igit:]
numeric characters
[: Graph:]
Non-null characters (not spaces, control characters)
[: Lower:]
lowercase characters
[: Cntrl:]
Control characters
[:p rint:]
Non-null characters (including spaces)
[:p UNCT:]
Punctuation
[: Space:]
All white-space characters (new lines, spaces, tabs)
[: Upper:]
Uppercase characters
[: Xdigit:]
hexadecimal digits (0-9,a-f,a-f)
5.
GrepCommand options
-?
Displays both the top and bottom of the matching row. Rows, such as:
grep-2 pattern filename Displays the top and bottom 2 rows of the matching row at the same time.
-b,--byte-offset
Print the block number in which the line is printed before the matching line.
-C,--Count
Prints only the number of rows that match and does not display the matching content.
-F File,--file=file
Extracts the template from the file. The empty file contains 0 templates, so nothing matches.
-h,--no-filename
When searching for multiple files, the matching filename prefix is not displayed.
-i,--ignore-case
ignores case differences.
-q,--quiet
Suppresses display and returns only the exit status. 0 indicates that a matching row was found.
-l,--files-with-matches
Print a list of files that match the template.
-l,--files-without-match
Print a list of files that do not match the template.
-n,--line-number
Print the line number before the matching line.
-s,--silent
Does not display error messages about the absence or inability to read files.
-v,--revert-match
Reverse retrieve, showing only rows that do not match.
-w,--word-regexp
If referenced by/< and/>, the expression is searched as a word.
-v,--version
Displays software version information.
6. Examples
To use good
grepThis tool, in fact, is to write a regular expression, so this is not the case
grepOf all the functions of the example, only a few examples, to explain a regular expression of the wording.
$ ls-l |
grep' ^a '
Filters the contents of the LS-L output through a pipe, showing only the rows that start with a.
$
grep' Test ' d*
Displays the rows that contain test in all files that start with D.
$
grep' Test ' AA bb cc
Displays the row that matches test in the aa,bb,cc file.
$
grep' [a-z]/{5/} ' AA
Displays all the lines that contain at least 5 consecutive lowercase characters for each string.
$
grep' W/(es/) T.*/1 ' AA
If West is matched, the es are stored in memory and labeled 1, then search for any character (. *), followed by another ES (/1), and the line is displayed. If you use Egrep or
grep-E, you do not use the "/" to escape, directly written ' W (es) T.*/1 ' on it.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.