Http://man.chinaunix.net/newsoft/grep/open.htm
1 grep Introduction
grep (Global search Regular expression (RE) and print out of the line, a comprehensive search for regular expressions and print out rows) is a powerful text search tool that uses regular expressions to search for text. and print out the matching rows. The grep family of Unix includes grep, Egrep, and Fgrep. Egrep and Fgrep commands are only a small difference from grep. Egrep is an extension of grep that supports more re metacharacters, Fgrep is fixed grep or fast grep, which regards all letters as words, that is, the metacharacters in the regular expression returns to its own literal meaning and is no longer special. Linux uses the GNU version of grep. It is more powerful and can use the EGREP and FGREP functions through the-G,-E,-F command-line Options.
grep works like this by searching for a string template in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as file names. The results of the search are sent to the screen without affecting the contents of the original file.
grep is available for shell scripts because grep indicates the state of the search by returning a status value, returns 0 if the template search succeeds, or 1 if the search is unsuccessful, and returns 2 if the searched file does not exist. We can use these return values to do some automated text processing work.
"Special attention"
1,grep text Search with a regular match
2,grep Search Suggestions:
String recommendations use double quotes: grep "Zoo_info" Zookeeper.log
A regular match should use single quotes: grep "\<zoo_info\>" Zookeeper.log
Usage: grep [options] pattern [FILE ...]
2 grep regular expression meta-character set (base set)
^ Anchor Line start like: ' ^grep ' matches all rows beginning with grep.
$ Anchor row end like: ' grep$ ' matches all rows with grep ending.
. matches a newline character Furu: ' GR.P ' matches for example grep.
* Match 0 or more previous characters Furu: ' *grep ' matches all one or more spaces immediately following the grep row ... * To use on behalf of any character.
[] matches a specified range of characters, such as ' [Gg]rep ' matches grep and grep.
[^] matches a character that is not in the specified range, such as: ' [^a-fh-z]rep ' match does not contain a letter beginning with a-f and H-z, followed by the rep line.
\(.. \) tag matching characters, such as ' \ (love\) ', Love is marked as 1.
\< anchors the beginning of a word, such as: ' \<grep ' matches a line containing a word that starts with grep.
\> anchors the end of a word, such as ' grep\> ' to match a line containing a word that ends with grep.
x\{m\} repeats characters x,m times, such as: ' O\{5\} ' matches rows containing 5 O.
x\{m,\} repeats the character x, at least m times, such as: ' O\{5,\} ' to match at least 5 o rows.
x\{m,n\} repeats the character x, at least m times, not more than n times, such as: ' O\{5,10\} ' matches a row of 5--10 O.
\w matches literal and numeric characters, that is, [a-za-z0-9], such as: ' G\w*p ' matches with G followed by 0 or more text or number characters, followed by P.
\w \w, which matches one or more non word characters, such as the dot number period.
\b Word locks, such as: ' \bgrep\b ' matches grep only.
3. Meta-character extension set for Egrep and Grep-e
+ matches one or more of the previous characters. such as: ' [a-z]+able ', match one or more lowercase letters followed by the able string, such as loveable,enable,disable.
? Matches 0 or more previous characters. For example: ' Gr?p ' matches a gr followed by one or no characters, then a line of P.
a|b|c matches A or B or C. such as: grep|sed match grep or sed
() grouping symbols, such as: Love (able|rs) ov+ match loveable or lovers, matching one or more ov.
X{m},x{m,},x{m,n} function with x\{m\},x\{m,\},x\{m,n\}
4. POSIX character classPOSIX (The Portable operating System Interface) adds special character classes, such as [: Alnum:], to keep one to the character encodings in different countries. To put them inside the [] number, you can become regular expressions, such as [a-za-z0-9] or [[: Alnum:]]. Under Linux, grep supports the POSIX character classes except Fgrep.
[: Alnum:] literal numeric characters
[: Alpha:] literal characters
[:d igit:] numeric characters
[: graph:] non-null characters (not spaces, control characters)
[: Lower:] lowercase characters
[: Cntrl:] control characters
[:p rint:] non-null (including spaces)
[:p unct:] punctuation [
: space:] all white space characters (new lines, spaces, tabs)
[: Upper:] Uppercase characters
[: xdigit:] hexadecimal digits (0-9,a-f,a-f)
5. grep command Options
-?
Displays both the top and bottom of the matching row. Rows, such as: grep-2 pattern filename Displays the top and bottom 2 rows of matching rows at the same time.
-b,--byte-offset
print a matching line before printing the block number where the line is located. -
C,--count
prints only the number of rows that match, and does not display the matching content. -
F file,--file=file
extracts the template from the file. The empty file contains 0 templates, so nothing matches.
-h,--no-filename
the matching filename prefix is not displayed when searching for multiple files.
-i,--ignore-case
ignores case differences.
-q,--quiet
Suppresses display and returns only the exit status. 0 indicates that a matching row was found.
-l,--files-with-matches
Print a list of files that match the template.
-l,--files-without-match
Print a list of files that do not match the template.
-n,--line-number
Prints the line number before the matching line.
-s,--silent
does not display error messages about the absence or inability to read files.
-v,--revert-match
Reverse retrieval, showing only unmatched rows.
-w,--word-regexp
Similar to the \< and \> references, the expression is used as a word search. Matches only the whole word, not part of the string (such as matching ' magic ', not ' magical ').
-v,--version
Displays software version information.
-r,-r,--recursive
Recursive lookup
6. Examples
To use a good grep this tool, in fact, is to write a good regular expression, so here is not the grep all the features of the example to explain, only a few examples, explain a regular expression of the wording. $ ls-l | grep ' ^d '
Filters the contents of the LS-L output through a pipe, showing only the rows that start with a.
$ grep ' test ' d*
Displays the rows that contain test in all files that start with D.
$ grep ' test ' AA bb cc
Displays the row that matches test in the aa,bb,cc file.
$ grep ' [a-z]\{5\} ' AA
Displays all the lines that contain at least 5 consecutive lowercase characters for each string.
$ grep ' w\ (es\) t.*\1 ' AA
If West is matched, es is stored in memory and labeled 1, then searches for any character (. *), followed by another ES (\1), and the line is displayed. If you use Egrep or GREP-E, you do not use the "\" to escape, directly written ' W (es) t.*\1 ' on it.
7. Practice grep Command
Demo_file
This are the 1ST UPPER case line in this FILE.
This are the 1st lower case line in this file.
This line has all it-the Word with Upper case.
Two lines above is empty.
And this is the last line.
7.1. The basic use of searching for the specified string grep from a single file is to search for a specific string from the specified file in the following example.
Grammar:
grep "literal_string" filename
$ grep ' This ' demo_file the ' This ' is the ' 1st lower case line in this
file.
Two lines above is empty.
And this is the last line.
7.2. Retrieves the specified string syntax in multiple files :
grep "string" File_pattern
First copy demo_file for Demo_file1. The results of grep include the file name before qualifying rows. When the filename contains a meta character, the Linux shell takes all matching files as input into grep.
$ CP demo_file demo_file1
$ grep ' This ' demo_* demo_file:this line are the 1st lower case line in this
file.
Demo_file:two lines above is empty.
Demo_file:and This is the last line.
Demo_file1:this Line are the 1st lower case line in this file.
Demo_file1:two lines above is empty.
Demo_file1:and This is the last line.
7.3 Output specific matches with Grep-o only:
Grep-o "lsyun_elapsed:[0-9]*"./lsyun_time.log | Awk-f: ' {print $} ' | Sort | Uniq-c
7.4. Case-insensitive search syntax with grep-i :
Grep-i "string" FILE
is also a basic usage, ignoring the case of the search string, so the following example matches "the", "the" and "the".
$ grep-i "the" "Demo_file" the "the" is the ' 1ST UPPER case line in this
file.
This are the 1st lower case line in this file.
This line has all it-the Word with Upper case.
And this is the last line.
7.4. Use regular ExpressionsGrammar:
grep "REGEX" filename
This is a useful feature if you can use regular expressions effectively. In the following example, search for a string that ends with "lines" as "empty", such as searching for any word]empty between lines[, and ignores case.
$ grep-i "Lines.*empty" Demo_file two lines above this line is
empty.
7.5. Search the whole word with grep-w, not part of the word stringUse the-W option to search for a word and avoid searching for a partial string in the word.
The following example searches for "is". If the-w option is not added, all rows containing "is", "he", "this" are displayed.
$ Grep-i ' is ' demo_file this-is the
1ST UPPER-Case-in-this file.
This are the 1st lower case line in this file.
This line has all it-the Word with Upper case.
Two lines above is empty.
And this is the last line.
The following example uses the-W option, and note that the result does not contain "this line has" the Word "and Upper case", although "this" contains "is".
$ Grep-iw ' is ' demo_file this-is the
1ST UPPER-Case-in-this file.
This are the 1st lower case line in this file.
Two lines above is empty.
And this is the last line.
7.6. Use grep-a, B-and-c to display a few lines before, after, before and afterWhen using grep to search for large files, it is a useful feature to display multiple rows of data near a matching row.
7.6.1 shows n rows after matching rows
-A
Grammar:
Grep-a "string" FILENAME
The following example shows 3 rows of data that match rows and
$ grep-a 3-i "Example" Demo_text
7.6.2 Show n rows before matching rows
-B
Grammar:
Grep-b "string" FILENAME
The following example shows matching rows and the previous 2 rows of data
$ grep-b 2 "single WORD" Demo_text
7.6.3 shows n rows before and after matching rows
-C shows the previous n rows, followed by n rows of data.
$ grep-c 2 "Example" Demo_text
7.7. Highlight the search string by Grep_optionsIf you want the search string to be highlighted in the results, try the following methods. Highlight the search string by modifying grep_options.
$ export grep_options= '--color=auto ' grep_color= ' 100;8 '
7.8. Search all files recursively with Grep-rYou can use the-r option if you want to find all the files for the current and its subdirectories. The following example
$ GREP-R "Ramesh" *
Note that the * is OK here, but you cannot make a regular match to the file:
$ grep-r "Ramesh" *.C error ...
The right way to do this:
$ find. -name *.C | Xargs grep-r "Ramesh"
or
$ grep-r "Ramesh" *--include= "*.C"
7.9. Use Grep-v to do a mismatch you can use the-V option to display rows that do not match the search string.
The following example shows rows in the Demo_text file that do not contain "go"
$ grep-v "Go" demo_text
7.10. Display line syntax that does not match all patterns :
Grep-v-E "pattern"-e "pattern"
Create the following example file Test-file.txt
A
b
c
D
$ grep-v-E "a"-E "B"-E "C" Test-file.txt
D
7.11. Match the number of rows with GREP-C statisticsGrammar:
Grep-c "pattern" filename
$ grep-c "Go" demo_text
6
Count the number of rows that do not match
$ grep-v-C-Demo_file
4
7.12. display file name only with Grep-l
$ grep-l This demo_*
demo_file
demo_file1
7.13. Display only matching stringsThe default display row of matching strings, you can use the-o option to display only matching strings. This feature is useful when using regular expressions.
$ grep-o "Is.*line" Demo_file is line is the 1st lower case line is the
7.14. Show matching locationGrammar:
Grep-o-B "pattern" file
$ cat Temp-file.txt
12345
12345
$ grep-o-B "3" Temp-file.txt
0:3
6:3
Note: The above output does not show the position within the row, but the byte byte position in the entire file
7.15. Display line number in output with Grep-nLine number starting from 1
$ grep-n "Go" demo_text