Compare 2 files and remove any lines in file2 when they match values found in file1

前端 未结 4 687
死守一世寂寞
死守一世寂寞 2020-12-10 22:11

I have two files. i am trying to remove any lines in file2 when they match values found in file1. One file has a listing like so:

File1

ZNI008
ZNI009         


        
4条回答
  •  盖世英雄少女心
    2020-12-10 22:48

    this is using Bash and GNU sed because of the -i switch

    cp file2 file3
    while read -r; do
        sed -i "/$REPLY/d" file3
    done < file1
    

    there is surely a better way but here's a hack around -i :D

    cp file2 file3
    while read -r; do
        (rm file3; sed "/$REPLY/d" > file3) < file3
    done < file1
    

    this exploits shell evaluation order


    alright, I guess the correct way with this idea is using ed. This should be POSIX too.

    cp file2 file3
    while read -r line; do
        ed file3 <

    in any case, grep seems to do be the right tool for the job.
    @byrondrossos answer should work for you well ;)

提交回复
热议问题