0

I have two text files:

f1

A   B
sam 23
dam  90

f2

 G  A  K
43 DQF 65
54 sam 90
56 jay 89
67 dam 43
45 fds 78

I would like to add to f1 cols G and K but only for names that I have in A

desired output:

 A   B    G    K
 sam 23   54   90
 dam  90  67   43
temor
  • 935
  • 2
  • 10
  • 26

1 Answers1

1

That is better accomplished with merge:

> merge(f1, f2)
    A  B  G  K
1 dam 90 67 43
2 sam 23 54 90

Doing it with grep might work, but merge is based on match and will be more efficient. It automatically matches on columns with shared names in the two first arguments , which is this case is "A" and is what you wanted. It then adds the remaining columns for the matching rows. (An "inner join" in the language of relational databases.)

IRTFM
  • 258,963
  • 21
  • 364
  • 487