Grep
Introduction
One of the Three Musketeers of the Linux text processing, the File filtering tool (the other two musketeers for sed: Text Editing Tool,awk: Text Report Generator) owned, according to user-specified "mode" Matches the target text line by row, and prints the function of the matching line.
Use
Grep can also use regular expression characters in addition to normal character usage.
Important command options for Grep
--color=auto keyword highlighting (centos7 default)
The *-V keyword displays rows that do not contain a key word, and the inverse
The *-i keyword ignores the case of the keyword
-N Displays the result of adding line numbers before each line
-C displays only the number of rows found for the result
*-o displays only the keywords that match, and does not display other content from peers
*-w keyword matches whole word
*-e equals egrep using an extended regular expression
Regular Expressions
Regular Expressions regexp, also known as regular expressions, are written by a class of special characters and text, some of which have characters (metacharacters) that do not represent literal meanings, but are functions of control or wildcard. is to make a "rule string" that is used to express a filter logic for a string, using predefined characters and combinations of these specific characters . A regular expression is a text pattern that describes one or more strings to match when searching for text .
use of regular expressions
character OFDM characters with special meanings have character matching, number of matches, position anchoring, grouping of four functions. Mastering these four functions will do more with less when using regular expressions.
The following four functions deal with meta characters.
1. character matching :
When we want to find something in the text, we first need to do a text search, then we need to match some of the characters to find what we want.
. In the meta-character character to match any single character
[] matches any single character within the specified range
[^] matches any single character outside the specified range
[: Alnum:] Letters and numbers
[: Alpha:] represents any English uppercase and lowercase characters, i.e. A-Z, A-Z
[: Blank:] white space characters (spaces and tabs)
[: Space:] Horizontal and vertical white space characters (more than [: blank:] contains a wide range)
[: Cntrl:] non-printable control characters (backspace, delete, alarm ...) )
[:d igit:] decimal digits [: xdigit:] hexadecimal digits
[: Graph:] printable non-whitespace characters
[:p rint:] printable characters
[:p UNCT:] Punctuation
2. Number of Matches
* Indicates the preceding character matches any time, can be 0 times, can infinite, greedy mode
. * Indicates any length of any content
\? Indicates that the preceding content matches 0 or 1 times
\+ indicates that the previous content matches more than 1 times
\{n\} matches the preceding character n times
\{m,n\} matches the preceding character at least m times, up to N times
\{,n\} matches the preceding character up to n times
\{n,\} matches the preceding character at least n times
3. location anchoring :
Enables you to pin a regular expression to the beginning or end of a line. They also enable you to create regular expressions that appear within a word, at the beginning of a word, or at the end of a word.
A locator is used to describe the bounds of a string or word. ^ and $ respectively refer to the beginning and end of the string,\b describes the front or back of the word, and\b represents a non-word boundary.
^ Beginning of the line anchor, for the leftmost mode
$ line End anchor for the right side of the pattern
^pattern$ for pattern matching entire row
^$ Empty Line
^[[:space:]]*$ Blank Line
\< or \b The first anchor, used for the left \> or \b ending anchoring of the word pattern; right side for Word mode
\<pattern\> Match Whole Word
4. grouping
When the current square character is bound together by \ (\), the character at this time can be manipulated as a whole.
The contents of the pattern in the grouping brackets are recorded in internal variables by the regular expression engine , which are named: \1, \2, \3, ...,
\1 represents the character that matches the pattern between the first opening parenthesis and the matching closing parenthesis from the left.
At this point, you can use latter references to implement certain functions, such as:
' ^\ (bin\). *\1$ ' where the \1 represents the first parenthesis in front of the bin wrapped up
or:\|
Example:a\|b:a or B
Special metacharacters.
Symbol |
Describe |
* |
The number of occurrences of a character 0 or more times |
. |
Arbitrary single-character |
^/$ |
Start or end |
N |
Indicates a match n times |
[] |
Any single character in parentheses |
\ |
Translation characters |
+ |
A character appears one or more times |
? |
Occurs 0 or 1 times |
() |
Grouping representations |
\ n |
latter references |
{M,n} |
The number of occurrences is higher than m less than n |
Summary : The use of regular expressions can be used in a variety of computer languages, he is a rule definition, to implement the operation of the string.
The grep and regular expression syntax of the Linux text-processing tool