zoukankan      html  css  js  c++  java
  • 【Shall技巧】grep/egrep中的正则使用

    1.grep 's Regular Expression Metacharacters(Which I always dismissed~)
    [^] Matches one character not in the set '[^A–K]ove' Matches lines not containing a character in the range A through K, followed by ove.
    /< Beginning-of-word anchor '/<love' Matches lines containing a word that begins with love.
    /> End-of-word anchor 'love/>' Matches lines containing a word that ends with love.
    /(../) Tags matched characters '/(love/)ing' Tags marked portion in a register to be remembered later as number 1. To reference later, use /1 to repeat the pattern. May use up to nine tags, starting with the first tag at the left-most part of the pattern. For example, the pattern love is saved in register 1 to be referenced later as /1.
    x/{m/} x/{m,/} x/{m,n/} [a] Repetition of character x: m times, at least m times, or between m and n times 'o/{5/}' 'o/{5,/}' 'o/{5,10/}' Matches if line has 5 occurences of o , at least 5 occurences of o , or between 5 and 10 occurrences of o .


    EXPLANATION

    Prints the line if it contains the word north. The /< is the beginning-of-word anchor, and the /> is the end-of-word anchor.

    4.grep 's Options

     
    Option What It Does
    –b Precedes each line by the block number on which it was found. This is sometimes useful in locating disk block numbers by context.
    –c Displays a count of matching lines rather than displaying the lines that match.
    –h Does not display filenames.
    –i Ignores the case of letters in making comparisons (i.e., upper-- and lowercase are considered identical).
    –l Lists only the names of files with matching lines (once), separated by newline characters.
    –n Precedes each line by its relative line number in the file.
    –s Works silently, that is, displays nothing except error messages. This is useful for checking the exit status.
    –v Inverts the search to display only lines that do not match.
    –w Searches for the expression as a word, as if surrounded by /< and />. This applies to grep only. (Not all versions of grep support this feature; e.g., SCO UNIX does not.)


    egrep (Extended grep )

    The main advantage of using egrep is that additional regular expression metacharacters (see Table 3.4 ) have been added to the set provided by grep. The /(/) and /{/}, however, are not allowed. (See GNU grep –E if using Linux.)

    Table 3.4. egrep 's Regular Expression Metacharacters
    Metacharacter Function Example What It Matches
    ^ Beginning-of-line anchor '^love' Matches all lines beginning with love.
    $ End-of-line anchor 'love$' Matches all lines ending with love.
    . Matches one character 'l..e' Matches lines containing an l, followed by two characters, followed by an e.
    * Matches zero or more characters '*love' Matches lines with zero or more spaces of the preceding characters followed by the pattern love.
    [ ] Matches one character in the set '[Ll]ove' Matches lines containing love or Love.
    [^ ] Matches one character not in the set '[^A–KM–Z]ove' Matches lines not containing A through K or M through Z, followed by ove.
    New with egrep:
    + Matches one or more of the preceding characters '[a–z]+ove' Matches one or more lowercase letters, followed by ove. Would find move, approve, love, behoove, etc.
    ? Matches zero or one of the preceding characters 'lo?ve' Matches for an l followed by either one or not any occurrences of the letter o. Would find love or lve.
    a|b Matches either a or b 'love|hate' Matches for either expression, love or hate.
    () Groups characters 'love(able|ly) (ov)+' Matches for lovable or lovely. Matches for one or more occurrences of ov.


                   作者:gnuhpc
                   出处:http://www.cnblogs.com/gnuhpc/
                   除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


    分享到:

  • 相关阅读:
    DRF内置限流组件之自定义限流机制
    DRF内置权限组件之自定义权限管理类
    DRF内置认证组件之自定义认证系统
    java基础(15)--多态
    java基础(13)--静态变量、静态代码块、实例代码块
    java基础(12)--static变量/方法 与 无 static的变量/方法的区别
    java基础(11)--封装
    java基础(10)--空指针异常
    java基础(9)--方法重载
    java基础(8)--键盘输入
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2807113.html
Copyright © 2011-2022 走看看