2012-09-26 48 views
2

我想用sed来做到这一点。我有2个文件:如何在下一个sed命令中使用pipe的结果?

keys.txt: 
host1 
host2 

test.txt 
host1 abc 
host2 cdf 
host3 abaasdf 

我想用sed来删除的test.txt包含在keys.txt关键字的行。所以test.txt的结果应该是

host3 abaasdf 

有人可以告诉我如何做到这一点与sed?

感谢,

+0

大约类似于http://stackoverflow.com/questions/7666149/command-substitution-within-sed-expression – tripleee

回答

2

我推荐使用grep这个(尤其是fgrep因为没有涉及到的正则表达式),所以

fgrep -v -f keys.txt test.txt 

做它的罚款。随着sed很快这个工程:

sed -i.ORIGKEYS.txt ^-e 's_^_/_' -e 's_$_/d_' keys.txt 
sed -f keys.txt test.txt 

(这就地修改原始keys.txt - 与备份 - 为sourceable sed脚本。)

+0

如果我做猫keys.txt | sed ...(删除这个键)。我怎样才能做到这一点? –

+0

你只是不能。 sed是有能力的,但没有那种能力的AFAIK。 –

+0

对于'grep -f'的+1 +1 –

2

fgrep -v -f是最好的解决方案。这里有几个选择:

commjoin

comm -13 <(join keys.txt test.txt) test.txt 

或AWK

awk 'NR==FNR {key[$1]; next} $1 in key {next} 1' keys.txt test.txt 
0

组合这可能会为你工作(GNU SED):

sed 's|.*|/^&\\>/d|' keys.txt | sed -f - test.txt