grep, a powerful text-search tool, is all called(Global search Regular expression (RE) and print out of the line), meaning tofull search of regular expressions and print out the lines。 It can use regular expressions to search for text and print matching lines.
Options
-C calculates the number of times to find the "search string"-o displays only the contents of the match regexp (used to count the number of times in the current article)-I ignores case-N to display line numbers at the beginning of the row-V invert selection, that is, the line-e extension grep that does not contain the "search string" content, that is --color=auto the meaning of the key-a after that matches a particular color, displays the data of the N-row of the matched string machine-B before, displays the matching string and its first n rows of data-C displays the matching string before and after the machine n-line-R recursive search-E Brake multiple matching styles-Q does not display any information. -W displays only the columns that match the whole word.
"File Contents"
[[email protected] test]# cat file "The world was not" can do "but" you Should.3 life was not easy for any of Us.4 We mu St Work hard.123456785 We hope so we can see you soon6
"Examples of common usage"
1. Exclude specified content
[e-mail protected] test]# grep-v We file The world are not a can do, but you Should.3 life is isn't easy for any of us. 5 We hope that we can see you soon6
2. Count the number of times we appear
[[email protected] test]# grep-c We file 1
3. Count the number of occurrences of the word "we", case-insensitive
[[email protected] test]# grep-ci We file 2
4. Output only the matching content
[Email protected] test]# grep-oi life file
Life
5. Show line numbers at the beginning
[Email protected] test]# grep-ni life file
3:3 life isn't easy for any of us.
6. Use the regular expression-e option
[[email protected] test]# grep-e "[1-9]+" File
1
2 The world isn't a can do, but you should.
3 life isn't easy for any of us.
4 We must work hard. 12345678
5 We hope that we can see you soon
6
7. Match multiple styles
Law one: Using Egrep
[Email protected] test]# egrep-i--color=auto "We|life" file
3 life isn't easy for any of us.
4 We must work hard.12345678
5 We hope that we can see you soon
Law II: Using the-e option
[Email protected] test]# Grep-ie "We"-ie "life" file
3 life isn't easy for any of us.
4 We must work hard.12345678
5 We hope that we can see you soon
8.-q Silent Output
[Email protected] test]# grep-q we file; echo $? 0 #不会输出任何信息, if the command runs successfully and returns 0, the failure returns a value other than 0. Typically used for conditional testing.
9. Print a line before or after the matching text
1) Display 3 rows after matching a result, using the-a option
[[email protected] test]# grep "3"-A 3 file
3 life isn't easy for any of us.
4 We must work hard.12345678
5 We hope that we can see you soon
6
2) display 3 rows before matching a result, using the-B option
[[email protected] test]# grep "5"-B 3 File
1
2 The world isn't a can do, but you should.
3 life isn't easy for any of us.
4 We must work hard.12345678
5 We hope that we can see you soon
As you can see, the first line is more in the output, and you can use the-W option to precisely match the characters, outputting only the fifth line and its first three rows, or 2-5 rows.
[[email protected] test]# grep-w "5"-B 3 File
2 The world isn't a can do, but you should.
3 life isn't easy for any of us.
4 We must work hard.12345678
5 We hope that we can see you soon
3) Display the first three rows and the last three rows that match a result, using the-C option
[[email protected] test]# grep-w "4"-C 2 File
2 The world isn't a can do, but you should.
3 life isn't easy for any of us.
4 We must work hard.12345678
5 We hope that we can see you soon
6
grep common parameters and application examples