2015-11-06 67 views
0

我的文件路徑列表,我需要一個字符串比較:的grep:用另一個字符串的文件比較字符串

​​3210

$修改 - 是存儲文件路徑字符串變量

病毒碼文件例如:

SVCS/resources/ 
SVCS/bus/projects/busCallout/ 
SVCS/bus/projects/busconverter/ 
SVCS/bus/projects/Resources/ (ignore .jar) 
SVCS/bus/projects/Teema/ 
SVCS/common/ 
SVCS/domain/ 
SVCS/techutil/src/ 
SVCS/tech/mds/src/java/fi/vr/h/service/tech/mds/exception/ 
SVCS/tech/mds/src/java/fi/vr/h/service/tech/mds/interfaces/ 
SVCS/app/cashmgmt/src/java/fi/vr/h/service/app/cashmgmt/exception/ 
SVCS/app/cashmgmt/src/java/fi/vr/h/service/app/cashmgmt/interfaces/ 
SVCS/app/customer/src/java/fi/vr/h/service/app/customer/exception/ 
SVCS/app/customer/src/java/fi/vr/h/service/app/customer/interfaces/ 
SVCS/app/etravel/src/java/fi/vr/h/service/app/etravel/exception/ 
SVCS/app/etravel/src/java/fi/vr/h/service/app/etravel/interfaces/ 
SVCS/app/hermes/src/java/fi/vr/h/service/app/hermes/exception/ 
SVCS/app/hermes/src/java/fi/vr/h/service/app/hermes/interfaces/ 
SVCS/app/journey/src/java/fi/vr/h/service/app/journey/exception/ 
SVCS/app/journey/src/java/fi/vr/h/service/app/journey/interfaces/ 
SVCS/app/offline/src/java/fi/vr/h/service/app/offline/exception/ 
SVCS/app/offline/src/java/fi/vr/h/service/app/offline/interfaces/ 
SVCS/app/order/src/java/fi/vr/h/service/app/order/exception/ 
SVCS/app/order/src/java/fi/vr/h/service/app/order/interfaces/ 
SVCS/app/payment/src/java/fi/vr/h/service/app/payment/exception/ 
SVCS/app/payment/src/java/fi/vr/h/service/app/payment/interfaces/ 
SVCS/app/price/src/java/fi/vr/h/service/app/price/exception/ 
SVCS/app/price/src/java/fi/vr/h/service/app/price/interfaces/ 
SVCS/app/product/src/java/fi/vr/h/service/app/product/exception/ 
SVCS/app/product/src/java/fi/vr/h/service/app/product/interfaces/ 
SVCS/app/railcar/src/java/fi/vr/h/service/app/railcar/exception/ 
SVCS/app/railcar/src/java/fi/vr/h/service/app/railcar/interfaces/ 
SVCS/app/reservation/src/java/fi/vr/h/service/app/reservation/exception/ 
SVCS/app/reservation/src/java/fi/vr/h/service/app/reservation/interfaces/ 
kraken_test.txt 
namaker_test.txt 
shmaker_test.txt 

我需要的文件搜索模式與字符串比較,用grep這可能嗎?

+0

你能詳細說明你的期望嗎?你想在文件列表中尋找一個字符串嗎?或者將文件列表與另一個列表進行比較? –

+0

我誤解了一些東西,還是僅僅是簡單的字符串比較?在這種情況下看看http://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash-script – beruic

+0

你想grep的文件名或你需要看看文件裏面,做一個grep的內容? – Dominique

回答

0

這應該工作:

git status -s | grep -v " M" | awk '{if ($1 == "M") print $2}' | \ 
grep --file=.git/ForGeneratingSBConfigAlert.txt --fixed-strings --line-regexp 
  • 直接通過管道將awk輸出grep完全避免while循環。在大多數情況下,你會發現你並不需要在其中打印調試消息等。
  • --file需要一個文件與一個模式匹配每行。
  • --fixed-strings避免將模式中的任何字符視爲特殊字符。
  • --line-regexp將圖案固定,以便它們只在完整的行輸入匹配其中一個圖案時匹配。

所有這一切說,你能澄清你實際上想要完成什麼?

+0

反斜槓並不是必須的;管線末端的管道足以在下一條管線上繼續管道。 – tripleee

+0

@tripleee我通常認爲這是一種反模式,類似於PHP文件末尾不需要'?>'和JavaScript中最後一個';'。 – l0b0

+0

我剛剛插入你的代碼,它的工作原理是我需要的:D我只刪除了'--line-regexp',因爲在我的情況下它不需要。我在箱子裏想(在while循環裏面)。謝謝! –

0

我不確定我是否理解整體邏輯,但是想到幾個直接的建議。

  • 在絕大多數情況下,您可以避免grep | awk
  • A while循環與grep在循環內一次一行是一個反模式。您可能只想在整個輸入上運行一個grep

您的問題仍然會從您實際嘗試完成的解釋中受益。

cd "$(git rev-parse --show-toplevel)" 
git status -s | awk '!/ M/ && $1 == "M" { print $2 }' | 
grep -Fxf .git/ForGeneratingSBConfigAlert.txt 

我努力想辦法加回你的人類可讀的嘮叨,但轉念一想,這個程序可能是沒有更好。

-x選項到grep可能是錯誤的,這取決於您真正希望實現的目標。

+0

另外,很好的答案! –