zoukankan      html  css  js  c++  java
  • grep 命令详解

    grep

    grep 工具的工作方式是对文件的每一行搜索给定字符串的首次出现。如果找到了这个字符串,就打印该行的内容;否则就不对该行进行打印。下面这个文件我称之为 “memo”,阐述了 grep 的用法和结果。

    To: All Employees

    From: Human Resources

    In order to better serve the needs of our mass market customers, ABC Publishing is integrating the groups selling to this channel for ABC General Reference and ABC Computer Publishing. This change will allow us to better coordinate our selling and marketing efforts, as well as simplify ABC's relationships with these customers in the areas of customer service, co-op management, and credit and collection. Two national account managers, Ricky Ponting and Greeme Smith, have joined the sales team as a result of these changes.

    To achieve this goal, we have also organized the new mass sales group into three distinct teams reporting to our current sales directors, Stephen Fleming and Boris Baker. I have outlined below the national account managers and their respective accounts in each of the teams. We have also hired two new national account managers and a new sales administrator to complete our account coverage. They include:

    Sachin Tendulkar, who joins us from XYZ Consumer Electronics as a national account manager covering traditional mass merchants.

    Brian Lara, who comes to us via PQR Company and will be responsible for managing our West Coast territory.

    Shane Warne, who will become an account administrator for our warehouse clubs business and joins us from DEF division.

    Effectively, we have seven new faces on board:

    1. RICKY PONTING
    2. GREEME SMITH
    3. STEPHEN FLEMING
    4. BORIS BAKER
    5. SACHIN TENDULKAR
    6. BRIAN LARA
    7. SHANE WARNE

    Please join me in welcoming each of our new team members.
    举一个简单的例子来说,要查找包含 “welcoming” 单词的行,最好的方法是使用下面的命令行:

    # grep welcoming memo
    Please join me in welcoming each of our new team members.
     


    如果您想查找单词 “market”,结果会有很大的不同,如下所示:

    # grep market memo
    In order to better serve the needs of our mass
    market customers, ABC Publishing is
    integrating the groups selling to this channel
    for ABC General Reference and ABC Computer
    Publishing. This change will allow us to
    better coordinate our selling and marketing
    efforts, as well as simplify ABC's
    relationships with these customers in the
    areas of customer service, co-op management,
    and credit and collection. Two national
    account managers, Ricky Ponting and Greeme
    Smith, have joined the sales team as a result
    of these changes.
     


    注意我们找到了两个匹配项:我们希望找的 “market” 和 “marketing”。如果在这个文件中还存在 “marketable” 或 “marketed” 之类的单词,那么上面的命令也会显示包含这些单词的行的内容。

    在 grep 中可以使用通配符和元字符,我强烈建议将它们放到引号中,这样 shell 就不会将它们解释成命令了。

    要查找所有包含数字的行,请使用下面的命令:

    # grep  "[0-9]" memo
    1. RICKY PONTING
    2. GREEME SMITH
    3. STEPHEN FLEMING
    4. BORIS BAKER
    5. SACHIN TENDULKAR
    6. BRIAN LARA
    7. SHANE WARNE
     


    要查找所有包含 “the” 的行,请使用下面的命令:

    # grep the memo
    In order to better serve the needs of our mass
    market customers, ABC Publishing is
    integrating the groups selling to this channel
    for ABC General Reference and ABC Computer
    Publishing. This change will allow us to
    better coordinate our selling and marketing
    efforts, as well as simplify ABC's
    relationships with these customers in the
    areas of customer service, co-op management,
    and credit and collection. Two national
    account managers, Ricky Ponting and Greeme
    Smith, have joined the sales team as a result
    of these changes.

    To achieve this goal, we have also organized
    the new mass sales group into three distinct
    teams reporting to our current sales
    directors, Stephen Flemming and Boris Baker. I
    have outlined below the national account
    managers and their respective accounts in each
    of the teams. We have also hired two new
    national account managers and a new sales
    administrator to complete our account
    coverage. They include:
     


    正如您可能已经注意到的一样,输出结果中包含了单词 “these”,还有单词 “the” 的一些精确匹配。

    grep 工具,与很多其他 UNIX/Linux 工具一样,都是大小写敏感的,这意味着查找 “The” 和 “the” 会产生完全不同的结果。

    # grep The memo
    To achieve this goal, we have also organized
    the new mass sales group into three distinct
    teams reporting to our current sales
    directors, Stephen Flemming and Boris Baker. I
    have outlined below the national account
    managers and their respective accounts in each
    of the teams. We have also hired two new
    national account managers and a new sales
    administrator to complete our account
    coverage. They include:
     


    如果您想查找一个特定的单词或短语,但却不太关心它们的大小写,那可以使用两种方法。第一种方法是使用方括号同时查找 “The” 和 “the”,如下所示:

    # grep "[T, t]he" memo
    In order to better serve the needs of our mass
    market customers, ABC Publishing is
    integrating the groups selling to this channel
    for ABC General Reference and ABC Computer
    Publishing. This change will allow us to
    better coordinate our selling and marketing
    efforts, as well as simplify ABC's
    relationships with these customers in the
    areas of customer service, co-op management,
    and credit and collection. Two national
    account managers, Ricky Ponting and Greeme
    Smith, have joined the sales team as a result
    of these changes.

    To achieve this goal, we have also organized
    the new mass sales group into three distinct
    teams reporting to our current sales
    directors, Stephen Flemming and Boris Baker. I
    have outlined below the national account
    managers and their respective accounts in each
    of the teams. We have also hired two new
    national account managers and a new sales
    administrator to complete our account
    coverage. They include:
     


    第二种方法是使用 -i 选项,这告诉 grep 忽略大小写的敏感性。

    # grep -i the memo
    In order to better serve the needs of our mass
    market customers, ABC Publishing is
    integrating the groups selling to this channel
    for ABC General Reference and ABC Computer
    Publishing. This change will allow us to
    better coordinate our selling and marketing
    efforts, as well as simplify ABC's
    relationships with these customers in the
    areas of customer service, co-op management,
    and credit and collection. Two national
    account managers, Ricky Ponting and Greeme
    Smith, have joined the sales team as a result
    of these changes.

    To achieve this goal, we have also organized
    the new mass sales group into three distinct
    teams reporting to our current sales
    directors, Stephen Flemming and Boris Baker. I
    have outlined below the national account
    managers and their respective accounts in each
    of the teams. We have also hired two new
    national account managers and a new sales
    administrator to complete our account
    coverage. They include:
     


    除了 -i 选项之外,还有另外几个命令行选项可以用来改变 grep 的输出结果。最常见的选项如下所示:

    -c —— 屏蔽正常输出;相反,打印每个输入文件的匹配行数。
    -l —— 屏蔽正常输出;相反,打印包含正常输出内容的每个输入文件的名字。
    -n —— 在每行输出前面加上该行在输入文件中的行号作为前缀。
    -v —— 将匹配意义进行逆反 —— 即选择那些不 匹配搜索条件的行。

  • 相关阅读:
    HashPasswordForStoringInConfigFile中的Md5算法并非常用的Md5算法
    (转)C#实现MD5加密
    pb将datawindow数据导出EXCEL
    (转)笔记320 SQLSERVER中的加密函数 2013-7-11
    Sqlserver2005附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法
    PowerDesigner中Name与Code同步的问题
    (转) PowerDesigner中Table视图同时显示Code和Name
    SQLServer2012在登录远程服务器实例时报错:尝试读取或写入受保护的内存
    PB代码动态解析执行器
    XenServer安全重启xapi的方法
  • 原文地址:https://www.cnblogs.com/sopost/p/2190167.html
Copyright © 2011-2022 走看看