grep,通配符存储在文件中 [英] grep with wildcards stored in a file

查看:132
本文介绍了grep,通配符存储在文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望grep通过读取需要从文本文件中过滤掉的内容来过滤掉行.

I want grep to filter out lines by reading what it needs to filter out from a text file.

这就是我给的grep.它存储在foo.txt:

Here's what I give grep. It's stored in foo.txt:

".*  /Users/1337/X$"
".*  /Users/1337/R$"
".*  /Users/1337/W$"

这是应该从中过滤的内容.它存储在bar.txt:

And here's what it should filter from. It's stored in bar.txt:

1121cfccd5913f0a63fec40a6ffd44ea64f9dc135c66634ba001d10bcf4302a2  /Users/1337/R
dc460da4ad72c482231e28e688e01f2778a88ce31a08826899d54ef7183998b5  /Users/1337/T
4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865  /Users/1337/W
53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3  /Users/1337/X

这是我运行的命令:

cat bar.txt | grep -f foo.txt

我希望它输出:

1121cfccd5913f0a63fec40a6ffd44ea64f9dc135c66634ba001d10bcf4302a2  /Users/1337/R
4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865  /Users/1337/W
53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3  /Users/1337/X

但是它什么也没输出.我该如何解决?

But it doesn't output anything. How can I fix this?

Mac OS X Yosemite,bash 3.2.57(1)-发行版

Mac OS X Yosemite, bash 3.2.57(1)-release

推荐答案

您应从foo.txt中删除双引号.引号由外壳处理,以保护特殊字符,但是引号实际上并不是模式的一部分,在从文件中读取时按字面意义进行处理.

You should remove the doublequotes from foo.txt. Quotes are processed by the shell to protect special characters, but they're not actually part of the pattern, and treated literally when reading from a file.

如果您无法修改文件,则可以使用进程替换快速删除它们:

If you can't modify the file, you can remove them on the fly using process substitution:

grep -f <(sed 's/^"\(.*\)"$/\1/' foo.txt) bar.txt

这篇关于grep,通配符存储在文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆