grep (abbreviated from globally search a Regular expression and print) is a powerful text-search tool that can use regular expressions to search for text and print matching lines. The grep family of Unix includes grep, Egrep, and Fgrep.
grep works in such a way that it searches 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 filenames. The results of the search are sent to standard output without affecting the contents of the original file.
1. Command format:
grep [option] Pattern file
2. Command function:
A specific character used for filtering/searching. The use of regular expressions can be used in conjunction with a variety of commands, the use of very flexible.
3. Command parameters:
-A--text #不要忽略二进制的数据.
-a< Displays rows >--after-context=< displays the number of rows > #除了显示符合范本样式的那一列之外, and displays the contents after that line.
-B--byte-offset #在显示符合样式的那一行之前, indicating the number of the first character of the line.
-b< Displays rows >--before-context=< displays the number of rows > #除了显示符合样式的那一行之外, and displays the contents before the line.
-C--count #计算符合样式的列数.
-c< Display rows >--context=< Displays the number of rows > or-< Displays the number of rows > #除了显示符合样式的那一行之外, and displays the contents before the line.
-D < action >--directories=< action > #当指定要查找的是目录而非文件时, this parameter must be used, otherwise the grep command returns information and stops the action.
-e< template style >--regexp=< template style > #指定字符串做为查找文件内容的样式.
-E--extended-regexp #将样式为延伸的普通表示法来使用.
-f< Rules file >--file=< rule File > #指定规则文件, with one or more rule styles, that grep finds the content of the file that matches the rule condition, in the form of a rule style per line.
-F--fixed-regexp #将样式视为固定字符串的列表.
-G--basic-regexp #将样式视为普通的表示法来使用.
-H--no-filename #在显示符合样式的那一行之前, does not indicate the name of the file to which the line belongs.
-H--with-filename #在显示符合样式的那一行之前 that represents the name of the file to which the row belongs.
-I.--ignore-case #忽略字符大小写的差别.
-L--file-with-matches #列出文件内容符合指定的样式的文件名称.
-L--files-without-match #列出文件内容不符合指定的样式的文件名称.
-N--line-number #在显示符合样式的那一行之前, indicating the number of columns in the row.
-Q--quiet or--silent #不显示任何信息.
-R--recursive #此参数的效果和指定 the "-D recurse" parameter.
-S--no-messages #不显示错误信息.
-V--revert-match #显示不包含匹配文本的所有行.
-V--version #显示版本信息.
-W--word-regexp #只显示全字符合的列.
-X--line-regexp #只显示全列符合的列.
-y #此参数的效果和指定 the same as the "-i" parameter.
4. Usage examples
Case 1: Exact match
Cat 1 grep cat1grep -w"all"~]#
Case 2: Adding automatic colors
Cat 1 grep -w"all" --color=autoall tooallto alltoall All
Case 3: Take the inverse parameter-v option
[Email protected] ~]#PS-ef |grep SSHRoot2055 1 0 the:Geneva?xx:xx:xx/usr/sbin/Sshdroot24498 2055 0 One: +?xx:xx: onsshd: [Email protected]/0Root24657 24502 0 A: Onepts/0 xx:xx:xx grep SSH[[Email protected]~]#PS-ef |grep SSH|grep-VgrepRoot2055 1 0 the:Geneva?xx:xx:xx/usr/sbin/Sshdroot24498 2055 0 One: +?xx:xx: onsshd: [Email protected]/0[[Email protected]~]#
Case 4: Number of occurrences of statistics
grep " All " 1 . txt 4 ~]#
Case 5: Show the number of matching rows
grep " All " 1 . txt 1 : All Tooall 2 : to Alltoall all 3 : allto100 4: Uuualltoal
Case 6: Displaying matching files
[Email protected] ~]#grep " All" 1. txt2. txt4. txt1. Txt:all Tooall1. txt:to Alltoall All1. txt:allto1001. Txt:uuualltoall2. Txt:alltohell2. txt4. Txt:allheot4.txt[[email protected]~]#grep-L" All" 1. txt2. txt4. txt1. txt2. txt4. TX
Case 7: Ignoring character capitalization
Cat 1 grep " All " All tooallalaalllto alltoall allallto100uuualltoall
Case 8: Ignoring character capitalization
Linux security essentials: use of GREP commands