我有空格分隔文件,如下所示:
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
我想要做的是拥有每一行中所有数字的总和,如果有一个负数(只有-1存在),请忽略它,所以我希望得到这个结果:
AX-75448119 Chr1_41908741 1 41908741 T C 13
(即1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2 + 2 + 1)
和
AX-75448118 Chr1_41908545 1 41908545 T C 98
在这种情况下,-1被忽略!
我正在考虑在Linux中使用awk,我通常将其用于空格分隔文件,但我只知道如何将它用于列而不是行
答案 0 :(得分:2)
这可能是您正在寻找的使用纯awk
。
awk 'NR >=2 {for (i=7;i<=NF;i++) if ($i !~ /^-/) sum += $i; print $1,$2,$3,$4,$5,$6,sum; sum = 0}' data.txt
输出:
AX-75448119 Chr1_41908741 1 41908741 T C 13
AX-75448118 Chr1_41908545 1 41908545 T C 98
答案 1 :(得分:1)
我想建议一个Perl脚本:
#!/usr/bin/env perl
while(<>) {
my ($line,$sum,$next);
# repeat while there are two (or more) integers after the "... T C" prefix:
while (/^(AX-\d+\s+\S+\s+\d+\s+\d+\s+\w+\s+\w+\s+)(\d+)\s+(-?\d+)/) {
$line = $1;
$sum = $2;
$next = $3;
$sum += $next if ($next > 0); # do not add negative numbers.
# replace the two integers by their sum.
s/$line\d+\s+$next/$line$sum/;
}
print;
}
您可以像cat data | ./script.pl
我明白了:
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 13
AX-75448118 Chr1_41908545 1 41908545 T C 98
答案 2 :(得分:1)
如果你真的想避免perl(为什么?)你可以做这个hacky的东西,显然,它表现不佳:
while read f1 f2 f3 f4 f5 f6 line
do
echo "$f1 $f2 $f3 $f4 $f5 $f6 $(echo "$line" |
xargs -n1 | grep -v '^-' | paste -sd+ | bc)"
done < input
我明白了:
AX-75448119 Chr1_41908741 1 41908741 T C 13
AX-75448118 Chr1_41908545 1 41908545 T C 98
答案 3 :(得分:1)
@ steve的awk解决方案略有改动版
awk '
NR>1{
s = 0;
for (i = 7 ; i <= NF ; i++)
{
if ($i != -1)
{
s+=$i;
}
}
for (j = 1 ; j < 7 ; j++)
{
printf("%s ", $j);
}
print s;
}' file
[jaypal:~/Temp] awk '
NR>1{
s = 0;
for (i = 7 ; i <= NF ; i++)
{
if ($i != -1)
{
s+=$i;
}
}
for (j = 1 ; j < 7 ; j++)
{
printf("%s ", $j);
}
print s;
}' file
AX-75448119 Chr1_41908741 1 41908741 T C 13
AX-75448118 Chr1_41908545 1 41908545 T C 98