-1

I have two files like this.

file 1

1:apple
2:banana
3:pineapple
4:guava
5:orange

and file 2 like this

apple
guava
orange

i just want to compare file 2 with file 1 and print the 1st field for the lines missing in file 2. Required is something like below

3
4

i tried using diff

file1 <(cut -d: -f2 file2)

but got the output as below

1a2,3
> banana
> pineapple
1

How about an associative array lookup using Awk?

awk -F: 'NR==FNR {a[$1]; next} !($2 in a) {print $1}' file2 file1
2
3
  • Thanks, it serves my purpose, awk is a line by line processing wont it take too much time for large size files. – upkar Dec 4 '18 at 3:35
  • @upkar Define "large". Note that this solution reads one of the files into memory. If your files are several gigabytes in size, you may have issues. – Kusalananda Dec 4 '18 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.