I have following code to join multiple files together. It works fine but I want to replace the empty values to 0, so I used -e “0”. But it doesn’t work.
Any ideas?
for k in `ls file?`
do
if [ -a final.results ]
then
join -a1 -a2 -e "0" final.results $k > tmp.res
mv tmp.res final.results
else
cp $k final.results
fi
done
example:
file1:
a 1
b 2
file2:
a 1
c 2
file3:
b 1
d 2
Results:
a 1 0 1 0
b 2 1 0
c 2
d 2
expected:
a 1 1 0
b 2 0 1
c 0 2 0
d 0 0 2
It’s poorly documented, but when using
jointhe-eoption only works in conjunction with the-ooption. The order string needs to be amended each time around the loop. The following code should generate your desired output.As you can see, it starts to become messy. If you need to extend this much further it might be worth deferring to a beefier tool such as awk or python.