我正在开发 bash 脚本,该脚本循环多列数据填充并执行集成的 AWK 代码来操作多列数据。
#!/bin/bash
home="$PWD"
# folder with the outputs
rescore="${home}"/rescore
# folder with the folders to analyse
storage="${home}"/results
while read -r d; do
awk -F ", *" ' # set field separator to comma, followed by 0 or more whitespaces
FNR==1 {
if (n) { # calculate the results of previous file
f= # apply this equation to rescore data using values of $3 and $2
f[suffix] = f # store the results in the array
n=$1 # take ID of the column
}
prefix=suffix=FILENAME
sub(/_.*/, "", prefix)
sub(/\/[^\/]+$/, "", suffix)
sub(/^.*_/, "", suffix)
n = 1 # count of samples
min = 0 # lowest value of $3 (assuming all $3 < 0)
}
FNR > 1 {
s += $3
s2 += $3 * $3
++n
if ($3 < min) min = $3 # update the lowest value
}
print "ID" prefix, rescoring
for (i in n)
printf "%s %.2f\n", i, f[i]
}' "${d}_"*/input.csv > "${rescore}/"${d%%_*}".csv"
done < <(find . -maxdepth 1 -type d -name '*_*_*' | awk -F '[_/]' '!seen[$2]++ {print $2}')
简而言之,工作流程应处理位于 ${d} 文件夹内的 input.csv 的每一行,该文件夹已被我的 bash 脚本正确识别:
# input.csv located in the folder 10V1_cne_lig12
ID, POP, dG
1, 142, -5.6500 # this is dG(min)
2, 10, -5.5000
3, 2, -4.9500
4, 150, -4.1200
我的 AWK 脚本预计会处理每个 CSV 文件的每一行,以便将它们减少为两列,并保留在输出中: i) input.csv 第一列中的数字(包含已处理行的 ID)+包含 CSV 文件的文件夹名称 ($d) 以及 ii) 对 input.csv 的 POP 和 dG 列中的数字应用数学运算 (f) 的结果:
f(ID)= sqrt(((dG(ID)+10)/10)^2+((POP(ID)-240)/240))^2)
其中 dG(ID) 是 input.csv 的“重新评分”行的 dG 值 ($3),POP(ID) 是其 POP 值 ($2)。最终,output.csv 包含有关 1 input.csv 的信息应为采用以下格式:
# output.csv
ID, rescore value
1 10V1_cne_lig12, f(ID1)
2 10V1_cne_lig12, f(ID2)
3 10V1_cne_lig12, f(ID3)
4 10V1_cne_lig12, f(ID4)
虽然我的代码的 bash 部分(处理不同目录中的 CSV 循环)工作正常,但我仍然使用 AWK 代码,它没有正确分配行 ID,以便我可以使用 $2 和 $3 应用演示的数学运算具有精确 ID 的行的列。