Join Multiple Files #AWK


I am in love AWK recently 😀 get rid of Java, Perl etc in somewhere else.  AWK is able to process files at once quickly. I have posted an example to join two files,  now, I will show you how to process 3 files which are related each other. For instance, we have 3 files data1, data2, data3

data1

1 A
2 C

data2

2
3

data3

1 XXX
2 YYY

Displaying data3 based on key in data1 and data2 can be done as follows

awk '{
if(FNR ==1) f++
if(NR==FNR){ref[$1]=$2;next}
else if(f==2){ref2[$1]=1;next}

if(ref[$1] && ref2[$1] )
{
print ref[$1] " " $2
}
}
' data1 data2 data3 
  1. No trackbacks yet.

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