1.
GrepIntroduction
Grep(Global Search Regular Expression (re) and print out the line, full search for regular expressions and print out rows) is a powerful text search tool, it can use regular expressions to search for text and print matching rows. UNIXGrepFamily includesGrep, Egrep, and fgrep. The egrep and fgrep commands only followGrepThere is a small difference. Egrep isGrepAnd supports more re metacharacters. fgrep is fixed.GrepOr fastGrepThey regard all letters as words. That is to say, the metacharacters in regular expressions indicate their literal meanings, and are no longer special. Linux uses the GNU versionGrep. It is more powerful and can use egrep and fgrep functions through the-G,-E,-F command line options.
GrepIt searches for a string template in one or more files. If the template contains spaces, it must be referenced. All strings after the template are treated as file names. The search result is sent to the screen without affecting the content of the original file.
GrepIt can be used in shell scripts becauseGrepA status value is returned to indicate the search status. If the template search is successful, 0 is returned. If the search is unsuccessful, 1 is returned. If the search file does not exist, 2 is returned. We can use these return values to automate text processing.
2.GrepRegular Expression metacharacters (basic set)
^
For example, '^ grep' matches allGrep.
$
For example, 'grep $ 'matches allGrepThe end of the row.
Match a non-linefeed character, for example, 'gr. P' matches gr followed by any character and then p.
*
Match zero or multiple previous characters, for example, '* grep' matches all one or more spaces followedGrep. . * Represents any character.
[]
Matches a character in a specified range, such as '[Gg] rep'GrepAndGrep.
[^]
Match a character that is not within the specified range, such as '[^ A-FH-Z] rep' match a letter that does not start with the A-R and T-Z, followed by the rep line.
/(../)
Mark matching characters, such as '/(Love/)'. Love is marked as 1.
/<
Specify the start of a word, for example, '//>
Anchor specifies the end of a word, such as 'grep/> '.GrepThe row of the ending word.
X/{M /}
Repeat the characters X and M, for example, '0/{5/} 'matches the rows that contain 5 o.
X/{M ,/}
Repeat character X for at least m times, for example, 'o/{5,/} 'matches rows with at least 5 o.
X/{M, N /}
Repeated characters X, at least m times, no more than N times. For example, 'o/{5, 10/} 'matches rows of 5-10 o.
/W
Match text and number characters, that is, [A-Za-z0-9], such as: 'G/W * P' match with g followed by zero or multiple characters or numbers, followed by P.
/W
/W reverse form, matching one or more non-word characters, such as periods and periods.
/B
Word lock, for example, '/bgrepb/' only matchesGrep.
3. For egrep andGrep-E metacharacters Extension Set
+
Matches one or more previous characters. For example, '[A-Z] + able' matches one or more lower-case letters followed by able strings, such as loveable, enable, and disable.
?
Matches zero or multiple previous characters. For example, 'gr? P' matches gr followed by one or no characters, and then the row of P.
A | B | C
Match A, B, or C. For example, grep | sed matchGrepOr SED
()
Group symbols, such as: Love (able | RS) ov + matches loveable or lovers and matches one or more ov.
X {m}, X {M,}, X {m, n}
Same role as X/{M/}, X/{M,/}, X/{M, N /}
4. POSIX character class
POSIX (the Portable Operating System Interface) adds a special character class, for example [: alnum:] is another way of writing a A-Za-z0-9 to preserve one character encoding in different countries. Put them in the [] sign to become a regular expression, such as [A-Za-z0-9] or [[: alnum:]. In LinuxGrepApart from fgrep, POSIX character classes are supported.
[: Alnum:]
Character
[: Alpha:]
Character
[: Digit:]
Numeric characters
[: Graph:]
Non-empty characters (non-space and control characters)
[: Lower:]
Lowercase characters
[: Cntrl:]
Control characters
[: Print:]
Non-empty characters (including spaces)
[: Punct:]
Punctuation Marks
[: Space:]
All blank characters (new line, space, Tab)
[: Upper:]
Uppercase characters
[: Xdigit:]
Hexadecimal number (0-9, A-F, A-F)
5.GrepCommand Options
-?
At the same time, the upper and lower lines of matching rows are displayed? Line, such:Grep-2 pattern filename: both upper and lower lines of matched rows are displayed.
-B, -- byte-offset
Print the block number of the row before the matching row.
-C, -- count
Only the number of matched rows is printed, and the matching content is not displayed.
-F file, -- file = File
Extract templates from files. The empty file contains 0 templates, so nothing matches.
-H, -- no-filename
When multiple files are searched, the matching file name prefix is not displayed.
-I, -- ignore-case
Ignore case differences.
-Q, -- quiet
Cancel display. Only the exit status is returned. 0 indicates that the matched row is found.
-L, -- files-with-matches
Print the list of files matching the template.
-L, -- files-without-match
Print the list of files that do not match the template.
-N, -- line-Number
Print the row number before the matched row.
-S, -- silent
The error message about the nonexistent or unreadable file is not displayed.
-V, -- revert-match
Reverse search: Only unmatched rows are displayed.
-W, -- word-Regexp
If it is referenced by/<and/>, the expression is used as a word search.
-V, -- version
Displays the software version information.
6. Instance
Make good useGrepThis tool is actually to write a regular expression, so it is not correct hereGrepAll the functions of the regular expression are described in the following example.
$ LS-L |Grep'^'
Filter the LS-L output content in the MPs queue and display only the rows starting with.
$Grep'Test' D *
Display all the lines containing test in files starting with D.
$Grep'Test' AA BB CC
The row Matching Test is displayed in the AA, BB, and CC files.
$Grep'[A-Z]/{5/}' AA
Display All rows of a string that contains at least five consecutive lowercase characters.
$Grep'W/(ES/) T. */1' AA
If West is matched, ES is stored in the memory, marked as 1, and any characters (. *). These characters are followed by another ES (/1). If they are found, the row is displayed. If you use egrep orGrep-E, you do not need to escape the "/" number and simply write it as 'W (ES) T. */1.