我有两个完整的AWK命令行:
awk 'NR >=2 {for (i=7;i<=NF;i++) if ($i ~ /^-/) sum1 += $i; print $1,$2,$3,$4,$5,$6,sum1, ; sum1 = 0}' test.txt
awk 'NR >=2 {for (i=7;i<=NF;i++) if ($i ~! /^-/) sum += $i; print $1,$2,$3,$4,$5,$6,sum, sum/192 ; sum = 0}' test.txt
我想将这两个命令组合到一个命令中,这样我就可以得到sum和sum1,我想打印它们两个!如果可以使用方程式!
类似的东西:
awk 'NR >=2 {for (i=7;i<=NF;i++) if ($i ~! /^-/) sum += $i; {for (i=7;i<=NF;i++) if ($i ~ /^-/) sum1 += $i ; print $1,$2,$3,$4,$5,$6,sum,sum1, sum/(192 +(sum1*2)) ; sum = 0 ; sum1 = 0}' test.txt
或
awk 'NR >=2 {for (i=7;i<=NF;i++) if ($i ~! /^-/) sum += $i && {for (i=7;i<=NF;i++) if ($i ~ /^-/) sum1 += $i ; print $1,$2,$3,$4,$5,$6,sum,sum1, sum/(192 +(sum1*2)) ; sum = 0 ; sum1 = 0}' test.txt
但是我收到了这个错误:
awk:cmd。行:1:
^意外的换行符或字符串结尾
如果它有助于我的文件是这样的:
probeset_id submitted_id chr snp_pos alleleA alleleB 562_201 562_202 562_203 562_204 562_205 562_206 562_207 562_208 562_209 562_210 562_211 562_212 562_213 562_214 562_215 562_216 562_217 562_218 562_219 562_220 562_221 562_222 562_223 562_224 562_225 562_226 562_227 562_228 562_229 562_230 562_231 562_232 562_233 562_234 562_235 562_236 562_237 562_238 562_239 562_240 562_241 562_242 562_243 562_244 562_245 562_246 562_247 562_248 562_249 562_250 562_251 562_252 562_253 562_254 562_255 562_256 562_257 562_258 562_259 562_260 562_261 562_262 562_263 562_264 562_265 562_266 562_267 562_268 562_269 562_270 562_271 562_272 562_273 562_274 562_275 562_276 562_277 562_278 562_279 562_280 562_281 562_283 562_284 562_285 562_289 562_291 562_292 562_294 562_295 562_296 562_400 562_401 562_402 562_403 562_404 562_405
AX-75448119 Chr1_41908741 1 41908741 T C 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 2 2 0 0 0 0 0 1 0 0 0 0 0
AX-75448118 Chr1_41908545 1 41908545 T C 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 1 2 2 2 2 2 2 2 2 2 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 1 2 2 2 0 1 1 1 2 -1 1 2 0 0 2 1 1 0 1 0 1 2 1 0 0 1 2 2 1 2 2 0 1 2 2 2 2 2 2 0 1 0 0 0 1 2 2 2 2 0
我希望结果像这样
AX-75448119 Chr1_41908741 1 41908741 T C 13 0 0.067
AX-75448118 Chr1_41908545 1 41908545 T C 98 -1 0.515
答案 0 :(得分:4)
以下是带有解释的格式良好的版本:
awk '
NR>1{
#Initialize the variables to 0 for every iteration
sum=0;
sum1=0;
#Loop from 7th column till the end
for(i=7;i<=NF;i++)
{
#Test if the value in that column is greater than zero
if($i>0)
{
#If test returns true, add value to variable sum
sum+=$i;
}
else
{
#If test returns false, add value to variable sum1
sum1+=$i;
}
}
#Loop again through the column 1-6
for(i=1;i<7;i++)
#Print the values of those columns
printf("%s ",$i);
#Print variables and function
printf("%d %d %f\n",sum,sum1,sum/(192 + (sum1*2)))
}' test.txt
[jaypal:~/Temp] awk '
NR>1{
sum=0;
sum1=0;
for(i=7;i<=NF;i++)
{
if($i>0)
{
sum+=$i;
}
else
{
sum1+=$i;
}
}
for(i=1;i<7;i++)
printf("%s ",$i);
printf("%d %d %f\n",sum,sum1,sum/(192 + (sum1*2)))
}' test.txt
AX-75448119 Chr1_41908741 1 41908741 T C 13 0 0.067708
AX-75448118 Chr1_41908545 1 41908545 T C 98 -1 0.515789
答案 1 :(得分:2)
这应该可以解决问题(避免多次迭代)。
$awk 'BEGIN{sum=0;sum1=0} NR >=2 {for (i=7;i<=NF;i++) if ($i !~ /^-/) sum += $i; else if ($i ~ /^-/) sum1 += $i; print $1,$2,$3,$4,$5,$6,sum, sum1,sum/(192 +(sum1*2)) ; sum=0;sum1=0}' test.txt
AX-75448119 Chr1_41908741 1 41908741 T C 13 0.0677083
AX-75448118 Chr1_41908545 1 41908545 T C -1 98 0.515789