How could I compare two files and remove similar rows in them (bash script)

前端 未结 3 1519
陌清茗
陌清茗 2021-01-28 05:21

I have two files of data with similar number of columns. I\'d like to save file2 in another file (file3) while I exclude the rows which are existed already in the file1.

<
相关标签:
3条回答
  • 2021-01-28 06:09

    Try:

    grep -Fxvf file1 file2
    

    Switch meanings available from the grep man page.

    0 讨论(0)
  • 2021-01-28 06:13

    grep -v -f is problematic because it searches file2 for each line in file1. With large files it will take a very long time. Try this instead:

    comm -13 <(cat file1 | tr '\t' ' ' | sort) <(sort file2)

    0 讨论(0)
  • 2021-01-28 06:18

    You can convert tabs to spaces on the fly:

    grep -vif <(tr '\t' ' ' < file1) file2 > file3
    

    This is process substitution.

    0 讨论(0)
提交回复
热议问题