meallhour meallhour - 2 months ago 10
Bash Question

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

Answer

With awk you can do:

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