2015-11-06 66 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

另外,很好的答案! –