meallhour
meallhour

Reputation: 15629

Getting values in a file which is not present in another file

I have two files

File1.txt:

docker/registry:2.4.2
docker/rethinkdb:latest
docker/swarm:1.0.0

File2.txt:

docker/registry:2.4.1
docker/rethinkdb:1.0.0
docker/swarm:1.0.0

The output should be:

docker/registry:2.4.2
docker/rethinkdb:latest

In other words, every line in File1 that doesn't exist in File2 should be part of the output.

I have tried doing the following but it is not working.

diff File1.txt File2.txt

Upvotes: 2

Views: 48

Answers (3)

James Brown
James Brown

Reputation: 37464

You could just use grep for it:

$ grep -v -f file2.txt file1.txt
docker/registry:2.4.2
docker/rethinkdb:latest

If there are lots of rows in the files I'd probably use @user000001 solution.

Upvotes: 2

SLePort
SLePort

Reputation: 15471

With comm:

comm -23 <(sort File1.txt) <(sort File2.txt)

Upvotes: 1

user000001
user000001

Reputation: 33387

With awk you can do:

awk 'NR==FNR{a[$0];next}!($0 in a)' file2 file1

Upvotes: 1

Related Questions