Grep:
Grep [-acinv] 'search string' file name
Example:
1. Search for a specific string: grep-n' the 'myfilename
2. Use [] to search for the set characters: To search for the two characters, such as test and taste, they have the same part, that is, T? St, you can do this:
Grep-n't [EA] st'myfilename [] represents only one character, regardless of the number of characters.
If you do not want to have other characters before some strings, you can use:
Grep-n '[^ g] oo' myfilename does not want to have the G character before oo
If you do not want OO to be preceded by lowercase letters, you can write [^ ABCD... z] Oo, but this is not convenient, because the ascll codes of lower-case letters are sequential, you can write as follows:
Grep-n' [^ A-Z] oo 'myfilename
In a group of character sets, if the character groups are consecutive, such as uppercase letters, lowercase letters, numbers, and so on, you can use [A-Z] [A-Z] [0-9] and other methods to write, if the string is required to be a number and English, all written together, into: [a-zA-z0-9], for example, to obtain the row with a number: grep-n '[0-9] 'myfilename
3. start and end of a line ^ $
We query the line with the character. If you want the line to appear only at the beginning, you need to use the locator:
Grep-n' ^ The 'myfilename
What if I want to list the rows in which the switch is a lowercase letter? You can do this:
Grep-n' ^ [A-Z] 'myfilename
If I do not want to switch to English letters, it can be like this:
Grep-n' ^ [^ A-Za-Z] 'myfilename
If you want to end a long trip with a line of the decimal point (.), the processing method is as follows:
Grep-n '/. $' myfilename
Because the decimal point has other meanings, you must use an escape character (/) to remove its special meaning.
Find out which row is blank:
Grep-n' ^ $ 'myfilename
Remove blank lines and comments from the file (take the c file as an example): grep-V '^ $'/etc/syslog. conf | grep-V '^ #'
4. Any one character (.) and repeated character (*)
To find G ?? D string, which consists of four characters, starting from G and ending with D. You can do this:
Grep-N 'G .. d' filename
Here. represents "absolutely an arbitrary character ".
If you want to list data with OO, OOO, and Oooo, that is to say, there must be at least two o s, what should we do?
Because (*) indicates "zero or multiple preceding re characters are repeated", (O *) indicates "null or more than one o character ".
For example, if there are at least two o strings, write as follows:
Grep-N 'ooo * 'the first two o of filename are required, and the third O is dispensable.
5. limit the range of consecutive repeated characters {}
We can use (.) (*) to set 0 to unlimited repeated characters. What if we want to limit the number of repeated characters in a range? For example, how do I find 2-5 consecutive o strings? It is necessary to use the character {} with a limited range, because the symbols {And} have special significance in shell, because it must use escape characters/To make it meaningless. Suppose we want to find two o strings, we can do this:
Grep-N 'o/{2/} 'filename
Suppose we want to find the g followed by 2 ~ Five O, and then a G string, it will be like this:
Grep-N 'go/{2, 5/} 'filename
If you want Gooo... G? In addition to Gooo * G, you can use the following method to find out:
Grep-N 'go/{2,/} 'filename repeats the O character for more than two consecutive times.
Select [! Range]. The regular expression is [^ range].