2012-01-02 91 views
0

我需要您使用簡短的bash腳本提供幫助。我有一個文件夾,其中包含約150,000(!)xml文件。我需要一個腳本來提取所有包含指定行的文件。腳本應該儘可能快地工作,因爲腳本必須經常使用。bash腳本在包含特殊行的子文件夾中獲取文件

我的第一種方法是以下,用grep:

for f in temp/* 
do 
    if grep "^.*the line which should be equal.*$" "$f" 
    then 
     echo "use this file" 
    else 
     echo "this file does not contain the line" 
    fi 
done 

這種方法有效,但它需要太多的時間。有人知道更快的方法嗎?如果另一種腳本語言是更好的選擇,那也是可以的。

最好的問候, 邁克爾

+2

始終避免執行「命令每個文件」,如果在所有可能的;這不可避免地比使用一個命令處理多個文件要慢。 – 2012-01-02 19:41:06

回答

3

您可以使用grep沒有任何bash的處理程序。

-l, --files-with-matches 
      Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is 
      specified by POSIX.) 

所以,試試這個:

grep "the line which should be equal" --files-with-matches temp/* 
+0

150k文件可能太多的參數,如果你打「參數列表太長」的錯誤閱讀此:http://mywiki.wooledge.org/BashFAQ/095 – 2012-01-02 20:26:14

+0

謝謝,它工作正常:) – Michael 2012-01-03 13:37:55

相關問題