Regular Expressions (Regular expression) are a standard for string processing, and for system administrators, regular is a compulsory skill. For example: The system generates too much data, as a system administrator to see so much information every day, from thousands of rows of data to find a row of problematic information, how to do? This time, we can through the function of regular expression, these login information processing, only to take out the problem of information for analysis, so that your system management work will be more intuitive, easy!
The commands we use frequently in regular expressions are the Three musketeers: grep, sed, awk. The three Musketeers are usually combined with regular special characters to accomplish the operations we need.
Options and Parameters:-A: The following can be added to the number, for after the meaning, in addition to listing the row, the subsequent n rows are also listed;-B: The following can add a number, for befer meaning, in addition to listing the row, the preceding n rows are also listed; Lor=auto can list colors for the correct fetch data (use alias grep= ' grep--color=auto ', add to ~/.BASHRC Permanent)-I: Ignores the case of matching characters;-V: Prints rows that are not matched;-N: Output line number;-O : Prints only matching fields instead of rows, used to count how many times to match to-e: This is equivalent to Egrep, using an extended regular expression
After playing with the grep common options, combine grep to see the special characters commonly used by the underlying regular.
Regular expression special character induction
Example: Searching for the line starting with the beginning of #, parallel travel number
Grep-n ' ^# ' filename
Example: End of line is! , and the number of travel
Grep-n '!$ ' filename
Example: The search string can be (Eve) (Eae) (EEE) (e e), but not only (EE)! That is, E and E in the middle "must" only have one byte, and the blank byte is also a byte!
Grep-n ' e.e ' filename
Example: Search for the line containing the single quote '!
GREP-N \ ' filename
Example: Find a String containing (es) (Ess) (ESSS) and so on, note that because * can be 0, ES is also compatible with the search string. In addition, because * is a repetition of the "previous re character" of the symbol, so, before the * must be connected to a RE-character Fu Yi! For example, any byte is ". *"!
Grep-n ' ess* ' filename
Example: Search for a line containing (GL) or (GD), it is necessary to pay special attention to [] in the "to represent a byte to be searched", such as "A[afl]y" for the search of the string can be Aay, Afy, Aly that [AFL] for a or F or l mean!
Grep-n ' g[ld] ' filename
Example: Search for the line that contains any number! Pay special attention to the minus sign in the byte set []-it's special, he represents all the contiguous bytes between two bytes! But this continuity is related to ASCII encoding, so your coding needs to be configured correctly (in bash, you need to determine if LANG and LANGUAGE variables are correct!) For example, all uppercase bytes are [a-z]
Grep-n ' [A-z] ' filename
Example: The search string can be (Oog) (Ood) but not (oot), that ^ within [], the meaning of the representation is "reverse selection" meaning. For example, I don't want to capitalize bytes, then [^a-z]. However, it is important to note that if you search by Grep-n [^a-z] Regular_express.txt and find all the lines in the file are listed, why? Because this [^a-z] is a "non-uppercase byte" meaning, because each row has a non-uppercase byte, for example, the first line of "Open Source" has p,e,n,o .... And so on, little handwriting.
Grep-n ' oo[^t] ' filename
Meaning: If \{n\} is the previous RE character of a continuous n
Meaning: If \{n,\} is a continuous n more than the previous RE character! Example: A string of 2 to 3 o existence between G and G, i.e. (GOOG) (Gooog)
Grep-n ' go\{2,3\}g ' filename
\< and \> meanings:
The character after the \< must be the first part of a word;
\> the preceding character must be the last part of a word;
Note: A word defines a combination of letters, numbers, underscores
So \<word\> must be separate word words to match, other symbols such as: ()/etc will be treated as Word division number
Grouping \ (\) Meaning:
The contents of the parentheses as a whole, such as \ (ab\) * Match 0 to Infinity AB
Example: Test file contents are as follows:
Love:lover
Love:liker
Like:lover
Like:liker
How do I remove rows 1 and 4?
When using the grouping function, we can \1 \2 \3 ... To reference, so:
grep ' \ (. *\): \1r ' Test
This article is from the "Sweetsmile" blog, make sure to keep this source http://daizhancheng.blog.51cto.com/9708457/1632044
Linux Regular expressions