Reputation: 781
I have two files, should compare 1st column of file1 with 1st column of file2 and the resultant file should be file2
For example:
file1
apple
banana
Mango
potato
tomato
file 2
apple:fruit
brinjal: vegetable
lady's finger: vegetable
orange: fruit
tomato: vegetable
potato: vegetable
Resultant file should look something like this:
apple:fruit
tomato: vegetable
potato: vegetable
any ideas on this would be appreciated
Thanks
Upvotes: 2
Views: 629
Reputation: 342303
without the need to sort (less process creation)
$ awk -F":" 'FNR==NR{f[$0];next}($1 in f)' file file2
apple:fruit
tomato: vegetable
potato: vegetable
Upvotes: 3
Reputation: 359925
In Bash, ksh, zsh:
join -t: <(sort file1) <(sort file2)
In other shells you will need to presort your files.
Upvotes: 1