grep is one of the most widely used commands in Unix and Linux
grep allows for pattern lookups of text files. If a matching pattern is found, grep prints all rows that contain the pattern. GREP supports basic positive
The expression is also supported by its extension set.
Test file datafile, as follows
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/04/wKiom1U9qy3wQkriAAEJag0M87o558.jpg "title=" Data " alt= "Wkiom1u9qy3wqkriaaejag0m87o558.jpg"/>
The 1.GREP General format is:
grep[Options] Basic Regular expression [file]
Here the basic regular expression can be a string.
2. When entering string parameters in the grep command, it is best to enclose them in double quotation marks.
One is to avoid being misunderstood as a shell command.
The second is a string that can be used to look up multiple words
You should also use double quotation marks when calling variables
3.grep Common Options
The common grep options are:
-C outputs only the count of matching rows.
-I is case-insensitive (only for single-character).
-H does not display a file name when querying multiple files.
-L Only output file names that contain matching characters when querying multiple files.
-N Displays matching lines and line numbers.
-S does not display error messages that do not exist or have no matching text.
-V Displays all lines that do not contain matching text.
4.grep and regular expressions
Use regular expressions to make pattern matching a rule, so you can add more choices to the extracted information. Use
It is best to enclose the regular expression in single quotation marks, which prevents the proprietary patterns used in grep from using some shell commands
Confusion in special ways.
1. Mode range
Extraction Code is 4 8 4 and 4 8 3
#grep ' 48[34] ' datafile
2. Do not match the beginning of the line
Draw a record so that its beginning is not 4 8
#grep ' ^[^48] ' datafile
3. Set case
Using the-i switch, you can block case or use [] mode
#grep ' [ss]ept ' datafile
If the above conditions are met, it is necessary to include 483.
#grep ' [ss]ept ' datafile | grep 483
4. Match any character
All codes starting with L and ending with D
#grep ' K ... D ' datafile
The first two are uppercase letters, the middle two arbitrarily, and end with C
#grep ' [a-z][a-z]. C ' datafile
5. Date Query
Query all records that end with 1996 or 1998 starting with 5
#grep ' 5..199[6,8] ' datafile
6. Range Combination
Get the first column of code
#grep ' ^[0-9][0-5][0-6] ' datafile
7. Probability of pattern occurrence
All rows containing the number 4 repeating at least two occurrences
#grep ' 4\{2,\} ' datafile
Take the record to include the number 999 (three x 9)
grep ' 9\{3,\} ' datafile
The query repeats the number of occurrences of all rows, the syntax is as follows, the number 9 repeats two times
grep ' 9\{2\} ' datafile
Repeated occurrences are within a certain range
Match number 8 repeats 2 to 6 times and ends with 3
#grep ' 6\{2,6\}3 ' datafile
8. Use grep to match "and" or "or" mode
grep command Plus-e parameter, this extension allows the use of extended pattern matching
Gets the code of 219 or 216,
#grep-E ' 219|216 ' datafile
9, empty Line
Use a combination of ^ and $ to query for blank lines. Use the-n parameter to display the actual number of rows:
#grep ' ^$ ' datafile
10. Match Special characters
such as $. ' " *[]^|\+, must be added before a specific character
#grep ' \. ' DataFile
Or
#grep ' \ ' datafile
To query the file name controll.conf
#grep ' controll\.conf '/path/to/some
11. Query the format file name
Find six lowercase characters in a directory, followed by a period, followed by two uppercase and lowercase files
#grep ' ^[a-z]\{1,6\}\. [A-z]\{2\} '/path/to/some
12. Query IP Address
#grep ' [0-9]\{3\}\. [0-9]\{3\}\. [0-9]\{3\}\. [0-9]\{3\} ' ipfile
13. Class Name
[[: Upper:]] [A-z] [[: Alnum:]] [0-9a-za-z]
[[: Lower:]] [A-z] [[: Space:]] space or TAB key
[[:d Igit:]] [0-9] [[: Alpha:]][a-za-z]
Example
Find starts with 5, followed by at least two uppercase letters.
#grep ' 5[[:upper:]][[:upper:]] ' datafile
5, Egrep
Egrep represents expression or extendedgrep, which is appropriate for the case. Egrep accepts all regular expressions, egrep
A notable feature of this is that you can use a file as a saved string and then pass it to Egrep as a parameter.
1. Check if there are account Louise, Matty or Pauline in the system.
Using the WHO command and piping output to egrep
W.H.O. | Egrep (Louise|matty|pauline)
2. You can also use the ^ symbol to exclude strings. If you want to view users on the system, but do not include Matty and Pauline
W.H.O. | Egrep-v ' ^ (matty|pauline) '
3, if you want to query a list of files, including shutdown,shutdowns,reboot and reboots, use egrep can be
Ex SITU implementation
#egrep ' (Shutdown | reboot) (s)? '/path/to/some
This article is from the "Linux is belong to You" blog, make sure to keep this source http://jwh5566.blog.51cto.com/7394620/1639057
grep Learning Notes