2015-10-03 38 views
0

我在文本文件中有一个令牌列表,并希望使用grep从包含这些令牌的第二个文本文件中获取行,但似乎无法使用grep访问shell变量:在grep中使用Shell变量

for n in `cat ./pos/1.txt` 
do 
cat dictionary.txt | grep "$n" 
done 

我试过$ N “$ N”,$ {N}, “$ {N}”,^ $ {N},和 “^ $ {N}” 他们似乎都不上班。

感谢

+0

你为什么不试试这个? 'grep“$ n”dictionary.txt' –

+4

我想你的整件事情会更好地写成'grep -f pos/1.txt dictionary.txt'。 –

+0

这个问题在Unix + Linux上交叉发布:http://unix.stackexchange.com/questions/233714/using-a-shell-variable-with-grep – John1024

回答

0

相反遍历的线条,这将是最好使用-f标志grep

grep -f pos/1.txt dictionary.txt 

这将打印与pos/1.txt中的模式相匹配的dictionary.txt的行。

0

貌似下面是你在找什么:

for n in `cat 1.txt` 
do 
grep $n dictionary.txt 
done