I am trying to reformat the table by filling any missing rows. The final table will have consecutive IDs in the first column. My problem is the index of the associate array in the awk script.
infile:
S01 36407 53706 88540
S02 69343 87098 87316
S03 50133 59721 107923
S05 1290 41074 135810
S06 11285 30164 40361
S07 11285 30164 40361
S10 11298 30165 40361
S11 18311 37266 135798
S12 14567 35958 62691
where S04, S08 & S09 are missing. And I am expecting the output as
S01 36407 53706 88540
S02 69343 87098 87316
S03 50133 59721 107923
S04 0 0 0
S05 1290 41074 135810
S06 11285 30164 40361
S07 11285 30164 40361
S08 0 0 0
S09 0 0 0
S10 11298 30165 40361
S11 18311 37266 135798
S12 14567 35958 62691
I have adapted the scripts of Schrutinizer from my previous post as
awk -v n=12 '
BEGIN {
OFS="\t"
zero=OFS 0 OFS 0 OFS 0
}
{
i=$1 # Line 7
A=$0 # Line 8
}
END{
for(i = 1; i <=n ; i++) {
id = sprintf("S%02d", i)
print id (id in A?A[id]:zero)
}
}
' infile
But the output is
S01S01 36407 53706 88540
S02S02 69343 87098 87316
S03S03 50133 59721 107923
S04 0 0 0
S05S05 1290 41074 135810
S06S06 11285 30164 40361
S07S07 11285 30164 40361
S08 0 0 0
S09 0 0 0
S10S10 11298 30165 40361
S11S11 18311 37266 135798
S12S12 14567 35958 62691
which has concatenated IDs in the first column.
The fix is to add an extra line between Line7 and Line8. $1=""
which is to empty $1 after each line.
I need elaboration how this new line works in awk.
Thanks a lot!