Diff command for two files and output to third

474 Views Asked by At

I just a have a small problem with comparing two files with the diff command in a shell script. Say I have two ascii files, file1.txt and file2.txt, with contents:

file1.txt

blah/blah2/content.fits/  
blah3/blah4/content2.fits/  
blah5/blah6/content3.fits/  
blah7/blah8/content4.fits/  

file2.txt

content.fits  
content2.fits

I would now like to make a comparison of the two files based on the .fits extensions but write out the output to an ascii file keeping the formatting in file1.txt, i.e in this particular example the output file after comparing these two should give:

blah5/blah6/content3.fits/  
blah7/blah8/content4.fits/  

any ideas?

1

There are 1 best solutions below

0
On

You can use this awk to get that output:

awk -F/ 'FNR==NR {a[$1];next} !($(NF-1) in a)' file2.txt file1.txt
blah5/blah6/content3.fits/
blah7/blah8/content4.fits/