使用awk或任何其他编辑器编辑大型文本文件:循环以更改第n行

时间:2019-03-13 09:28:38

标签: awk notepad++ notepad

我有一个大文件(2Gb),具有以下结构:

-------------------------------------------------------------------------------
176 (comment: line 1)
  i =        1 (comment: line 2)
H -0.073307  8.187645  4.065238 (comment: line 3)
H 1.964613  5.187168  7.038084 (comment: line 4)
... (comment: lines 5 - 178)
176 (comment: line 179)
  i =        2 (comment: line 180)
H -0.055549  8.224715  4.041477 (comment: line 181)
H 1.971048  5.193901  7.012246 (comment: line 182)
... (comment: lines 183 - 356)
-------------------------------------------------------------------------------

这是分子动力学运行的输出。

176是原子数,'i = n'是迭代数,下面的线是原子坐标。我需要编写一个脚本,通过以下方式以非常大的txt格式修改每次迭代的前两行:

-------------------------------------------------------------------------------   
176 (comment: line 1)
  i =        1, time =        0.500, E =      -100.0000000000 (comment: line 2)  
H -0.073307  8.187645  4.065238 (comment: line 3)
H 1.964613  5.187168  7.038084 (comment: line 4)
... (comment: lines 5 - 178)
176 (comment: line 179)
  i =        2, time =        1.000, E =      -100.0000000000 (comment: line 2)
H -0.055549  8.224715  4.041477 (comment: line 181)
H 1.971048  5.193901  7.012246 (comment: line 182)
(comment: lines 183 - 356)
-------------------------------------------------------------------------------

这意味着对于迭代i = n,存在一个时间= 0.500 * n,以及能量(需要在此处输入常量-100.0000000000),例如。

i =        1000, time =        500.000, E =      -100.0000000000 

我知道这很简单,但是我完全迷失了。

1 个答案:

答案 0 :(得分:1)

$ awk 'match($0,/^[[:space:]]+i =[[:space:]]+[0-9]+/) {
    $0 = sprintf("%s, time =        %0.3f, E =        -100.0000000000%s", substr($0,1,RLENGTH), 0.5 * (++n), substr($0,RLENGTH+1))
} 1' file
-------------------------------------------------------------------------------
176 (comment: line 1)
  i =        1, time =        0.500, E =        -100.0000000000 (comment: line 2)
H -0.073307  8.187645  4.065238 (comment: line 3)
H 1.964613  5.187168  7.038084 (comment: line 4)
... (comment: lines 5 - 178)
176 (comment: line 179)
  i =        2, time =        1.000, E =        -100.0000000000 (comment: line 180)
H -0.055549  8.224715  4.041477 (comment: line 181)
H 1.971048  5.193901  7.012246 (comment: line 182)
... (comment: lines 183 - 356)
-------------------------------------------------------------------------------