grep is a powerful text search tool that uses regular expressions to search for text and print matching lines.
grep common usage
' Search String ' The filename option and parameter:-A: The number of times the binary file is searched for the data ' search string ' as atext file - I: Ignoring the difference in case, so the case is considered the same -N: The line thatoutputs the row number ' search string ' content! --color=auto: Can be found in the keyword part of the color display Oh!
1. The output of the row containing W in filename
[[email protected] log]# grep w filename wo ai wo jiawo hen haoda jia Hao wo shi yi ge hao ren
2. Show the line number
[[email protected] log]# grep-1: Wo ai wo jia3: Wo hen hao4:d a jia H Ao wo shi yi ge hao ren
3. How many lines contain a matching pattern
[[email protected] log]# grep-3
4. Combined with regular expressions
Search for character classes : Search for the two words of test or taste, [] There are several bytes in it, he would like to represent a "one" byte
is is Huan
Reverse Search --inverse selection of character classes [^]: If you want to search for a line that has OO, but do not want to have g in front of Oo, the following
' [^g]oo ' 8 is a super
beginning of line with trailing bytes ^ $
Attention:
^ symbols, which are different from outside of the character class symbols (brackets [])! In [] represents the "reverse selection", outside [] represents the meaning of positioning at the beginning of the line!
As long as the line at the beginning is Wo
[[email protected] log]# grep-n ^1: Wo ai wo jia3: Wo hen hao
find a blank line ----Only the beginning and end of the line (^$)
' ^$ ' 6:9:ten:
Any one of the bytes. With repeating bytes *
The meanings of these two symbols in regular expressions are as follows:
. (decimal point): means "must have an arbitrary byte" meaning; * (asterisk): represents "repeating the previous character, 0 to infinity" meaning, for the combined form
[Email protected] log]# grep-n ' G.. d ' filename
8:good is a gread
grep command in Linux