Before you learn the regular expression, be clear and keep it in mind:
In Linux, wildcards are interpreted by the shell, while regular expressions are interpreted by commands, so don't confuse them. Remember!!!
usually have three text Processing tools/commands: grep, sed, awk, all three of them can interpret the regular. Let me show you how to use the grep command.
First, use the grep command to search for content in a file
The grep and Egrep commands can search for a particular character pattern in the contents of one or more files, also known as regular expressions (regular expressions). a pattern can be a single character, a string, a word, or a sentence.
A regular expression is a pattern that describes a set of strings. Regular expressions are made up of mathematical expressions that combine smaller expressions into a new expression by using operators. A regular expression can be either plain text or some special character used to produce a pattern. To further define a search pattern, the grep command supports several meta-characters, also known as wildcards, for the following regular expressions.
PS: The regular expression language consists of two basic character types: literal (normal) text word and metacharacters character. Metacharacters enable regular expressions to have processing power. A meta-character is a special character in a regular expression that can be used to specify its leading character (that is, the character in front of the metacharacters) in the target object.
c*: will match 0 or more characters C
.: will match any one and can only be one character (except for line break)
[XYZ]: matches any one of the characters in the square brackets
[^XYZ]: will match all characters that do not include the characters in square brackets
^: Locking the beginning of a line
$: Lock the end of a line
In the basic regular expression, such as metacharacters *, +, {}, |, and () have lost their original meaning, if you want to reply to their original meaning to be preceded by a backslash \, such as \*, \+ and so on.
The grep command is used to search for a specific pattern in each file or on standard output. When you use the grep command, each line that contains a specified character pattern is printed on the screen, and the grep command has the following syntax format:
grep option Mode file name
Options can change the way the grep command is searched, and each option can be used in the Egrep and Fgrep commands in addition to the-w option. The common options for the grep command are as follows:
-C (count): Lists only the number of rows that contain patterns
-I (Ignore): Ignore the case of letters in the pattern
-L (list): Lists filenames with matching rows
-N: List line numbers before matching lines
-V: Lists rows with no matching pattern (inverse)
-W: Search for an expression as a complete word, ignoring those partially matched rows
-Q: Silent mode, no output, you have to use $? To determine the success of the execution, that is, there is no filtering to the desired content
-A N: If the match succeeds, the matching row and the subsequent n rows are printed together
-B N: If the match succeeds, the matching row and its first n rows are printed together
-C N: If the match succeeds, the matching row and its n rows are printed together
PS: If you are searching for multiple files, the results of the grep command only show the file name of the matching pattern found in the files;
If you are searching for a single file, the result of the grep command displays each row that contains a matching pattern.
Above is the basic use of the grep command. But sometimes a simple regular expression cannot locate what you are searching for, such as searching for rows of data that satisfy both pattern one and pattern two, then the EGREP command can be useful.
The syntax format of the EGREP command is the same as the grep command. However, the Egrep command is used to search for specific patterns in the contents of one or more files using meta-characters of extended regular expressions. The meta-character of an extended regular expression contains the regular expression metacharacters used in the grep command, along with some additional metacharacters, roughly the following:
+: Match one or more leading characters
A|b: Match A or b
(RE): matches the regular expression in parentheses RE
Below I will use some basic examples to improve your understanding of grep and egrep command usage. Given that all of the options in the grep command are Egrep (egrep more powerful), I'll use the egrep command to demonstrate for your convenience:
Example one: Shows the file/etc/passwd contains the root row
Example two: Output any rows in/etc/passwd that contain bash, and also output the contents of the next two rows
Example three: Shows how many lines in/etc/passwd contain nologin
Example four: shows that the rows in the/etc/passwd contain root and the line number is output
Example five:
New users abominable, Abominate, Anomie, atomize, write regular expressions, match them up
Example six: New user alex213sb, wpq2222b, Yh438pig, egon666, Egon filter out the user name consists of the letter + number + Letter line
Example VII: Shows all filenames containing root in/etc directory
Example eight: Filter out all comments and all blank lines in/etc/ssh/sshd_config
The above example is only a small part, as long as you practice, you will soon be able to grasp the use of the grep command ^^
Use of the grep command for Shell programming