Leif Hvitved
Leif Hvitved

Reputation: 23

awk/join ? How do I print field from column based on match between two files

I have three files I'd like to join into one, semicolon delimited (note multiple occurences in file 1 of first row value possible)

File 1:

1;FOO;BAR;NU
1;V;V;E
2;F;B;N
3;FOO;NU;BAR

File 2:

1;YES
2;NO
3;YES

File 3:

1;NO
2;NO
3;YES

Desired outcome: (file1 $0, file2 $2, file3 $2)

1;FOO;BAR;NU;YES;NO
1;V;V;E;YES;NO
2;F;B;N;NO;NO
3;FOO;NU;BAR;YES;YES

I cant get my head around how this can be done... so any help would be appreciated!

Upvotes: 0

Views: 104

Answers (2)

anubhava
anubhava

Reputation: 785196

Using gnu awk you may do this:

awk 'BEGIN { FS=OFS=";" }
ARGIND == 1 {
   ++fr[$1]
   map[$1][fr[$1]] = $0
   next
}
$1 in fr {
   for (i in map[$1])
      map[$1][i] = map[$1][i] OFS $2
}
END {
   for (i in map)
      for (j in map[i])
         print map[i][j]
}' file1 file2 file3
1;FOO;BAR;NU;YES;NO
1;V;V;E;YES;NO
2;F;B;N;NO;NO
3;FOO;NU;BAR;YES;YES

Upvotes: 0

potong
potong

Reputation: 58430

This might work for you (GNU join):

join -t\; file1 file2 | join -t\; - file3

Join file1 and file2 first using ; as the field delimiter and pipe the result to a second invocation of join using stdin and file3 and the same delimiter.

Upvotes: 2

Related Questions