Shruti
Shruti

Reputation: 781

return common fields in two files

I have two files, should compare 1st column of file1 with 1st column of file2 and the resultant file should be file2

For example:

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

Answers (3)

ghostdog74
ghostdog74

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

Dennis Williamson
Dennis Williamson

Reputation: 359925

In Bash, ksh, zsh:

join -t: <(sort file1) <(sort file2)

In other shells you will need to presort your files.

Upvotes: 1

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31560

grep "$(cat file1.txt)" file2.txt

Upvotes: 0

Related Questions