logo

Openplatform.xyz

Placeholder for our stuff related to Telecom, IT, Internet of things (IOT), ESP8266, Raspberry Pi

 

AWK Script:  Join two tables on common field

Script-1: All rows have equal number of fields in table-1

::::::::::::::
table1
::::::::::::::
2,xx,yy,zz
1,x,y,z
3,xxx,yyy,zzz



::::::::::::::
table2
::::::::::::::
1,a,b,c,d
3,aaa,bbb,ccc,ddd
2,aa,bb,cc,dd
3,aaa3,bb3,cc3,dd3


awk -F',' 'NR==FNR {a[$1]=($0);next} a[$1] {$1=a[$1]} {print $0}' OFS=',' table1 table2
1,x,y,z,a,b,c,d
3,xxx,yyy,zzz,aaa,bbb,ccc,ddd
2,xx,yy,zz,aa,bb,cc,dd
3,xxx,yyy,zzz,aaa3,bb3,cc3,dd3 

Script-2: Some of rows have less fields in table-1

::::::::::::::
table1
::::::::::::::
2,xx,yy
1,x,y,z
3,xxx,yyy,zzz

::::::::::::::
table2
::::::::::::::
1,a,b,c,d
3,aaa,bbb,ccc,ddd
2,aa,bb,cc,dd
3,aaa3,bb3,cc3,dd3


awk -F',' 'NR==FNR {a[$1]=($1","$2","$3","$4);next} a[$1] {$1=a[$1]} {print $0}' OFS=',' table1 table2
1,x,y,z,a,b,c,d
3,xxx,yyy,zzz,aaa,bbb,ccc,ddd
2,xx,yy,,aa,bb,cc,dd
3,xxx,yyy,zzz,aaa3,bb3,cc3,dd3 



 



Suresh Hariramani

I am an IOT enthusiast with more than 20 years of experience in the IT sector. Specializing in telecom service's; follow me for some very innovative and best in class IOT products as I unfold my knowledge and passion for the subject.


Vatsal Hariramani

Just me, myself and I, exploring the universe of uknownment. I have a heart of love and interests in technology, IOT and travel . And I want to share my world with you .