Hi friends
please help me on below,
i have 5 files like below
file1 is
x 10
y 20
z 15
file2 is
x 100
z 245
file3 is
y 78
z 23
file4 is
x 100
y 900
z 300
now i want the output as below( in a table format)
x 10 100 0 100
y 20 0 78 900
z 15 245 23 300
please help me it really urgent.. please help
I will let you figure out how to put a 0 when there is no data.
$ awk 'BEGIN{while (getline < "file1"){ map[$1]=$2 }}{x=map[$1];if ($1 in map){map[$1]=x" "$2}}END{for (i in map){print i,map}}' file2 file3 file4
x 10 100 100
y 20 78 900
z 15 245 23 300
cheers,
Devaraj Takhellambam
---------- Post updated at 09:45 AM ---------- Previous update was at 08:34 AM ----------
Try this....Hope this works.
awk 'BEGIN{while (getline < "file1"){ map[$1]=$2 }}{if (FNR == 1 && NR > 1){for (i in map){y=map;mn=split(y,m," ");if (mn < ln){map=y " " 0;}}};x=map[$1];if ($1 in map){map[$1]=x " " $2;ln=split(map[$1],n," ");}}END{for (i in map){print i,map}}' file2 file3 file4
O/P
x 10 100 0 100
y 20 0 78 900
z 15 245 23 300
cheers,
Devaraj Takhellambam
@devtakh: if a one-liner is more than say 60 characters then it may be time to make it a multi-liner.
pamu
4
or
$ awk 'FNR==1{++a}
{A[$1,a]=$2;B[$1]++}
END{for(i in B){
for(j=1;j<=a;j++){
t=A[i,j]?A[i,j]:"0"; s=s?s"\t"t:i"\t"t} print s; s="" }}
' file1 file2 file3 file4
x 10 100 0 100
y 20 0 78 900
z 15 245 23 300