繁体   English   中英

如何在包含特定搜索字符串的两个大括号之间获得多行字符串?

[英]How do I get multi-line string between two braces containing a specific search string?

我正在寻找一个快速简单的单行程序来提取包含来自文本文件的搜索字符串的所有大括号分隔的文本块。 我只是在谷歌搜索自己疯狂,但每个人似乎只发布关于在没有搜索字符串的情况下在大括号之间获取文本。

我有一个包含这样内容的大文本文件:

blabla
blabla {
  blabla
}
blabla
blabla {
  blabla
  blablaeventblabla
}
blabla

绝大多数括号内的条目不包含搜索字符串,即“事件”。

我想要提取的是每组花括号之间的所有文本(特别是包括多行匹配),但前提是所述文本还包含搜索字符串。 所以输出如下:

blabla {
  blabla
  blablaeventblabla
}

我的linux命令行是/ usr / bin / bash。 我一直在尝试各种grep和awk命令,但是无法让它工作:

awk '/{/,/event/,/}/' filepath

grep -iE "/{.*event.*/}" filepath

我觉得这很容易,因为这是一项常见的任务。 我在这里错过了什么?

这个gnu-awk应该有效:

awk -v RS='[^\n]*{|}' 'RT ~ /{/{p=RT} /event/{ print p $0 RT }' file
blabla {
   blabla
   blablaeventblabla
}

RS='[^\\n]*{\\n|}'将输入记录分隔符设置为后跟{ OR a }任何文本。 RT是内部awk变量,根据RS正则表达式设置为匹配文本。

用户999999999999999999999999999999得到了一个很好的答案使用sed ,我真的很喜欢,不幸的是他们的答案似乎已经因某种原因消失了。

这是为了那些可能感兴趣的人:

sed '/{/{:1; /}/!{N; b1}; /event/p}; d' filepath

说明:

/{/ if current line contains { then execute next block { start block :1; label for code to jump to /}/! if the line does not contain then execute next block { start block :1; label for code to jump to /}/! if the line does not contain then execute next block { start block :1; label for code to jump to /}/! if the line does not contain } then execute next block { start block N; add next line to pattern space b1 jump to label 1 }; end block /event/p if the pattern space contains the search string, print it (at this point the pattern space contains a full block of lines from then execute next block { start block N; add next line to pattern space b1 jump to label 1 }; end block /event/p if the pattern space contains the search string, print it (at this point the pattern space contains a full block of lines from then execute next block { start block N; add next line to pattern space b1 jump to label 1 }; end block /event/p if the pattern space contains the search string, print it (at this point the pattern space contains a full block of lines from { to } ) }; end block d delete pattern space then execute next block { start block N; add next line to pattern space b1 jump to label 1 }; end block /event/p if the pattern space contains the search string, print it (at this point the pattern space contains a full block of lines from ) }; end block d delete pattern space ) }; end block d delete pattern space

这是来自'leu'的这个宝石的修改版本(10x leu用于启发我们)。 这个做的事情非常相似。 提取以'DEC :: PKCS7 ['开头并以']结尾的所有内容!':

cat file | sed '/^DEC::PKCS7\[/{s///; :1; /\]\!$/!{N; b1;}; s///;};'
Explanation:
/^DEC::PKCS7\[/             # if current line begins with 'DEC::PKCS7[' then execute next block
{                           # start block
    s///;                       # remove all upto 'DEC::PKCS7['
    :1;                         # label '1' for code to jump to
    /\]\!$/!                     # if the line does not end with ']!' then execute next block
    {                               # start block
        N;                          # add next line to pattern space
        b1;                         # jump to label 1
    };                          # end block
    s///;                       # remove all from ']!' to end of line
};                          # end block

笔记:

  • 这适用于单线和多线。
  • 如果你有'],这将有意想不到的行为! 在输入的中间。
  • 这不回答这个问题。 它已经得到了很好的回答。 我的意图只是为了帮助其他案件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM