【shell一天一练】遍历文件内容

73 阅读1分钟

今日小练题目📢

有两个文件a.txt和b.txt,需求是,把a.txt中有的且b.txt中没有的行找出来,并写入到c.txt, 然后计算c.txt文件的行数。

优秀作业🤌🏻

#!/bin/bash
#author:xYLiuuuuuu
#date:2024-12-16

[ -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

敲黑板📝

  • 遍历文件每一行,用while read ...而不是for
  • grep "^${abc}$" b.txt 过滤的是完整的行,从头匹配到结尾