March 26 2018 Monday
Weather : rainy to cloudy
1、需求:
有两个文件a.txt和b.txt,需要将a.txt中有的但b.txt中没有的行找出来,并重定向到c.txt,然后计算c.txt的行数!
[root@Dasoncheng sbin]# cat b.sh
#!/bin/bash
##Find the line differents eche other !
[ -f c.txt ] && rm -f c.txt
n=`wc -l a.txt | cut -d " " -f 1`
for i in `seq 1 "$n"` ;
do
m=`sed -n "$i"p a.txt`
grep -q $m b.txt
if [ $? -eq 0 ] ;
then
continue
else
echo $m >> c.txt
fi
done
wc -l c.txt
answer referred:
[root@Dasoncheng sbin]# cat b.sh
#!/bin/bash
##Find the line differents eche other !
[ -f c.txt ] && rm -f c.txt
cat a.txt | while read line
do
if ! grep -q "^$line$" b.txt
then
echo $line >> c.txt
fi
done
wc -l c.txt
或者使用grep -f 实现:
#!/bin/bash
grep -vwf b.txt a.txt > c.txt
wc -l c.txt