2014-02-19 63 views
0

我需要一個shell腳本,可以搜索包含匹配模式(在段落的任何一行)款項的INPUT_FILE幫助&產生2個輸出文件:shell腳本來搜索段落對於給定的模式

OUTPUT_FILE_1 ::僅包含PARAGRAPHS文件不包含任何線路的匹配模式

OUTPUT_FILE_2 ::僅包含PARAGRAPHS文件,其中包含在任何線路的匹配模式

輸入,用於shell腳本:: 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 

感謝

回答

2

AWK oneliner:

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

非常感謝,正是我期待的! –