I have two files
(___ represents blanks)
Foo1
1000 345 456
1001 876 908
1002 ___ 786
1003 643 908
1004 345 234
and Foo2
1000 345
1001 876
1002 111
1003 643
1004 345
I would like to read column 2 in foo1 and replace with column 2 (where $1 in foo1 matches $1 in foo2) in foo2 if value is blank.
So output file
1000 345 456
1001 876 908
1002 111 786
1003 643 908
1004 345 234
Thanks
awk 'FNR==NR{a[$1]=$2;next}NF <3{print $1,a[$1],$2}NF>2{print}' foo2 foo1
-Devaraj Takhellambam
devtakh,
You forgot to check if the first field of Foo1 is an index in the array.
garethsays,
Assuming you have 3 fields in Foo1:
awk '
NR==FNR{a[$1]=$2;next}
a[$1] && NF==2{$2=a[$1] FS $2}
1' Foo2 Foo1
Alternatively:
$
$ perl -ne 'BEGIN{open(F,"foo2"); while(<F>){($a,$b)=unpack"a4xa3"; $ids{$a}=$b} close(F)}
> {($x,$y,$z)=unpack("a4xa3xa3"); print "$x $ids{$x} $z\n"}' foo1
1000 345 456
1001 876 908
1002 111 786
1003 643 908
1004 345 234
$
$
tyler_durden