• Linux.CommanlineTool.grep


    grep

    1 grep [options] PATTERN [FILE...]
    2 grep [options] [-e PATTERN | -f FILE] [FILE...]

     

    1. grep的Regular Expression

    Ref[1]

    A): 匹配单字符

    reg ex中基本的构建块是匹配单个字符的reg ex。大多数字符(包括数字和字母)都是匹配自己的reg ex。

    B): 元字符

    任何有特殊含义的元字符(metacharacter)都是被前导的反斜线(backslash, 即: )开始标识。

    C): bracket expression

    bracket expression是由[]包围的字符列表。这类表达式匹配列表中的任何一个字符(注意是 只匹配一个字符)。

    如果^是字符列表中的第一个字符,则该表达式匹配任意一个不在列表中的字符。

    例如:[0123456789] 匹配任意一个单一数字; [a-z] 匹配任意一个小写字母。

    要匹配字符']', 则需要将']'放在字符列表的第一个位置。例如: []a-z]

    类似的要匹配'^'需要将'^'放在字符列表中非第一的位置。例如: [a^]

    类似的要匹配'-'需要将'-'放在字符列表中的最后的位置。 例如: [a-]

    D): 命名的字符类别 (named classes of characters)

    [:alnum:]  [:alpha:] [:cntrl:] [:digit:] [:graph:] [:lower:] [:print:] [:punct:] [:space:] [:upper:] [:xdigit:]

    E): '.' 圆点匹配任意一个单一字符。 w 和 [[:alnum:]] 同义, W 和 [^[:alnum:]] 同义。

    F): ^ 和 $ 分别匹配一行的开始和结尾的空字符串。例如:

    $ grep ^line1 data 

    匹配文件data中以"line1"开头的行。

    G): < > 

    The symbols < and > respectively match the empty string at the beginning and end of a word.

    H):  B

    The symbol  matches the empty string at the edge of a word, and B matches the empty string

    provided it's not at the edge of a word.

    I): 重复操作符 (repetition operator)

    repetition operator
    ?

    The preceding item is optional and matched at most once.

    *

    The preceding item will be matched zero or more times.

    +

    The preceding item will be matched one or more times.

    {n}

    The preceding item is matched exactly n times.

    {n,}

    The preceding item is matched n or more times.

    {n,m}

    The preceding item is matched at least n times, but not more than m times.

    J): 匹配元字符 *

    ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions ?, +, {, |, (, and )

    K): reg ex的操作: concatenation, join

    Two regular expressions may be concatenated; the resulting regular expression matches any string formed

    by concatenating two substrings that respectively match the concatenated subexpressions.

    Two regular expressions may be joined by the infix operator |; the resulting regular expression matches

    any string matching either subex- pression.

    Repetition takes precedence over concatenation, which in turn takes precedence over alternation.

    A whole subexpression may be enclosed in parentheses to override these precedence rules.

    The backreference , where n is a single digit, matches the substring previously matched by the nth parenthesized

    subexpression of the regu- lar expression.

    例如: $ grep "line1|line2" data

    匹配包含"line1"或者"line2"的行。

    2. grep的Example

    [Todo]: Ref[2] 


     

    Reference

    1. grep

    http://unixhelp.ed.ac.uk/CGI/man-cgi?grep

    2. Advanced Regular Expressions in Grep Command with 10 Examples – Part II

    http://www.thegeekstuff.com/2011/01/advanced-regular-expressions-in-grep-command-with-10-examples-%E2%80%93-part-ii/

  • 相关阅读:
    函数模板
    c#使用多线程的几种方式示例详解
    C#中数组、ArrayList和List三者的区别
    c# Thread类
    IEnumerable和IEnumerator 详解
    C#执行CMD命令并接收返回结果的实现方法
    C# Process.Start()方法详解
    C#Json转Xml格式数据的方法
    sql存储过程
    SQL存储过程基础
  • 原文地址:https://www.cnblogs.com/cwgk/p/4616392.html
Copyright © 2020-2023  润新知