grep command Introduction
grep is a powerful Text Search tool command that finds strings in a file that match a specified format and supports regular expressions. If you do not specify any file name, or if the file name is - , the GERP command reads the data from the standard input device.
grepFamily includes grep egrep and fgrep . The Egrep and Fgrep commands are only a small difference from grep. Egrep is the extension of grep, Fgrep is the fixed grep or fast grep, which uses the metacharacters in any regular expression to denote its own literal meaning, not special. Where Egrep is equivalent to "grep-e", Fgrep is equivalent to "grep-f". (A little dizzy.) grep in Linux is powerful and supports many parameters that can be easily used for text processing.
grep command Common parameter description
-a 不要忽略二进制数据-A 除了显示符合条件的那一行之外,并显示该列之后的内容-b 在显示符合范本样式的那一列之前,标示出该列第一个字符的位编号-B 除了显示符合条件的那一行之外,并显示该列之前的内容-c 计算符合结果的行数-C 除了显示符合条件的那一行之外,并显示该列之前后的内容-e 按指定的字符串查找-E 按指定的字符串指定的正则查找-f 指定范本文件,其内容含有一个或多范本样式 -F 将范本样式视为固定的字符串列表-G 将范本样式视为普通的表示法来用-h 在显示符合范本样式的那一列之前,不标示该列所属的文件名称-H 在显示符合范本样式的那一列之前,标示该列所属的文件名称-i 忽略字符大小写-l 列出文件内容符合指定的范本样式的文件名称-L 列出文件内容不符合指定的范本央视的文件名称-n 在显示符合范本样式的那一列之前,标示出该列的列数编号-q 不显示任何信息-r 在指定路径中递归查找-s 不显示错误信息-v 反向查找-V 显示版本信息-w 匹配整个单词-x 只显示全列符合的列--help 在线帮助
When grep is used alone, there are at least two parameters, such as less than two parameters, and grep waits until the program is interrupted. If such a situation is encountered, termination can be used Ctrl + c . By default, only the current directory is searched, and if you recursively find subdirectories, you can use -r options.
How to use the grep command
Finds a string in the specified file:
grep root /etc/passwd 在 /etc/passwd 中查找 root 字符串
Used in conjunction with pipelines:
cat /etc/passwd | grep rootgrep mysql my.cnf | grep datadir
Find the line number where the eligible content is located:
grep -n root /etc/passed
In nginx.conf, look for the line number that contains the listen and print it out:
grep listen nginx.conf
To find the specified string:
grep uuid test.txt 区分大小写grep UUID test.txt 区分大小写grep -i uuid test.txt 不区分大小写
List the file name of the matching string:
grep -l uuid test.txt
List filenames that do not match the string:
grep -L uuid test.txt
Match the entire word:
gerp -w UU test.txtgrep -w UUID test.txt
In addition to displaying the matching rows, display the n rows of the row context, respectively:
grep -C1 UUID test.txt 此处 n = 1 ,跟在 -C 参数后面
Finds the specified string by regular expression:
grep -n -E "^[a-z]+" test.txtgrep -n -E "^[^a-z]+" test.txtgrep -E "datadir | socket" my.cnf
Recursive lookup:
grep -r var . | head -3
grep Regular Parameter description
^ 指定匹配字符串的行首$ 指定匹配字符串的结尾* 表示0个以上的字符 + 表示1个以上的字符\ 去掉指定字符的特殊含义^ 指定行的开始$ 指定行的结束. 匹配一个非换行的字符* 匹配零个或多个先前字符[] 匹配一个指定范围内的字符[^] 匹配一个不在指定范围内的字符\(..\) 标记匹配字符< 指定单词的开始> 指定单词的结束x{m} 重复字符 x,m 次x{m}, 重复字符 x, 至少 m 次x{m,n} 重复字符x, 至少 m 次,不多于 n 次w 匹配文字和数字字符,也就是 [A-Za-z0-9]b 单词锁定符+ 匹配一个或多个先前的字符? 匹配零个或多个先前的字符a|b|c 匹配 a 或 b 或 c() 分组符号 [:alnum:] 文字数字字符[:alpha:] 文字字符[:digit:] 数字字符[:graph:] 非空格、控制字符[:lower:] 小写字符[:cntrl:] 控制字符[:print:] 非空字符(包括空格)[:punct:] 标点符号[:space:] 所有空白字符(新行,空格,制表符)[:upper:] 大写字符[:xdigit:] 十六进制数(0-9,a-f,A-F)
Linux commands (eight) filter text grep