I have a file containing many lines that are separated by one whitespace for example:
ATOM 9803 C2' 5AD 303 72.790 69.600 43.700 +0.140 0.00 PROT C 0
ATOM 9804 H2'' 5AD 303 73.450 68.960 44.300 +0.090 0.00 PROT H 0
ATOM 15 HE1 MET 1 110.230 70.830 15.490 +0.090 0.00 PROT H 0
ATOM 16 HE2 MET 1 111.230 72.300 15.480 +0.090 0.00 PROT H 0
ATOM 17 HE3 MET 1 112.070 70.760 15.610 +0.090 0.00 PROT H 0
ATOM 24 CB GLU 2 107.460 66.320 18.200 -0.180 0.00 PROT C 0
ATOM 251 HB1 GLU 2 106.550 65.940 17.700 +0.090 0.00 PROT H 0
I would like to arrange the columns according to a reference column with a specific spacing
AAAA 9999 AAAA AAA 999 99.999 99.999 99.999 +9.999 9.99 AAAA A 9
so the final output should be
ATOM 9803 C2' 5AD 303 72.790 69.600 43.700 +0.140 0.00 PROT C 0
ATOM 9804 H2'' 5AD 303 73.450 68.960 44.300 +0.090 0.00 PROT H 0
ATOM 15 HE1 MET 1 110.230 70.830 15.490 +0.090 0.00 PROT H 0
So far I'm trying to do it with awk but it does not work. It replaces all the lines with the reference line.
reference_line="AAAA 9999 AAAA AAA 999 99.999 99.999 99.999 +9.999 9.99 AAAA A 9"
awk -v ref_line="$reference_line" '
BEGIN { OFS = "" }
NR == 1 { print; next }
{
for (i = 1; i <= NF; i++) {
if (i == 2) {
$i = substr(ref_line, 11, 5)
} else if (i == 3) {
$i = substr(ref_line, 17, 4)
} else if (i == 4) {
$i = substr(ref_line, 22, 4)
} else if (i == 6) {
$i = substr(ref_line, 32, 5)
} else if (i == 7) {
$i = substr(ref_line, 39, 7)
} else if (i == 8) {
$i = substr(ref_line, 47, 8)
} else if (i == 9) {
$i = substr(ref_line, 56, 8)
} else if (i == 10) {
$i = substr(ref_line, 65, 8)
} else if (i == 11) {
$i = substr(ref_line, 74, 8)
} else if (i == 12) {
$i = substr(ref_line, 83)
}
}
print
}' out.txt > rearranged_out.txt