2013-07-02 31 views
0

我期待在我的網站上找到關鍵字的外觀。因此,例如,我搜索除「.pdf」以外的所有文件中的「數字貨幣」。如果可能,我想輸出結果(出現單詞的文件名)到文件中,並在該文件中的每個條目後添加一個新行。搜索關鍵字的外觀和直接輸出到文件

添加結果顯示的行號也很棒,但一次只能做一件事。

我已經把兩條命令混合在一起,試圖接近這個,兩者都沒有像預期的那樣工作。

grep -rl "Digital Currency" --exclude "*.pdf" >> wordcount-digital-currency.txt 

find /home/ukglobal/public_html/ -exec grep -H -r -n 'Digital Curency' "*.html" --exclude "*.pdf" {} \ >> wordcount-digital-currency.html; 

誰能告訴我這些命令有什麼問題/如何實現這個?

回答

1

這是不夠的:

grep -nr "Digital Currency" --exclude "*.pdf" \ 
    --exclude wordcount-digital-currency.txt > wordcount-digital-currency.txt 

,如果你想排除匹配的行,使用cut

grep -nr "Digital Currency" --exclude "*.pdf" | \ 
     cut -d: -f1,2 > wordcount-digital-currency.txt 

grep man-page

-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.) 
0

要搜索的發生在文件中的字符串SEARCH_STRING,並將文件名寫入另一個文件網元W_FILE_NAME

for i in `ls -1tr` 
{ 
SEARCH_STRING_LINE_NO=`grep -n SEARCH_STRING i | cut -d: -f1`; 
if [ SEARCH_STRING_LINE_NO > 0 ] then 
FILE_NAME >> NEW_FILE_NAME 
fi 
} 

小的語法錯誤之外,這應該ATLEAST指導u到年目標