The general format of grep is:
Grep [Option] basic regular expression [file]
Here, the basic regular expression can be a string.
Double quotation marks
When you enter a string parameter in the grep command, it is best to enclose it with double quotation marks.
Single quotation marks should be used for invocation mode matching.
For example, "m y s t r I n g ". There are two reasons for doing this: one is to avoid misunderstanding as the s h e l command, and the other is to find strings composed of multiple words.
When you call a variable, double quotation marks should also be used, such as: grep "$ myvar" file name. Otherwise, no results will be returned.
Common grep 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.
Before starting the discussion, you can create a file, insert a piece of text, and add the <tab> key after each column. In the grep command example, the vast majority name data. f. Generate a file. The record structure of data. F is as follows:
Column 1st: City Location Number.
Column 2nd: Month.
Column 3rd: storage code and delivery year.
Column 4th: product code.
5th columns: Unified product price.
Column 6th: ID number.
7th columns: qualified quantity.
The file content is as follows:
$ Cat data. f
48 dec 3bc1977 lpsx68.00 lvx2a 138
483 Sept 5ap1996 USP 65.00 lvx2c 189
47 Oct 3zl1998 lpsx 43.00 kvm9d 512
219 dec 2cc1999 CAD 23.00 plv2c 68
484 Nov 7pl1996 CAD 49.00 plv2c 234
483 may 5pa1998 USP 37.00 kvm9d 644
216 Sept 3zl1998 USP 86.00 kvm9e 234
1. query multiple files
Query the word "Sort it" in all files"
$ Grep "Sort it "*
2. Row matching
1) display the text containing the "4 8" string:
$ Grep "48" data. f
2) Total number of output matched rows
$ Grep-c "48" data. f
4
Grep returns the number 4, indicating four rows containing the string "4 8.
3) number of rows
Display the number of rows meeting the matching mode:
$ Grep-n "48" data. f
The number of rows is in the first output column, followed by each matching row containing 4 8.
4) show unmatched rows
Show all rows that do not contain
$ Grep-V "48" data. f
5) exact match
You may have noticed that in the previous example, the extracted string "48" contains other strings including "48", such as 484 and 483, in fact, only 48 rows should be accurately extracted.
A more effective method for extracting exact matching using grep is to add \> after extracting strings. Assume that the precise extraction of 4 8 is as follows:
$ Grep "48 \>" data. f
I have tried another method, but it does not seem to work:
Note that there is a <tab> key after extracting the string in each matching mode, so the operation should be as follows:
<Tab> click the tab key.
$ Grep "48 <tab>" data. f
6) case sensitive
By default, grep is case-sensitive. to query strings that are case-insensitive, you must use the-I switch. The data. f file contains the month character Sept, both uppercase and lowercase. To obtain this string, use the following method:
$ Grep-I "48" data. f
Grep and Regular Expressions
Use regular expressions to add rules to pattern matching. Therefore, you can add more options to the extraction information. When using regular expressions, it is best to enclose them in single quotes to prevent confusion between the proprietary mode used in grep and some special methods of the s h e l command.
1. mode range
Extract the location of the city where the code is 4 8 4 and 4 8 3. You can use [] to specify the string range.
$ Grep "48 [34]" data. f
483 Sept 5ap1996 USP 65.00 lvx2c 189
484 Nov 7pl1996 CAD 49.00 plv2c 234
483 may 5pa1998 USP 37.00 kvm9d 644
2. does not match the beginning of the line
To make the beginning of a line not 4 or 8, you can use the ^ mark in square brackets.
$ Grep "^ [^ 48]" data. f
219 dec 2cc1999 CAD 23.00 plv2c 68
216 Sept 3zl1998 USP 86.00 kvm9e 234
For string 48
$ Grep-V "^ [^ 48]" data. f
3. Set case sensitivity.
Use the-I switch to block the case sensitivity of month s e p t
$ Grep-I "Sept" data. f
483 Sept 5ap1996 USP 65.00 lvx2c 189
216 Sept 3zl1998 USP 86.00 kvm9e 234
Alternatively, you can use the [] mode to extract all information of each row containing s e p t and s e p t.
$ Grep '[ss] EPT 'data. f
If you want to extract all months that contain s e p t, regardless of the case, and this row contains a string of 483, you can use the pipeline command, that is, the output of the command on the left of the symbol "|" is used as the input of the command on the right of "|. Example:
$ Grep '[ss] EPT 'data. f | grep 48
483 Sept 5ap1996 USP 65.00 lvx2c 189
Do not place the file name in the second grep command because the input information is from the output of the first grep command.
4. match any character
If you extract all the code starting with K and ending with D, you can use the following method, because the known code length is five characters:
$ Grep 'K... d' data. f
47 Oct 3zl1998 lpsx 43.00 kvm9d 512
483 may 5pa1998 USP 37.00 kvm9d 644
The above code is slightly changed. The first two are uppercase letters, the middle two are arbitrary, and end with C:
$ Grep '[A-Z] .. c 'data. f
483 Sept 5ap1996 USP 65.00 lvx2c 189
219 dec 2cc1999 CAD 23.00 plv2c 68
484 Nov 7pl1996 CAD 49.00 plv2c 234
5. Date Query
A common query mode is date query. Query all records whose names start with 5 and end with 1 9 9 6 or 1 9 9 8. Usage mode 5 .. 1 9 [6, 8]. This means that the first character is 5, followed by two vertices, followed by 1 9 9, and the remaining two digits are 6 or 8.
$ Grep '5 .. 199 [6, 8] 'data. f
483 Sept 5ap1996 USP 65.00 lvx2c 189
483 may 5pa1998 USP 37.00 kvm9d 644
6. Range combination
You must use [] to extract information. Assume that you want to obtain the city code. The first character is 0-9, the second character is between 0 and 5, and the third character is between 0 and 6. Use the following mode.
$ Grep '[0-9] [0-5 [0-6] 'data. f
48 dec 3bc1977 lpsx68.00 lvx2a 138
483 Sept 5ap1996 USP 65.00 lvx2c 189
47 Oct 3zl1998 lpsx 43.00 kvm9d 512
219 dec 2cc1999 CAD 23.00 plv2c 68
484 Nov 7pl1996 CAD 49.00 plv2c 234
483 may 5pa1998 USP 37.00 kvm9d 644
216 Sept 3zl1998 USP 86.00 kvm9e 234
A lot of information is returned, either desired or unwanted. The returned result is correct in reference mode.
$ Grep '^ [0-9] [0-5] [0-6] 'data. f
219 dec 2cc1999 CAD 23.00 plv2c 68
216 Sept 3zl1998 USP 86.00 kvm9e 234
In this way, an expected correct result is returned.
7. Probability of mode appearance
Extract all rows that contain numbers 4 at least twice. The method is as follows:
$ Grep '4 \ {2, \} 'data. f
483 may 5pa1998 USP 37.00 kvm9d 644
The preceding syntax indicates that the number 4 must be repeated at least twice. Note that there is no difference between the boundary characters.
Similarly, the extraction record contains the number 9 9 9 (three 9). The method is as follows:
$ Grep '9 \ {3, \} 'data. f
219 dec 2cc1999 CAD 23.00 plv2c 68
If you want to query all rows that repeat for a certain number of times, the syntax is as follows. The number 9 Repeat twice or three times:
$ Grep '9 \ {3 \} 'data. f
219 dec 2cc1999 CAD 23.00 plv2c 68
$ Grep '9 \ {2 \} 'data. f
483 Sept 5ap1996 USP 65.00 lvx2c 189
47 Oct 3zl1998 lpsx 43.00 kvm9d 512
219 dec 2cc1999 CAD 23.00 plv2c 68
484 Nov 7pl1996 CAD 49.00 plv2c 234
Sometimes you need to query the number of repeated occurrences within a certain range. For example, if a number or letter appears twice to 6 times, in the following example, the matching number 8 appears twice to 6 times and ends with 3:
$ Cat myfile
83
888883
8884
88883
$ Grep '8 \ {2, 6 \} 3' myfile
888883
88883
8. Use grep to match the "and" or "Mode
The grep command adds the-e parameter, which allows the extension mode to match. For example, to extract the city code as 2 1 9 or 2 1 6, the method is as follows:
$ Grep-e '2017 | 100' data. f
219 dec 2cc1999 CAD 23.00 plv2c 68
216 Sept 3zl1998 USP 86.00 kvm9e 234
9. Empty rows
Use ^ and $ to query empty rows. Use the-C parameter to display the total number of rows:
$ Grep-c '^ $' myfile
Use the-n parameter to display the actual line:
$ Grep-c '^ $' myfile
10. Match special characters
Query characters with special meanings, such as $. '"* [] ^ | \ +? , Must be preceded by a specific character \. Suppose you want to query all rows containing ".", the script is as follows:
$ Grep '\. 'myfile
Or a double quotation mark:
$ Grep '\ "'myfile
In the same way, to query the file name conftroll. conf (this is a configuration file), the script is as follows:
$ Grep 'conftroll \. conf' myfile
11. query formatted file names
Use a regular expression to match any file name. The system provides standard naming formats for text files. Generally, a maximum of six lower-case characters, followed by a period, followed by two upper-case characters.
$ Grep '^ [A-Z] \ {\} \. [A-Z] \ {1, 2 \}' filename
12. Query IP addresses
To view the NNN. NNN network address, if you forget the rest of the second part, only two periods are known, such as n nn. nn... To extract all the NNN. nnn ip addresses, use [0-9] \ {3 \} \. [0-0 \ {3 \}\. It means that any number appears three times, followed by a sentence, followed by any number three times, followed by a sentence.
[0-9] \ {3 \} \. [0-9] \ {3 \}\.'