linux grep command

As one of the three most commonly used text (awk, sed, grep) processing tools in linux, it is necessary to master its usage.

First talk about the common format of the grep command: grep [options] "mode" [file]

There are three grep families in total: grep, egrep, fgrep.

Common options:

  -E : Regular expression to enable extension (Extend).

  -i : ignore case.

  -v : Invert (invert), print only those that do not match, and do not print those that match.

  -n : display line numbers

  -w : The matched text can only be a word, not a part of the word. For example, if there is a liker in the text, and I only search for likes, you can use the -w option to avoid matching liker

  -c : Show how many lines are matched in total, instead of showing what was matched. Note that if you use the -cv option at the same time, it shows how many lines are not matched.

  -o : Display only the strings matched by the pattern.

  --color : Highlight the matched content in color.

  -A n: Display the line where the matched string is located and the next n lines, after

  -B n: Display the line where the matched string is located and its first n lines, before

  -C n: Display the line where the matched string is located and n lines before and after it, context

   

   

 

 

Mode part:

  1. Directly enter the string to be matched. This can be replaced by fgrep (fast grep) to improve the search speed. For example, I want to match the number of printfs in the hello.c file: grep -c "printf" hello.c

  2. Use basic regular expressions . Let's talk about the use of basic regular expressions:

    Match characters:

      . : Any character.

      [abc] : means to match a character, this character must be one of abc.

      [a-zA-Z]: Indicates to match a character, which must be one of the 52 letters of az or AZ.

      [^123] : Match a character that is all characters except 1, 2, 3.

      对于一些常用的字符集,系统做了定义:

      [A-Za-z] 等价于 [[:alpha:]]

      [0-9] 等价于 [[:digit:]]

      [A-Za-z0-9] 等价于 [[:alnum:]]

      tab,space 等空白字符 [[:space:]]

      [A-Z] 等价于 [[:upper:]]

      [a-z] 等价于 [[:lower:]]

      标点符号 [[:punct:]]

      

 

 

    匹配次数:

      \{m,n\} :匹配其前面出现的字符至少m次,至多n次。
      \? :匹配其前面出现的内容0次或1次,等价于\{0,1\}。
      * :匹配其前面出现的内容任意次,等价于\{0,\},所以 ".*" 表述任意字符任意次,即无论什么内容全部匹配。

      

 

 

 

    位置锚定:

      ^ :锚定行首

      $ :锚定行尾。技巧:"^$"用于匹配空白行。

      \b或\<:锚定单词的词首。如"\blike"不会匹配alike,但是会匹配liker

      \b或\>:锚定单词的词尾。如"\blike\b"不会匹配alike和liker,只会匹配like

      \B :与\b作用相反。

      

      

 

 

    分组及引用:

      \(string\) :将string作为一个整体方便后面引用

        \1 :引用第1个左括号及其对应的右括号所匹配的内容。

        \2 :引用第2个左括号及其对应的右括号所匹配的内容。

        \n :引用第n个左括号及其对应的右括号所匹配的内容。

        

 

 

  3、扩展的(Extend)正则表达式(注意要使用扩展的正则表达式要加-E选项,或者直接使用egrep):

    匹配字符:这部分和基本正则表达式一样

 

    匹配次数

      * :和基本正则表达式一样

      ? :基本正则表达式是\?,二这里没有\。

      {m,n} :相比基本正则表达式也是没有了\。

      + :匹配其前面的字符至少一次,相当于{1,}。

 

    位置锚定:和基本正则表达式一样。

 

    分组及引用

      (string) :相比基本正则表达式也是没有了\。

        \1 :引用部分和基本正则表达式一样。

        \n :引用部分和基本正则表达式一样。

 

    或者

      a|b :匹配a或b,注意a是指 | 的左边的整体,b也同理。比如 C|cat 表示的是 C或cat,而不是Cat或cat,如果要表示Cat或cat,则应该写为 (C|c)at 。记住(string)除了用于引用还用于分组。

     

注1:默认情况下,正则表达式的匹配工作在贪婪模式下,也就是说它会尽可能长地去匹配,比如某一行有字符串 abacb,如果搜索内容为 "a.*b" 那么会直接匹配 abacb这个串,而不会只匹配ab或acb。

注2:所有的正则字符,如 [ 、* 、( 等,若要搜索 * ,而不是想把 * 解释为重复先前字符任意次,可以使用 \* 来转义。

 

下面用一个练习来结束本次grep的学习:

在网络配置文件 /etc/sysconfig/network-scripts/ifcfg-ens33 中检索出所有的 IP

1、检索出 0-255的范围

2、由0-255的数字组合成IP

3、简化

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325549066&siteId=291194637