Deleting lines from one file which are in another file

后端 未结 9 681
余生分开走
余生分开走 2020-11-28 01:46

I have a file f1:

line1
line2
line3
line4
..
..

I want to delete all the lines which are in another file f2:

9条回答
  •  情话喂你
    2020-11-28 02:02

    For exclude files that aren't too huge, you can use AWK's associative arrays.

    awk 'NR == FNR { list[tolower($0)]=1; next } { if (! list[tolower($0)]) print }' exclude-these.txt from-this.txt 
    

    The output will be in the same order as the "from-this.txt" file. The tolower() function makes it case-insensitive, if you need that.

    The algorithmic complexity will probably be O(n) (exclude-these.txt size) + O(n) (from-this.txt size)

提交回复
热议问题