2

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

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
Shruti
  • 741
  • 4
  • 14
  • 25

3 Answers3

3

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
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
1

In Bash, ksh, zsh:

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

In other shells you will need to presort your files.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
0
grep "$(cat file1.txt)" file2.txt
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106