Delete lines from file A that is in file B

后端 未结 2 1730
执念已碎
执念已碎 2021-01-25 12:29

I have two large files A and B

I need to delete lines that appear in file B from file A and save the result

to file C

I have tried comm -23 file1

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-25 12:59

    You can use the following:

    grep -vxFf fileB fileA > fileC
    

    Sample:

    $ seq 5 > a   # 1 to 5  in file a
    $ seq 10 > b  # 1 to 10 in file b
    $ grep -vxFf a b > c
    $ cat c
    6
    7
    8
    9
    

    From man grep

    -F, --fixed-strings

    Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.)

    -f FILE, --file=FILE

    Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX.)

    -v, --invert-match

    Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)

    -x, --line-regexp

    Select only those matches that exactly match the whole line. (-x is specified by POSIX.)

提交回复
热议问题