Parameters :
- -A or--text : Do not omit binary data.
- -a< Show rows > or--after-context=< display rows > : In addition to displaying the column that conforms to the template style, the contents after that line are displayed.
- - B or--byte-offset : Indicates the number of the first character of the line before displaying the line that matches the style.
- -b< Show rows > or--before-context=< display rows > : In addition to displaying the line that matches the style, and displaying the contents before the line.
- - C or--count : Calculates the number of columns that conform to the style.
- -c< Show rows > or--context=< show rows > or-< show rows > : In addition to displaying the line that conforms to the style, the contents before the row are displayed.
- - D < action > or--directories=< action > : This parameter must be used when specifying that a directory is to be found instead of a file, otherwise the GREP directive returns information and stops the action.
- -e< Template Style > or--regexp=< template style > : Specifies a string as a style for finding the contents of a file.
- - E or--extended-regexp : Use the style as an extended normal notation.
- -f< rule File > or--file=< rule file > : Specifies a rule file whose contents contain one or more rule styles that let grep find the contents of the file that match the rule condition, in the form of a rule style per line.
- - F or--fixed-regexp : treats the style as a list of fixed strings.
- - G or--basic-regexp : Use the style as a normal notation.
- - H or--no-filename : does not indicate the name of the file to which the line belongs until it displays the line that matches the style.
- - H or--with-filename : Indicates the name of the file to which the line belongs before it displays the line that matches the style.
- -I or--ignore-case : ignores character case differences.
- - L or--file-with-matches : Lists the file names of the file contents that match the specified style.
- - L or--files-without-match : Lists file names for file contents that do not conform to the specified style.
- - N or--line-number : Indicates the number of columns in the row before displaying the line that matches the style.
- - Q or--quiet or--silent : No information is displayed.
- - R or--recursive : The effect of this parameter is the same as specifying the "-D recurse" parameter.
- - s or--no-messages : No error message is displayed.
- - V or--revert-match : Displays all lines that do not contain matching text.
- - V or--version : Displays version information.
- - W or--word-regexp : Displays only the columns that match the whole word.
- - x--line-regexp : Displays only columns that are compliant with all columns.
- - y : The effect of this parameter is the same as specifying the "-i" parameter.
1. grep's simplest usage, match one word: grep word filename
2. Ability to match from multiple files: grep word filename1 filenam2 filename3
3. The ability to use the regular table to match: Grep-e pattern F1 F2 f3 ...
4. Ability to use-O to print only the matching characters, as seen below:
[[email protected] net]# echo this was line. |grep-o "[a-z]*.$"
Line.
[[email protected] net]# echo this was line. |grep-o "[A-z]*\.]
Line.
5. Print a line other than the matching line, using-V:
[Email protected] net]# echo-e "1\n2\n3\n4"
1
2
3
4
[Email protected] net]# echo-e "1\n2\n3\n4" |grep-v "[1]"
2
3
4
[Email protected] net]# echo-e "1\n2\n3\n4" |grep-v "[1-2]"
3
4
[Email protected] net]# echo-e "1\N2\N3\N4" |grep-v-E "[1-2]"
3
4
6. Count the number of rows matching the string. Using the-C
[Email protected] net]# echo-e "1\n2\n3\n4" |grep "[+]"-C
2
7. Count the number of string pattern matches. Can be combined with-O. For example, the following:
The previous example does not add the-o parameter
[Email protected] net]# echo-e "11111\222" |grep "[Up]"
11111\222
[Email protected] net]# echo-e "11111\222" |grep-o "[Up]"
1
1
1
1
1
2
2
2
8. Assume that you need to display line numbers to open-N, for example, the following
[Email protected] net]# echo-e "11111\n222" |grep-n-O "[Up]"
1:1
1:1
1:1
1:1
1:1
2:2
2:2
2:2
[Email protected] net]# echo-e "11111\n222" |grep-n "[Up]"
1:11,111
2:222
9. The-B option prints out the offset of the matching string to the starting position of the line where it is located (starting from 0). Usually mates with-o, such as the following:
[Email protected] net]# echo "0123456789" | Grep-b-O 4
4:4
[Email protected] net]# echo "012333456789" | Grep-b-O 4
6:4
10.-p parameter (declares a regular expression to be used after grep)
[Email protected] net]# echo-e "\nline.123\nline." | Grep-p "[a-z]*\.\d+"
Line.123
[Email protected] net]# echo-e "\nline.123\nline." | Grep-p "[A-z]*\.]
Line.123
Line.
[Email Protected]abbix net]# echo-e "\nline.123\nline." | Grep-p "[a-z]*\.$"
Line.
[Email protected] net]# echo-e "\nline.123\nline." | Grep-p "\.$"
Line.
11. Matching multiple string patterns
No-o parameter does not print only matches
[[email protected] ~]# echo "This was a line." | Grep-e "This"-e "is"-e "line"-e "a"
This was a line.
Only matches are printed after you add the-o parameter
[[email protected] ~]# echo "This was a line." | Grep-o-E "This"
This
[[email protected] ~]# echo "This was a line." | Grep-e "This"-e "is"-e "line"-e "a"-O
This
Is
A
Line
================================================================
Parameter op is used together, the number to be matched is printed separately
[Email protected] a]# echo office365 | Grep-op ' \d+ '
365
Only the parameter-P will display a full line of matching content highlighting
[Email protected] a]# echo office365 | Grep-p ' \d+ '
Office365
Only the parameter-O, does not match anything, because grep is not declared to use regular expressions
[Email protected] a]# echo office365 | Grep-o ' \d+ '
[Email protected] a]#
===============================================================
12. Print the matching row context information, and use-A-N to print the matching row and its following n rows of information. Use-b N to print matching rows and their top n rows of information. Use-c N. Prints the matching line and its n-th row information. Suppose there are multiple matches that will be used--isolation.
[[email protected] ~]# SEQ 1 10 | grep 5
5
[[email protected] ~]# SEQ 1 10 | grep 5-a 2
5
6
7
[[email protected] ~]# SEQ 1 10 | grep 5-a 2-b 4
1
2
3
4
5
6
7
[[email protected] ~]# SEQ 1 10 | grep 5-c 2
3
4
5
6
7
[Email protected] ~]# echo-e "A\nb\nc\nd\na\nb\nc\nd" |grep b-a 1
B
C
--
B
C
[Email protected] ~]# echo-e "A\nb\nc\nd\na\nb\nc\nd" |grep b-b 1
A
B
--
A
B
[Email protected] ~]# echo-e "A\nb\nc\nd\na\nb\nc\nd" |grep b-c 1
A
B
C
--
A
B
C
The 13.-Z option will end with/0 when the output matches the file name xargs-0 can play a lot of roles, such as deleting files that match a pattern such as the following:
[[email protected] a]# echo Linux >> a.txt
[[email protected] a]# echo Linux >> b.txt
[Email protected] a]# echo Li >> c.txt
[Email protected] a]# grep-lz "Linux" *|xargs-0 RM
[[email protected] a]# ls
C.txt
The above command will include the a.txt and b.txt deletion of the Linux string.
14. Qualifying Whole Word matching option:-W
[Email protected] a]# Grep-rn "Li" *
A.txt:1:linux
B.txt:1:linux
C.txt:1:li
[Email protected] a]# GREP-RNW "Li" *
C.txt:1:li
The 15.-E parameter matches more than one keyword at a time
[Email protected] a]# cat/etc/passwd|grep-e "Root|long"
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
Long:x:1002:1002::/home/long:/bin/bash
Grep-e with Egrep
[Email protected] a]# cat/etc/passwd|egrep "Root|lilong"
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
Lilong:x:1002:1002::/home/long:/bin/bash
The-e parameter also matches multiple keywords
[Email protected] a]# cat/etc/passwd|grep-e root-e Long
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
Long:x:1002:1002::/home/long:/bin/bash
One more example:
[email protected] a]# cat D.txt
1 5
2 3
3 2
2 3 4
5
6
7
[Email protected] a]# grep-e ' 2|3 ' d.txt
2 3
3 2
2 3 4
[Email protected] a]# egrep ' 2|3 ' d.txt
2 3
3 2
2 3 4
[Email protected] a]# grep-e 2-e 3 d.txt
2 3
3 2
2 3 4
A detailed description of the grep command under Linux