rmuller
rmuller

Reputation: 12849

How to delete all lines specified in another file

I have two (sorted) text files:

a.txt

1
2
3 
4

b.txt

1
3
7

I want to create a file only listing the lines in a.txt where all lines of file b.txt are removed.
So the result should be:

result.txt

2
4

Upvotes: 2

Views: 63

Answers (2)

Eugeniu Rosca
Eugeniu Rosca

Reputation: 5305

grep a.txt -F -x -v --file=b.txt                                   
            |  |  |    +                                           
            |  |  |    +--> obtain PATTERN from file                
            |  |  +-------> invert match                           
            |  +----------> force PATTERN to match only whole lines
            +-------------> PATTERN is a set of newline-separated fixed strings

outputs:

2
4

Upvotes: 4

Mario Zannone
Mario Zannone

Reputation: 2883

Assuming a.txt and b.txt are already sorted lexically:

comm -23 a.txt b.txt

Normally comm reads two files, which should be sorted lexically, and produces three text columns as output: lines only in file1; lines only in file2; and lines in both files.

The options -23 suppress the second and third columns.

Upvotes: 3

Related Questions