如何grep块?或者我应该使用 awk/ack?

Ame*_*ina 6 text-processing

假设我有一个包含以下信息的文件:

...
Entry '234238': some text
  some text
  some text
  some text
Entry '899823': some text
  some text
  some text
Entry '234238': more text
  more text
  more text
Entry '645353': some text
  some text
  some text
Run Code Online (Sandbox Code Playgroud)

我想提取一个特定的Entry '<code>'. 例如,grep_my_block 'Entry '234238'应该返回:

Entry '234238': some text
  some text
  some text
  some text
Entry '234238': more text
  more text
  more text
Run Code Online (Sandbox Code Playgroud)

注意:

  1. 所述<code>标识块可以在文件中出现多次。我们想提取所有这样的块。
  2. 块可能由未知数量的行组成

我该如何使用grep,awkack?

Hau*_*ing 6

awk "/^Entry '234238'/ {printline = 1; print; next}
     /^Entry / {printline = 0}
     printline"
Run Code Online (Sandbox Code Playgroud)

  • @Gnouc 我认为将易于理解的代码缩短为明显不太容易理解的代码 ** 而不** 添加解释是不好的编辑实践。这是炫耀,对读者没有帮助。 (4认同)
  • 同意。但是,两个版本(之前和之后)都会从一些解释中受益...... (3认同)