Hi,
I am trying to merge two files based on the matching of the first column in both. The first file looks like:
x1 x2 x3 x4
name1 10 20 40
name2 30 20 15
name3 90 5 21
the second file looks like:
name1 xyz1
name1 xyz2
name2 xxx1
name2 yyy1
name2 zzz1
and I want the output to look like this:
name1 10 20 40 | xyz1
name1 10 20 40 | xyz2
name2 30 20 15 | xxx1
name2 30 20 15 | yyy1
name2 30 20 15 | zzz1
the problem is that the attached script does not show all lines, it shows only
name1 10 20 40 | xyz2
name2 30 20 15 | zzz1
The script I used is:
awk ' BEGIN {
{FS="\t"}
if (!fn) fn = "inp1.dat"
while ((getline<fn)>0) {
a[$1] = $1; b[$1] = $2 }
}
{ if (a[$1] == $1) printf "%s|%s\n",$0,b[$1] }
' inp2.dat
how can I show all lines in the form above, any help is appreciated.
I am trying to merge two files based on the matching of the first column in both. The first file looks like:
x1 x2 x3 x4
name1 10 20 40
name2 30 20 15
name3 90 5 21
the second file looks like:
name1 xyz1
name1 xyz2
name2 xxx1
name2 yyy1
name2 zzz1
and I want the output to look like this:
name1 10 20 40 | xyz1
name1 10 20 40 | xyz2
name2 30 20 15 | xxx1
name2 30 20 15 | yyy1
name2 30 20 15 | zzz1
the problem is that the attached script does not show all lines, it shows only
name1 10 20 40 | xyz2
name2 30 20 15 | zzz1
The script I used is:
awk ' BEGIN {
{FS="\t"}
if (!fn) fn = "inp1.dat"
while ((getline<fn)>0) {
a[$1] = $1; b[$1] = $2 }
}
{ if (a[$1] == $1) printf "%s|%s\n",$0,b[$1] }
' inp2.dat
how can I show all lines in the form above, any help is appreciated.