• 【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 中国大陆”许可协议授权。


    分享到:

  • 相关阅读:
    在vue项目中使用stylus来实现移动端的1px
    Promise对象和回调函数,解决异步数据传递问题
    axios在实际项目中的使用介绍
    关于React.PropTypes的废除,以及新版本下的react的验证方式
    javascript之日期对象
    js Date 日期格式化(转)
    jquery无缝隙连续滚动代码
    8款惊艳的HTML5粒子动画特效
    web前端设计师们常用的jQuery特效插件汇总
    js/jQuery实现类似百度搜索功能
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2807113.html
Copyright © 2020-2023  润新知