我想处理文件中的某个块。我想要的工作是和块中的列。有可能使用awk吗?还是其他指挥官更好?
现在我能做的是
awk 'BEGIN { i=0; sum=0.0;} { if ($9) { print $7; sum+=$7;}} END {printf "total charge is %10.6f", sum}'在这种情况下,如果第9列中有另一个值,这将是错误的.
示例文件w. 1500行
.... many lines
[ atoms ]
; nr type resi res atom cgnr charge mass ; qtot
1 c3 1 2 C 1 0.138400 12.01000 ; qtot 0.138
2 os 1 2 O 2 -0.436600 16.00000 ; qtot -0.298
3 c3 1 2 C1 3 -0.093400 12.01000 ; qtot -0.392
4 c3 1 2 C2 4 -0.076400 12.01000 ; qtot -0.468
[ bonds ]
; ai aj funct r k
1 2 1 1.4316e-01 2.5824e+05 ; C - O
.... many lines are followed发布于 2018-09-27 05:58:46
您可以试试这个awk脚本:
awk '$0=="[ atoms ]"{go=1} # Set flag to start the sum
go{sum+=$7} # If flag set sum the 8th column
$0=="[ bonds ]"{go=0} # End the sum
END{printf "total charge is %10.6f\n",sum} # Print result
' filehttps://stackoverflow.com/questions/52528723
复制相似问题