过滤掉两个文件之间的内容

时间:2016-08-31 02:54:37

标签: linux shell unix ubuntu filter

我有两个文件

conditions.txt

abcd
efgh

logs.txt

efgh
ijkl
mnop
qrst

我期待输出为:

ijkl
mnop
qrst

实际输出:

efgh
ijkl
ijkl
mnop
mnop
qrst
qrst

这是我到目前为止所使用的代码

func(){
while read condition
do
if [[ $line = $condition ]] ; then
:
else
echo "$line";
done < condition.txt
}

while read line
do
func $line
done < log.txt

2 个答案:

答案 0 :(得分:2)

尝试使用grep:

$ grep -v -f conditions.txt logs.txt

来自GNU grep的手册页:

-v, --invert-match
          Invert the sense of matching, to select non-matching lines.

-f FILE, --file=FILE
          Obtain patterns from FILE, one per line.  If this option is used multiple times or is combined with the -e (--regexp)  option,  search  for  all  patterns
          given.  The empty file contains zero patterns, and therefore matches nothing.

答案 1 :(得分:1)

如果你不想重新发明轮子......

grep -vf conditions.txt logs.txt 
ijkl
mnop
qrst