http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
First create the demo file Demo_file we need to use to practice the grep command.
$ cat Demo_file
This was the 1ST UPPER case.
This was the 1st lower case.
This is the Word with Upper case in its first Character.
The lines above is empty.
The last line.
1. Search for a specified string from a single file
The underlying usage of grep is to search for a specific string from the specified file, as in the following example.
Grammar:
grep "literal_string" filename
$ grep "This" demo_file
This was the 1st lower case.
The lines above is empty.
The last line.
2. Retrieving the specified string in multiple files
Grammar:
grep "string" File_pattern
Copy Demo_file to Demo_file1 first. The result of grep will include the file name before qualifying rows. When the file name contains metacharacters, the Linux shell takes all matching files as input into grep.
$ CP demo_file Demo_file1
$ grep "This" demo_*
Demo_file:this Line was the 1st lower case line in this file.
Demo_file:two lines above is empty.
Demo_file:and.
Demo_file1:this Line was the 1st lower case line in this file.
Demo_file1:two lines above is empty.
Demo_file1:and.
3. Use grep-i for case-insensitive searches
Grammar:
Grep-i "string" FILE
is also a basic usage, ignoring the capitalization of the searched string, so the following example matches "the", "the" and "the".
$ grep-i "the" Demo_file
This was the 1ST UPPER case.
This was the 1st lower case.
This is the Word with Upper case in its first Character.
The last line.
4. Using regular expressions
Grammar:
grep "REGEX" filename
This is a useful feature if you can effectively use regular expressions. In the following example, searches all strings that begin with "empty" at "lines", such as searching for "any word]empty between lines[" and ignoring case.
$ grep-i "Lines.*empty" Demo_file
The lines above is empty.
Several repetitive actions followed by regular expressions
- ? Match at most once
- * Match 0 or more times
- + Match more than one time
- {n} matches n times
- {N,} matches at least n times
- {, m} matches up to M times
- {N,m} matches n to M times
5. Use Grep-w to search the whole word, not part of the word string
Use the-W option to search for a word and avoid searching for partial strings in the word.
The following example searches for "is". If you do not add the-w option, all rows containing "is" are displayed with "is", "he", "this", and so on.
$ grep-i "is" demo_file
This was the 1ST UPPER case.
This was the 1st lower case.
This is the Word with Upper case in its first Character.
The lines above is empty.
The last line.
The following example uses the-W option, note that the result does not contain "this line has all it first Character of the Word with Upper case", although "this" contains "is".
$ GREP-IW "is" demo_file
is The 1ST UPPER case line in this FILE.
is the 1st lower case line in this file.
The lines above is empty.
The last line.
6. Use Grep-a,-B and-c to display a few lines before, after, and after
When using grep to search for large files, it is a useful feature to display multiple rows of data near matching rows.
Create the following file
$ cat Demo_text
4. Vim Word Navigation
Want to does several navigation in relation to the words, such as:
* E-go to the end of the current word.
* E-go to the end of the current WORD.
* B-go to the previous (before) word.
* B-go to the previous (before) WORD.
* W-go to the next word.
* W-go to the next WORD.
Word-word consists of a sequence of non-blank characters, separated with white space.
Word-word consists of a sequence of letters, digits and underscores.
Example to show the difference between word and word
* 192.168.1.1-single WORD
* 192.168.1.1-seven words.
6.1 Show n rows after matching rows
-A
Grammar:
Grep-a "string" FILENAME
The following example shows the matching rows and the following 3 rows of data
$ grep-a 3-i "Example" Demo_text
Example to show the difference between word and word
* 192.168.1.1-single WORD
* 192.168.1.1-seven words.
6.2 Show n rows before matching rows
-B
Grammar:
Grep-b "string" FILENAME
The following example shows the matching row and the previous 2 rows of data
$ grep-b 2 "single WORD" Demo_text
Example to show the difference between word and word
* 192.168.1.1-single WORD
6.3 Show n rows before and after matching rows
-C Displays the previous n rows, followed by the n rows of data.
$ grep-c 2 "Example" Demo_text
Word-word consists of a sequence of letters, digits and underscores.
Example to show the difference between word and word
* 192.168.1.1-single WORD
7. Highlight the searched string by grep_options
If you want the search string to be highlighted in the results, you can try the following methods.
Highlight the search string by modifying the grep_options.
$ export grep_options= '--color=auto ' grep_color= ' 100;8 '
$ grep this Demo_file
This was the 1st lower case.
this is empty.
the last line.
8. Search all files recursively with Grep-r
You can use the-r option if you want to find all the files currently in the current and its subdirectories. The following example
$ GREP-R "Ramesh" *
9. Do not match using grep-v
You can use the-V option to display rows that do not match the search string. The following example shows a line in the Demo_text file that does not contain "go"
$ grep-v "Go" demo_text
4. Vim Word Navigation
Want to does several navigation in relation to the words, such as:
Word-word consists of a sequence of non-blank characters, separated with white space.
Word-word consists of a sequence of letters, digits and underscores.
Example to show the difference between word and word
* 192.168.1.1-single WORD
* 192.168.1.1-seven words.
10. Show rows that do not match all modes
Grammar:
Grep-v-E "pattern"-e "pattern"
Create the following example file
$ cat Test-file.txt
A
B
C
D
$ grep-v-E "a"-E "B"-E "C" test-file.txt
D
11. Number of rows matched with grep-c statistics
Grammar:
Grep-c "pattern" filename
$ grep-c "Go" demo_text
6
Count rows that do not match
$ grep-v-C this demo_file
4
12. Use Grep-l to display only file names
$ grep-l This demo_*
Demo_file
Demo_file1
13. Only matching strings are displayed
The line that matches the string is displayed by default, and 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 was the 1st lower case line
Is line
Is was the last line
14. Show matching Locations
Grammar:
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 shows not the position within the line, but the byte position in the entire file
15. Use Grep-n to display line numbers at output
Line number starting from 1
$ grep-n "Go" demo_text
5: * E-go to the end of the current word.
6: * E-go to the end of the current WORD.
7: * B-go to the previous (before) word.
8: * B-go to the previous (before) WORD.
9: * W-go to the next word.
Ten: * W-go to the next WORD.
Source: http://www.cnblogs.com/xuxm2007/archive/2011/01/10/1932288.html
The grep of Linux