【问题标题】:Shell Script To Search Paragraphs for a given pattern用于搜索给定模式的段落的 Shell 脚本
【发布时间】:2014-02-19 11:39:17
【问题描述】:

我需要一个 shell 脚本的帮助,它可以在 INPUT_FILE 中搜索包含 MATCHING PATTERN(在 PARAGRAPH 的任何行中)的 PARAGRAPHS 并生成 2 个输出文件:

OUTPUT_FILE_1:: 文件仅包含在任何行中不包含 MATCHING PATTERN 的 PARAGRAPHS

OUTPUT_FILE_2:: 仅包含 PARAGRAPHS 的文件,其中任何行中都包含 MATCHING PATTERN

Shell 脚本的输入:: 1. 输入文件 2. MATCH_KEY [这里,MATCH_KEY="模式匹配" ]

假设:

All PARAGRAPHS are separated by a BLANK LINE

// INPUT_FILE

first paragraph first line
first paragraph second line
first paragraph third line

second paragraph first line
second paragraph pattern match second line
second paragraph third line

third paragraph first line
third paragraph second line
third paragraph third line

fourth paragraph first line
fourth paragraph second line
fourth paragraph pattern match third line

2 个输出文件:

// OUTPUT_FILE_1:: 仅包含具有匹配模式的段落

second paragraph first line
second paragraph pattern match second line
second paragraph third line

fourth paragraph first line
fourth paragraph second line
fourth paragraph pattern match third line

// OUTPUT_FILE_2:: 仅包含没有匹配模式的段落

first paragraph first line
first paragraph second line
first paragraph third line

third paragraph first line
third paragraph second line
third paragraph third line

谢谢

【问题讨论】:

    标签: regex linux shell unix


    【解决方案1】:

    awk oneliner:

      awk -v RS="" -v ORS="\n\n" '/pattern match/{print > "file1";next}{print >"file2"}' file
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      相关资源
      最近更新 更多