Regex Shell脚本,用于搜索给定模式的段落

Regex Shell脚本,用于搜索给定模式的段落,regex,linux,shell,unix,Regex,Linux,Shell,Unix,我需要一个shell脚本的帮助,该脚本可以在输入文件中搜索包含匹配模式的段落(在段落的任何一行中),并生成2个输出文件: OUTPUT_FILE_1::仅包含在任何行中都不包含匹配模式的段落的文件 OUTPUT_FILE_2::仅包含在任何行中包含匹配模式的段落的文件 SHELL脚本的输入:: 1.输入文件 2.MATCH_KEY[此处,MATCH_KEY=“模式匹配”“] 假设: All PARAGRAPHS are separated by a BLANK LINE //输入文件 firs

我需要一个shell脚本的帮助,该脚本可以在输入文件中搜索包含匹配模式的段落(在段落的任何一行中),并生成2个输出文件:

OUTPUT_FILE_1::仅包含在任何行中都不包含匹配模式的段落的文件

OUTPUT_FILE_2::仅包含在任何行中包含匹配模式的段落的文件

SHELL脚本的输入:: 1.输入文件 2.MATCH_KEY[此处,MATCH_KEY=“模式匹配”“]

假设:

All PARAGRAPHS are separated by a BLANK LINE
//输入文件

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个输出文件:

All PARAGRAPHS are separated by a BLANK LINE
//输出文件\u 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
first paragraph first line
first paragraph second line
first paragraph third line

third paragraph first line
third paragraph second line
third paragraph third line
//输出文件\u 2::只包含没有匹配模式的段落

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
first paragraph first line
first paragraph second line
first paragraph third line

third paragraph first line
third paragraph second line
third paragraph third line
感谢awk oneliner:

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