簡體   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