2018/03/26

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

转载于:https://my.oschina.net/u/3651233/blog/1785230

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值