One To Many Join #AWK


data1 contains a list of key

1 A,B
2 C

data2

1 XXX
2 YYY

We want to combine data1 and data2 by using column1 and display the results like this :

A XXX
B XXX
C YYY

The AWK script should be :

awk 'NR==FNR{ref[$1]=$2;next} 
{
if(ref[$1]) 
{
n=split(ref[$1],column2,",") 
for (i in column2) print column2[i] " " $2 
} 
}' data1 data2
Iklan
  1. November 6th, 2013

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s