The grep command linux

First introduced to the grep command:

grep (global search regular expression (RE) and print out the line, comprehensive search regular expression and print out the line) is a powerful text search tool, you can use a regular expression search text, and print the matching lines come out. It is "one of the text-processing tools" linux most commonly used, and sed, awk linux collectively known as the "Three Musketeers."

Options:

  • -a or --text  : Do not ignore the binary data.
  • -A <the number of lines> or --after-context = <number of lines displayed>  : In addition to displaying the template in line with the row outside the pattern, and then display the contents of the row.
  • -b-offset or --byte  : Prior to demonstrate compliance with the line style, mark the number of the first character of the line.
  • -B <the number of lines> or --before-context = <number of lines displayed>  : In addition to displaying the line patterns meet outside, and displays the contents of the previous row.
  • -c or --count  : calculated in line with the number of columns style.
  • -C <the number of lines> or --context = <number of lines displayed> or - <the number of lines>  : In addition to displaying the line style than meet, and the display contents before the row.
  • -d <action> or --directories = <action>  : When you specify when you want to find is a directory rather than a file, you must use this parameter, otherwise the grep command will return information and stop action.
  • -e <style templates> or --regexp = <template pattern>  : find files specified string as the contents of the style.
  • Or -E-regexp --extended  : the style of expression is positive extended use.
  • -f <rule file> or --file = <rule file>  : specify rules file, the contents of which contain one or more regular pattern, so grep to find content file rule conditions are met, the format for each line of a regular pattern.
  • -F regexp-or --fixed  : a style seen as a list of fixed strings.
  • -G-regexp or --basic  : the style of notation as ordinary use.
  • -h-filename or --no  : Prior to demonstrate compliance with the line style, does not indicate the file name of the row belongs.
  • -H or-filename --with  : Prior to demonstrate compliance with the line style, it represents a file name of the row belongs.
  • Case--i or --ignore  : ignore character case differences.
  • -l-with-The matches or --file  : lists the contents of the file file name matches the specified style.
  • -L-the without-match or --files  : lists the contents of the file file name does not comply with the specified style.
  • Number The -n-or --line  : Prior to demonstrate compliance with the line style, mark the number of columns in the row number.
  • -o-matching or --only  : PATTERN only matching portion.
  • -q or --quiet or --silent  : do not display any information.
  • --recursive or -r  : effect of this parameter is specified and "-d recurse" the same parameters.
  • -s-messages or --no  : no error message.
  • -v-match or --revert  : Displays all rows matching text.
  • -V or --version  : show the version information.
  • -w-regexp or --word  : show only matching whole-word column.
  • --line-regexp -x  : display only full compliance with columns columns.
  • -Y  : effect of this parameter is specified and "-i" the same parameters.

Example:

For convenience, we prepared a test file test.txt, which reads as follows

 If we want to search for the line containing the string hello, you can use the following command:

 The above figure represents the command to use the grep command to search in the test.txt file contains the line "hello" string, and print out the line that contains the string hello.

 By default, grep is case-sensitive, if it is a case-insensitive search out the contents were looking -i can add parameters, as follows:

 Because it is used for experiments, so the content of the test file written in relatively small, in general, a file contents ranging from a few dozen lines in routine work, as many as hundreds of thousands of lines, and we want to make sure that we want how to search the contents need to do a few lines in this file?

-N plus a parameter can solve the above problem, as shown below:

As shown by -n parameters can be displayed to the number of lines to retrieve the content.

If we want to know how many rows contain what we crawl, and do not care about which rows contain them, we can use the following command to get the number of rows in qualifying.

 

 Sometimes, we need to reverse lookup such as finding "that do not contain a string of" this time "-v" option can solve this problem

 Find out the line of text above statement does not contain a "test" of.

某些场景下,我们需要同时从多个目标中匹配,什么意思呢?来,我们看下示例就知道了

 上例子我们同时检索了包 hello 和 abc字符串,包含这两个字符串中的每一行都被打印出来,没错,就像上面的图例一样,使用“-e”选项可以同时匹配多个目标,多个目标存在“或”关系,即匹配到其中的任意一个都算作匹配成功。

在写脚本时,你可能只是想要利用grep判断文本中是否存在某个字符串,你只关心有没有匹配到,而不关心匹配到的内容,你只关心有,或者没有,这时,我们可以使用grep的静默模式,示例如下。

  当使用"-q"选项时,表示grep使用静默模式,静默模式下grep不会输入任何信息,无论是否匹配到指定的字符串,都不会输出任何信息,所以,我们需要配合"echo $?"命令,查看命令的执行状态,如果返回值为0,证明上一条grep命令匹配到了指定的字符串,如果返回值为1,则证明上一条grep命令没有匹配到指定的字符串,就像上图示例中显示的那样,静默模式下,grep没有输出任何信息,当我们在test.txt文本中查找"test"字符串时,可以匹配到结果,当在文本中查找"1111111111"字符串的时候,没有匹配到结果,所以,我们只关心有没有匹配到指定字符时,可以使用"-q"选项,但是需要配合"echo $?"命令查看执行状态。

grep还有很多其它选项,我只是列举了常用的一些选项,其它选项可以自行练习。

其实,除了grep命令,其实还有egrep命令,还有fgrep命令(fast grep),它们有各自的特点。

grep:支持基本正则表达式

egrep:支持扩展正则表达式,相当于grep -E

fgrep:不支持正则表达式,只能匹配写死的字符串,但是速度奇快,效率高,fastgrep

【参考资料:http://www.zsythink.net/archives/1733

 

Guess you like

Origin www.cnblogs.com/sxFu/p/11777467.html