簡體   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