Shell Programming Three Musketeers grep

Regular Expressions

The definition of regular expressions:
Regular expressions also known as regular expressions, regular expressions. Code often abbreviated as regex, regexp or RE. Regular expressions are used to describe the use of a single string, the string line with a series of matching syntax rules, in simple terms, is a way to match the string, through some special symbols, quick search, delete, replace a specific string.
Basic regular expressions
to httpd configuration file, for example
in order to prevent mistakes in the configuration file for httpd damage, first of all let's copy httpd configuration file elsewhere.
Shell Programming Three Musketeers grep
1) look for a particular character
can be found from the specific character file httpd.txt "the" location
can use the command: grep -n 'the' httpd.txt
Shell Programming Three Musketeers grep
find that do not contain "the" character
can use the command: grep - VN 'the' httpd.txt
Shell Programming Three Musketeers grep
2), the use of brackets "[]" to find a collection of characters
is desired to find the "then" and "they" two strings, the two strings can be found contains "the" . Run the following commands: grep -n 'the [ny] ' httpd.txt
to simultaneously find the "then" and "they" two strings. "[]" In regardless of the number of characters, are only representative of a character, that "[ny]" means match "n" or "y".
Shell Programming Three Musketeers grep
Finds repeated single character "oo"
can use the command: grep -n 'oo' httpd.txt
Shell Programming Three Musketeers grep
looks for the string "oo" not preceded of "r"
can use the command: grep -n '[^ r] oo' httpd. TXT
Shell Programming Three Musketeers grep

If desired "oo" in front of the presence of capital letters, can be used "grep -n '[^ AZ] oo'httpd.txt" command implementation
Shell Programming Three Musketeers grep

Finds line numbers can be achieved by the command "grep -n '[0-9]' httpd.txt"
Shell Programming Three Musketeers grep

3) Find the first line "^" and end of line characters "$"
inquiry begins with a capital letter line using grep -n '^ [AZ]' httpd.txt
Shell Programming Three Musketeers grep
inquiry. "" End of the line using grep -n '. $' httpd.txt
because the decimal point (.) in a regular expression is a character metacharacters (will be mentioned later), so here need to "\" will have a special significance with the escape character is converted into ordinary characters.
Shell Programming Three Musketeers grep
Queries blank line, perform "grep -n '^ $' httpd.txt " command

4), to find any character "." Repetitive character "*"
is the decimal point in the positive expression (.) Is a meta-character, represents any character, for example, to find the beginning of r, the end of t, between two unknown character string, can use the command grep -n 'r..t' httpd.txt.
Shell Programming Three Musketeers grep

Comprising at least two or more query strings o, "grep -n 'ooo *' httpd.txt" is executed command.
Shell Programming Three Musketeers grep

5), to find the range of consecutive characters "{}"
as "{}" has a special meaning in the Shell, so when using "{}" character, need to use an escape character "\", convert "{}" characters into ordinary characters
(1) two query character o
grep -n 'o {2}' httpd.txt
Shell Programming Three Musketeers grep

Guess you like

Origin blog.51cto.com/14449528/2441306