【问题标题】:Grepping stdout to attain specific strings, while avoiding repeatsGrepping stdout 以获得特定的字符串,同时避免重复
【发布时间】:2021-07-06 04:58:36
【问题描述】:

我试图 grep 一个命令的标准输出,只显示两个字符串之间的结果。我想从“别名”的第一个案例打印到“有效”的第一个案例,然后再次重复。

stdout: ("

foo
bar
Alias: Name                        <---
fo bar
foo bar
Valid from: Monday Until: Thu May 26 12:44:38         <---
Valid from: Tuesday Until: Fri Nov 27 22:41:01 
Alias: Another_Name                                   <---
Valid from: Wednesday Until: Fri Nov 27 22:41:01         <---
Valid from: Friday Until: Thu Dec 04 23:31:58
Foo bar 
bar foo 

管道预期输出:

Alias: Name 
Valid from: Monday Until: Thu May 26 12:44:38
Alias: Another_Name 
Valid from: Wednesday Until: Fri Nov 27 22:41:01

我尝试过的事情:

命令 | egrep "Alias:|Valid" #这也捕获了第二个'valid' 命令 | grep -P "Alias*|Valid*" #这也再次捕获了第二个'valid..'

任何帮助将不胜感激!

【问题讨论】:

    标签: awk sed grep


    【解决方案1】:

    编辑:在与 OP 讨论后,以下代码对 OP 运行良好。用 GNU awk 编写和测试。

    awk -v IGNORECASE="1" '
    /alias/{
      count=0
      aliasVal=$0
      next
    }
    aliasVal && /valid/ && ++count==1{
      print aliasVal ORS $0
    }
    ' Input_file
    

    这可以在awk程序中完成,请尝试一次。仅使用您展示的示例编写和测试。

    awk '
    /Alias:/{
      count=0
      aliasVal=$0
      next
    }
    aliasVal && /Valid from:/ && ++count==1{
      print aliasVal ORS $0
    }
    '   Input_file
    

    说明:为上述添加详细说明。

    awk '                      ##Starting awk program from here.
    /Alias:/{                  ##Checking if line contains Alias: then do following.
      count=0                  ##Setting count to 0 here.
      aliasVal=$0              ##Setting aliasVal variable to current line value here.
      next                     ##Skipping all further statements from here.
    }
    aliasVal && /Valid from:/ && ++count==1{  ##Checking if aliasVal is set AND line contains Valid from: AND count is 1 then do following.
      print aliasVal ORS $0    ##Printing aliasVal ORS and current line here.
    }
    ' Input_file               ##Mentioning Input_file name here.
    


    要在 bash 脚本中运行它,请尝试以下操作:

    cat script.bash
    #!/bin/bash
    awk '
    /Alias:/{
      count=0
      aliasVal=$0
      next
    }
    aliasVal && /Valid from:/ && ++count==1{
      print aliasVal ORS $0
    }
    '   Input_file
    

    然后给脚本适当的权限并运行它。

    要从命令行运行它,您可以在非一个衬里代码上运行,也可以直接在终端本身上跟随一个衬里代码。

    awk '/Alias:/{count=0;aliasVal=$0;next} aliasVal &amp;&amp; /Valid from:/ &amp;&amp; ++count==1{print aliasVal ORS $0}' Input_file

    【讨论】:

    • 嗨@RavinderSingh13,感谢您的回复!对 bash 有点陌生,所以想知道我是否只是将上面的内容粘贴到脚本中?有没有办法让它从命令行执行?
    • @Shak,是的,您可以在终端上运行这一行,例如:awk '/Alias:/{count=0;aliasVal=$0;next} aliasVal &amp;&amp; /Valid from:/ &amp;&amp; ++count==1{print aliasVal ORS $0}' Input_file 其中 Input_file 是您的文件。试试看,让我知道结果如何。
    • 嗨@RavinderSingh13 我非常感谢你的详细解释,但是我正在使用命令输出和管道它。 “命令|”可以这样做吗?还是需要制作文件?
    • @Shak,还是试试awk '/[aA][Ll][Ii][Aa][Ss]/{count=0;aliasVal=$0;next} aliasVal &amp;&amp; /[vV][Aa][Ll][Ii][Dd]/ &amp;&amp; ++count==1{print aliasVal ORS $0}' Input_file 一次?
    • 成功!!!!!!!!!!!!!我能否解释一下它是如何工作的以及最终是什么问题?
    【解决方案2】:

    可能是这样的:

    awk -F: '$1==(f ? "Valid from" : "Alias name") {print; f=!f}'
    

    : 上拆分,当 $1 是预期值时打印(通过翻转一个变量来交替)。

    或者 sed 可以使用它的范围运算符,后跟它的特殊空正则表达式地址(使用最近使用的正则表达式)

    sed '/^Alias name:/,/^Valid from:/!d;//!d'
    

    【讨论】:

    • 嗨@rowboat 感谢您的回答,但这似乎不适用于管道。我正在做的是“命令|”而不是将其存储在文件中。
    • 根据这个other questionkeytool 的输出实际上有Alias name: ... 而不是Alias: name,就像你在问题中写的那样。
    【解决方案3】:

    这可以使用简单的sed 来完成:

    sed -n '/^Alias:/,/^Valid from:/ {//p;}' file
    
    Alias: Name
    Valid from: Monday Until: Thu May 26 12:44:38
    Alias: Another_Name
    Valid from: Wednesday Until: Fri Nov 27 22:41:01
    

    【讨论】:

    • 嗨@anubhava 感谢您的回答,但这似乎不适用于管道。我正在做的是“命令|”而不是将其存储在文件中。
    • 只要使用your_command | sed -n '/^Alias:/,/^Valid from:/ {//p;}'
    • @Shak 管道输入和文件输入没有什么神奇之处,无论是来自command | sed 'script' 还是sed 'script' file 都只是输入,awk 也是如此。
    【解决方案4】:
    $ awk '/^Alias/{print; f=0} /^Valid/ && !f++' file
    Alias: Name                        <---
    Valid from: Monday Until: Thu May 26 12:44:38         <---
    Alias: Another_Name                                   <---
    Valid from: Wednesday Until: Fri Nov 27 22:41:01         <---
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 2014-02-05
      • 1970-01-01
      • 2023-02-06
      • 2018-10-15
      • 1970-01-01
      • 2021-08-11
      相关资源
      最近更新 更多