Deleting lines from one file which are in another file

后端 未结 9 680
余生分开走
余生分开走 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:15

    grep -v -x -f f2 f1 should do the trick.

    Explanation:

    • -v to select non-matching lines
    • -x to match whole lines only
    • -f f2 to get patterns from f2

    One can instead use grep -F or fgrep to match fixed strings from f2 rather than patterns (in case you want remove the lines in a "what you see if what you get" manner rather than treating the lines in f2 as regex patterns).

提交回复
热议问题