【发布时间】:2021-10-11 00:24:15
【问题描述】:
我有两个文件。
-> cat firstfile
sample-dev
sample-dev-auth
-> cat secondfile
sample-dev
sample-dev-auth
sample-dev-temp
然后我执行grep -wvf firstfile secondfile,我知道这会给我两个文件之间的差异,所以我认为输出将是sample-dev-temp,但没有
-> grep -wvf firstfile secondfile
什么都没有。
我尝试了grep -wf,它与-wvf相反,我虽然它会告诉我
sample-dev
sample-dev-auth
但还是不,就像
-> grep -wf firstfile secondfile
输出:
样本开发
sample-dev-auth
sample-dev-temp
它返回所有项目并突出显示相似的部分。
为什么会这样?以及如何得到我想要的结果?
【问题讨论】:
-
您可能正在寻找
comm或diff,而不是grep。 -
-
试试
grep -wvf secondfile firstfile。 -
echo sample-dev-auth | grep -w sample-dev打印出sample-dev-auth。-w选项查找单词边界,破折号是单词边界。所以firstfile中的第一行必然会匹配secondfile中的所有三行——因此不会打印任何内容。您需要一个具有不同“单词”定义的命令——或者可能使用-x(精确)匹配选项而不是-w。 (对grep -xvf firstfile secondfile的随意实验会产生错误的结果;看起来并不那么容易。)
标签: linux shell command-line grep command