Shell text filtering (grep) Classification: Linux Shell script LearningRead by 588Comment (0)FavoritesReport Shell Regular Expression extension tool Storage
Grep (Global Regular Expression version) allows Pattern searches for text files. If the matching mode is found, grep prints all rows containing the mode. Grep supports basic regular expressions and its extension set.
The general format of g r e p is:
Grep [Option] basic regular expression [file]
Here, the basic regular expression can be a string. If it is a string, add the "" sign, otherwise it is prone to errors.
Common g r e p options include:
-C only outputs the Count of matched rows.
-I is case insensitive (only applicable to single characters ).
-H: When querying multiple files, the file name is not displayed.
-L only names containing matching characters are output when multiple files are queried.
-N: the matching row and row number are displayed.
-S does not display the error message that does not exist or does not match the text.
-V: displays all rows that do not contain matched text.
Grep and Regular Expressions
Use regular expressions to add rules to pattern matching. Therefore, you can add more options to the extraction information. It is best to enclose the Regular Expression in single quotes to prevent confusion between the proprietary mode used in g r e p and some special methods of the s he l command.
To make good use of the grep tool, we need to write a regular expression. Therefore, we will not explain all the functions of grep here. We will only list a few examples to illustrate how to write a regular expression.
$ 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 or grep-E, you do not need to escape the "/" number and write it as 'W (ES) T. */1.
Grep Regular Expression metacharacters (basic set)
^
For example, '^ grep' matches all rows starting with grep.
$
For example, 'grep $ 'matches all rows ending with grep.
.
Match a non-linefeed character, for example, 'gr. P' matches gr followed by any character and then p.
*
Match zero or multiple previous characters, such as '* grep'. Match All one or more spaces followed by grep rows. . * Represents any character.
[]
Matches a character in a specified range, for example, '[Gg] rep' matches grep and grep.
[^]
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. For example, 'grep/> 'matches the row containing the word ending with grep.
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 /}
Repeat character 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
The word lock, for example, '/bgrepb/' matches only grep.