Shell脚本搜索给定模式的段落

时间:2014-02-19 11:39:17

标签: regex linux shell unix

我需要一个shell脚本的帮助,它可以在INPUT_FILE中搜索包含匹配模式的PARAGRAPHS(在PARAGRAPH的任何行中)&产生2个输出文件:

OUTPUT_FILE_1 ::仅包含任何行中不包含匹配模式的PARAGRAPHS的文件

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

SHELL SCRIPT的输入:: 1. INPUT_FILE 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

由于

1 个答案:

答案 0 :(得分:2)

awk oneliner:

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