Reference Links:
Http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html
the grep family includes grep, Egrep, and Fgrep. Egrep and Fgrep commands are only a small difference from grep.
Egrep is the extension of grep, which supports more re metacharacters , and fgrep is fixed grep or fast grep, which regards all the letters as words, that is to say, The metacharacters in a regular expression represents a literal meaning back to itself, no longer special .
Linux uses the GNU version of grep. It is more powerful and can use the Egrep and FGREP functions with the-G,-e,-f command line options.
grep common usage:
-A: binary files are searched for data in the form of a text file
-C: Calculates the number of ' search string ' found
-I : ignores case differences , so case is considered the same
-N: Output line number by the way
- V: Reverse selection , which shows the line without the ' search string ' content!
--color=auto: Can be found in the keyword part of the color display OH !
eg
Remove the/etc/passwd, where Root is present.
[[email protected] ~]# grep root/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
Remove the/etc/passwd, which is the root row, and display the line numbers of the rows in/etc/passwd
[Email protected] ~]# grep-n root/etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
Show the last three lines of the keyword with the first two lines
[Email protected] ~]# dmesg |grep-n-a3-b2 ' eth '
476-udev:starting version 147
477-piix4_smbus 0000:00:01.3:smbus Base Address Uninitialized-upgrade BIOS or use force_addr=0xaddr
478:initialising Xen Virtual Ethernet driver.
479-alloc Irq_desc for 928 on node 0
480-alloc Kstat_irqs on Node 0
481-alloc Irq_desc for 927 on node 0
grep tar * searches the current directory for lines with tar, displaying rows and files
grep-r tar * searches the current directory and its subdirectories for rows with tar, displaying rows and files
grep-l-R Tar * searches the current directory and its subdirectories for a file with a tar line, but does not display matching rows, only matching files are displayed
grep and regular expressions
[] There are a few bytes in it, he would like to represent a "one" byte
[[email protected] ~]# grep-n "t[ae]st" Agc.txt #只能匹配a或e, cannot match ae simultaneously
4:i can ' t finish the test.
5:oh! The soup taste good.
inverse selection of the character class [^] want to search for a line with Oo, but do not want oo preceded by G
[Email protected] ~]# grep-n [^g]oo agc.txt
7:apple is my favorite food.
8:football game isn't use feet only.
9:google is the best tools for search keyword. #有一个too
10:goooooogle yes! #这个oo前面还有o
Don't want lowercase/uppercase/numeric characters in front of OO
grep-n [^a-za-z0-9]oo Agc.txt
Want to get the line with numbers
[Email protected] ~]# grep-n [0-9] Agc.txt
6:oh! The soup teaste good 9.
Beginning of line with trailing bytes ^ $
List only the lines that begin with the
[Email protected] ~]# grep-n "^the" Agc.txt
5:the soup taste good.
6:the Soup Teaste Good 9.
List only lines beginning with uppercase characters
[Email protected] ~]# grep-n "^[a-z]" Agc.txt
4:i can ' t finish the test.
8:football game isn't use feet only.
To list lines that do not begin with the English alphabet
[[email protected] ~]# grep-n "^[^a-za-z]" Agc.txt # character class reverse selection [^]
1:9tar
Take it out? End of Line
[Email protected] ~]# grep-n "? $" agc.txt
2:ddddddddddddd?
remove the line that ends with. because the decimal point has a special meaning, you need to escape escaping
[Email protected] ~]# grep-n "\.$" Agc.txt
4:i can ' t finish the test.
8:football game isn't use feet only.
9:google is the best tools for search keyword.
Find Blank Lines
[Email protected] ~]# grep-n "^$" Agc.txt
8:
Any one of the bytes. With repeating bytes *
. (decimal point): Represents "must have an arbitrary byte " meaning;
* (asterisk): represents "repeating the previous character, 0 to infinity " meaning, for the combined form
Find out g?? A string of D
[Email protected] ~]# grep-n "G.. D "Agc.txt
5:the Soup Taste good
6:the Soup Teaste Good 9
Want to list data with Oo, OOO, oooo, etc.
Analysis:
* Represents the meaning of "repeating 0 or more preceding RE characters", so "o*" stands for: "Having empty bytes or an O or more bytes ",
Therefore, " grep-n ' o* ' Regular_express.txt" will print out all the data on the terminal !
So, when we need a "minimum of two + O" strings, we need ooo*
[Email protected] ~]# grep-n "ooo*" Agc.txt
5:the Soup Taste good
6:the Soup Teaste Good 9
7:apple is my favorite foo
9:football game isn't use feet only.
10:google is the best tools for search keyword.
11:goooooogle yes!
Want string start and end is G, but two g can only exist between at least one o, that is Gog, Goog, Gooog ....
[Email protected] ~]# grep-n "Goo*g" Agc.txt
10:google is the best tools for search keyword.
11:goooooogle yes!
Find the line where G begins with G and the characters are optional
[[email protected] ~]# grep-n "G.*g" Agc.txt #. * The RE means that any character is very common
8:gg
10:google is the best tools for search keyword.
11:goooooogle yes!
Find the "any number" line, because there are only numbers, so
[Email protected] ~]# grep-n "[0-9][0-9]*" Agc.txt
1:9tar
6:the Soup Teaste Good 9
Qualifying Continuous RE character range {}
For example, I want to find out two to five o continuous string, how to do? This is the time to use the qualified character {}. But since the symbol {and} is of special significance in the shell , we have to use the character \ To make him lose special meaning .
The syntax for {} is this, to find two strings of O
[[email protected] ~]# grep-n "o\{2\}" Agc.txt
5:the Soup Taste good
6:the Soup Teaste Good 9
7:apple is my favorite foo
9:football game isn't use feet only.
10:google is the best tools for search keyword.
11:goooooogle yes!
To find the G followed by 2 to 6 O, and then followed by a G string
[Email protected] ~]# grep-n "Go\{2,6\}g" Agc.txt
10:google is the best tools for search keyword.
11:goooooogle yes!
Want to be 2 o GOOOO....G
[Email protected] ~]# grep-n "Go\{2,\}g" Agc.txt
10:google is the best tools for search keyword.
11:goooooogle yes!
Extended grep (GREP-E or Egrep):
The main benefit of using the extended grep is the addition of additional regular expression meta-character sets.
Print All rows that contain NW or EA
[Email protected] ~]# egrep "nw| EA "Agc.txt
Northwest NW Charles Main 3.0.98 3 34
Eastern EA TB Savage 4.4.84 5 20
[Email protected] ~]# grep-e "nw| EA "Agc.txt
Northwest NW Charles Main 3.0.98 3 34
Eastern EA TB Savage 4.4.84 5 20
For standard grep, extended option-E is automatically enabled if \,grep is preceded by an extension metacharacters.
[[email protected] ~]# grep "nw\| EA "Agc.txt
Northwest NW Charles Main 3.0.98 3 34
Eastern EA TB Savage 4.4.84 5 20
Search all rows that contain one or more 3
[[email protected] ~]# grep "3\+" Agc.txt
The Soup teaste Good 93
Northwest NW Charles Main 3.0.98 3 34
[Email protected] ~]# egrep "Agc.txt "
The Soup teaste Good 93
Northwest NW Charles Main 3.0.98 3 34
[Email protected] ~]# grep-e "Agc.txt "
The Soup teaste Good 93
Northwest NW Charles Main 3.0.98 3 34
Search for one or more consecutive no rows
[Email protected] ~]# egrep "(NO) +" Agc.txt
Ggnono
Adbcnoinonodd
Northwest NW Charles Main 3.0.. 98 3 34
Football game isn't use feet only.
[Email protected] ~]# grep-e "(NO) +" Agc.txt
Ggnono
Adbcnoinonodd
Northwest NW Charles Main 3.0.. 98 3 34
Football game isn't use feet only.
[[email protected] ~]# grep "\ (no\) \+" Agc.txt
Ggnono
Adbcnoinonodd
Northwest NW Charles Main 3.0.. 98 3 34
Football game isn't use feet only.
Do not use regular expressions
The fgrep query is faster than the grep command , but not flexible enough: it can only find fixed text, not regular expressions.
If you want to find a line that contains an asterisk character in a file or output
[Email protected] ~]# grep-f "*" Agc.txt
9tar**
**ggnono
[Email protected] ~]# fgrep "*" Agc.txt
9tar**
**ggnono
This article is from the "Youth Deng Yong" blog, please be sure to keep this source http://dengyong.blog.51cto.com/8409869/1871005
The grep command is detailed