繁体   English   中英

在grep中使用shell变量

[英]Using a shell variable with grep

我在一个文本文件中有一个令牌列表,想要使用grep从包含这些令牌的第二个文本文件中获取行,但是似乎在用grep访问shell变量时遇到了麻烦:

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

我已经尝试过$ n,“ $ n”,$ {n},“ $ {n}”,^ $ {n}和“ ^ $ {n}”,但它们似乎都不起作用。

谢谢

看起来以下是您正在寻找的:

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

与其遍历所有行,不如使用grep-f标志:

grep -f pos/1.txt dictionary.txt

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM