2016-02-25 50 views
0

這一起顯示比賽是Multiple line, repeated occurence matching桑達比賽在多個文件中,文件名和行號

持續一段時間我有內容按照前文的多線程測試中* .txt文件。

test1.txt的

blah blah.. 
blah blah.. 
blah abc blah1 
blah blah.. 
blah blah.. 
blah abc blah2 
blah blah.. 
blah efg1 blah blah 
blah efg2 blah blah 
blah blah.. 
blah blah.. 

blah abc blah3 
blah blah.. 
blah blah.. 
blah abc blah4 
blah blah.. 
blah blah blah 

blah abc blah5 
blah blah.. 
blah blah.. 
blah abc blah6 
blah blah.. 
blah efg3 blah blah 

blah efg4 blah blah 
blah abc blah7 
blah blah.. 
blah blah.. 
blah abc blah8 
blah blah.. 

現在我想修改輸出上運行的所有文件sed命令,而且還加上行號與sed命令的輸出顯示的文件名(如果可能) ...

我運行下面的命令

ls test*.txt | xargs sed -n -f findMatch.txt 

findMatch.txt內容

/abc/h;/efg/!b;x;/abc/p;z;x 

輸出是

blah abc blah2 
blah abc blah6 
blah abc blah2 
blah abc blah6 
blah abc blah2 
blah abc blah6 

我需要更多的詳細的輸出按下面

test1.txt ln6 blah abc blah2 
test1.txt ln23 blah abc blah6 
test2.txt ln6 blah abc blah2 
test2.txt ln23 blah abc blah6 
test3.txt ln6 blah abc blah2 
test3.txt ln23 blah abc blah6 
+0

該操作是一個有點複雜。雖然它應該可以用sed(或sed的管道)來實現,但我建議你移動到awk。使用awk會容易得多。 awk可以執行此操作。另外,awk有像'FILENAME'和'NR/FNR'這樣的變量可以在這裏使用。 – anishsane

回答

1

用於搜索所述特定圖案的所有文件grep命令。

grep -f patten_file -Rn *.txt 

-R遞歸

-n行數

Patten_file 
hai 
hello 
this 

輸出:

1.txt:1:hai 
1.txt:2:hello 
2.txt:1:hai 
2.txt:2:this 
+0

我不認爲OP的操作感興趣,只是搜索。 – anishsane