Delete lines from file A that is in file B

后端 未结 2 1729
执念已碎
执念已碎 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:55

    As you mentioned, since comm needs sorted file to fetch the diff you can do in-line sorting before using comm like this :

    comm -23 <(sort file1) <(sort file2) > fileC
    
    0 讨论(0)
  • 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.)

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