【问题标题】:How to extract text between two words in second occurrence - unix shell scripting如何在第二次出现时提取两个单词之间的文本 - unix shell scripting
【发布时间】:2021-01-15 13:05:38
【问题描述】:

我正在尝试提取一个字符串,该字符串包含字符串的两个单词之间的所有内容:

输入:

Please find the text [ This is a sample text [ Hello World - Earth ] ]

输出:

Hello World - Earth

第二次出现“[”和“]”之间的文本

【问题讨论】:

  • 这是grepsed 等的贪婪匹配不是真正问题的地方,即echo "Please find the text [ This is a sample text [ Hello World - Earth ] ]" | sed 's/^.*[[]//;s/[]].*$//':输出`Hello World - Earth`。祝你好运。

标签: bash unix awk sed grep


【解决方案1】:

如果您的grep 支持-P 选项(PCRE),请尝试一下:

grep -Po "\[.*?\[\K.+?(?=])" <<< "Please find the text [ This is a sample text [ Hello World - Earth ] ]"
  • \K 序列告诉引擎丢弃前面的匹配项 匹配结果为lookbehind
  • (?=]) 模式是期待匹配右方括号 也从结果中排除匹配项。

【讨论】:

    【解决方案2】:

    使用awk 你可以做类似的事情

    $ awk -F'[][]' '{print $3}' <<< "Please find the text [ This is a sample text [ Hello World - Earth ] ]"
     Hello World - Earth 
    

    【讨论】:

      【解决方案3】:

      这可能对你有用(GNU sed):

      sed -E 's/^[^[]*\[(.*)\].*/\1/;s//\1/' file
      

      查找第一组[...]之间的所有内容,然后重复该过程以查找第二组[...]之间的所有内容。

      可编程版本:

       sed -E ':a;/^[^[]*\[(.*)\].*/!d;s//\1/;x;s/^/x/;/x{2}/{x;b};x;ta' file
      

      x{2} 中的整数更改为所需的深度。

      注意这是逐行工作的,不适合带引号的方括号。

      【讨论】:

        猜你喜欢
        • 2011-09-01
        • 2016-12-21
        • 1970-01-01
        • 1970-01-01
        • 2014-08-20
        • 2021-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多