如何在awk中将列附加到现有文件?

时间:2015-04-28 20:11:29

标签: awk

我有一个名为bt.B.1.log的文件,如下所示:

.
.
.
Time in seconds =                   260.37
.
.
.
Compiled procs  =                        1
.
.
.
Time in seconds =                   260.04
.
.
.
Compiled procs  =                        1
.
.
.

以及以秒为单位的40条时间记录和编译过程(点代表无用的行)。 如何将具有Compiled procs值(即1)的单个列添加到以下两个命令的结果中:

  • 这会打印以秒为单位的时间平均值(感谢dawg这个值)

    awk -F ='/以秒为单位的时间/ {s + = $ 2; c ++} END {print s / c}'bt.B.1.log> t1avg.dat

期望的输出:

260.20 1
  • 以秒为单位打印所有时间,但是它有一个小问题;它在列表的开头打印了一个额外的空白行。

    awk'BEGIN {FS =“以秒为单位的时间=”}; {printf $ 2} {printf“”}'bt.B.1.log> t1.dat

期望的输出:

260.37 1
260.04
.
.
.

在这两种情况下,我都需要Compiled procs的值只出现一次,最好是在第一行,而不使用中间文件。 到目前为止我设法做的事情是以秒为单位打印所有时间值,其中Compiled procs列出现在每一行并带有一个奇怪的标识:

awk '/seconds/ {printf $5} {printf " "} /procs/ {print $4}' bt.B.1.log > t1.dat

请帮忙!

更新

文件bt.B.1.log的内容:

-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-
Start in 16:40:51--25/12/2014


NAS Parallel Benchmarks 3.3 -- BT Benchmark 

No input file inputbt.data. Using compiled defaults
Size:  102x 102x 102
Iterations:  200    dt:   0.0003000
Number of active processes:     1

Time step    1
Time step   20
Time step   40
Time step   60
Time step   80
Time step  100
Time step  120
Time step  140
Time step  160
Time step  180
Time step  200
Verification being performed for class B
accuracy setting for epsilon =  0.1000000000000E-07
Comparison of RMS-norms of residual
       1 0.1423359722929E+04 0.1423359722929E+04 0.7507984505732E-14
       2 0.9933052259015E+02 0.9933052259015E+02 0.3147459568137E-14
       3 0.3564602564454E+03 0.3564602564454E+03 0.4783990739472E-14
       4 0.3248544795908E+03 0.3248544795908E+03 0.2309751522921E-13
       5 0.3270754125466E+04 0.3270754125466E+04 0.8481098651866E-14
Comparison of RMS-norms of solution error
       1 0.5296984714094E+02 0.5296984714094E+02 0.2682819657265E-15
       2 0.4463289611567E+01 0.4463289611567E+01 0.1989963674771E-15
       3 0.1312257334221E+02 0.1312257334221E+02 0.4060995034457E-15
       4 0.1200692532356E+02 0.1200692532356E+02 0.2958887128106E-15
       5 0.1245957615104E+03 0.1245957615104E+03 0.2281113665977E-15
Verification Successful


BT Benchmark Completed.
Class           =                        B
Size            =            102x 102x 102
Iterations      =                      200
Time in seconds =                   260.37
Total processes =                        1
Compiled procs  =                        1
Mop/s total     =                  2696.83
Mop/s/process   =                  2696.83
Operation type  =           floating point
Verification    =               SUCCESSFUL
Version         =                      3.3
Compile date    =              25 Dec 2014

Compile options:
   MPIF77       = mpif77
   FLINK        = $(MPIF77)
   FMPI_LIB     = -L/usr/lib/openmpi/lib -lmpi -lopen-rte -lo...
   FMPI_INC     = -I/usr/lib/openmpi/include -I/usr/lib/openm...
   FFLAGS       = -O
   FLINKFLAGS   = -O
   RAND         = (none)


Please send the results of this run to:

NPB Development Team 
Internet: npb@nas.nasa.gov

If email is not available, send this to:

MS T27A-1
NASA Ames Research Center
Moffett Field, CA  94035-1000

Fax: 650-604-3957


Finish in 16:45:14--25/12/2014
-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-
-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-
Start in 16:58:50--25/12/2014


NAS Parallel Benchmarks 3.3 -- BT Benchmark 

No input file inputbt.data. Using compiled defaults
Size:  102x 102x 102
Iterations:  200    dt:   0.0003000
Number of active processes:     1

Time step    1
Time step   20
Time step   40
Time step   60
Time step   80
Time step  100
Time step  120
Time step  140
Time step  160
Time step  180
Time step  200
Verification being performed for class B
accuracy setting for epsilon =  0.1000000000000E-07
Comparison of RMS-norms of residual
       1 0.1423359722929E+04 0.1423359722929E+04 0.7507984505732E-14
       2 0.9933052259015E+02 0.9933052259015E+02 0.3147459568137E-14
       3 0.3564602564454E+03 0.3564602564454E+03 0.4783990739472E-14
       4 0.3248544795908E+03 0.3248544795908E+03 0.2309751522921E-13
       5 0.3270754125466E+04 0.3270754125466E+04 0.8481098651866E-14
Comparison of RMS-norms of solution error
       1 0.5296984714094E+02 0.5296984714094E+02 0.2682819657265E-15
       2 0.4463289611567E+01 0.4463289611567E+01 0.1989963674771E-15
       3 0.1312257334221E+02 0.1312257334221E+02 0.4060995034457E-15
       4 0.1200692532356E+02 0.1200692532356E+02 0.2958887128106E-15
       5 0.1245957615104E+03 0.1245957615104E+03 0.2281113665977E-15
Verification Successful


BT Benchmark Completed.
Class           =                        B
Size            =            102x 102x 102
Iterations      =                      200
Time in seconds =                   260.04
Total processes =                        1
Compiled procs  =                        1
Mop/s total     =                  2700.25
Mop/s/process   =                  2700.25
Operation type  =           floating point
Verification    =               SUCCESSFUL
Version         =                      3.3
Compile date    =              25 Dec 2014

Compile options:
   MPIF77       = mpif77
   FLINK        = $(MPIF77)
   FMPI_LIB     = -L/usr/lib/openmpi/lib -lmpi -lopen-rte -lo...
   FMPI_INC     = -I/usr/lib/openmpi/include -I/usr/lib/openm...
   FFLAGS       = -O
   FLINKFLAGS   = -O
   RAND         = (none)


Please send the results of this run to:

NPB Development Team 
Internet: npb@nas.nasa.gov

If email is not available, send this to:

MS T27A-1
NASA Ames Research Center
Moffett Field, CA  94035-1000

Fax: 650-604-3957


Finish in 17:03:12--25/12/2014
-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-

日志中有40个条目,但我只提供了2个用于缩写的目的。

1 个答案:

答案 0 :(得分:0)

要解决第一个问题,请替换:

awk -F= '/Time in seconds/ {s+=$2; c++} END {print s/c}' bt.B.1.log > t1avg.dat

使用:

awk 'BEGIN { FS = "[ \t]*=[ \t]*" }  /Time in seconds/ { s += $2; c++ } /Compiled procs/ { if (! CP) CP = $2 } END { print s/c, CP }' bt.B.1.log >t1avg.dat

一个潜在的小问题是可能会输出260.205 1,但问题并未将此作为给定脚本的弱点。使用printf "%.2f %s\n", s/c, CP之类的舍入可以提供260.21 1。要截断额外数字,请使用printf "%.2f %s\n", int (s/c * 100) / 100, CP之类的内容。

要解决第二个问题,请替换:

awk 'BEGIN { FS = "Time in seconds =" } ; { printf $2 } {printf " "}' bt.B.1.log > t1.dat

使用:

awk 'BEGIN { FS = "[ \t]*[=][ \t]" }  /Time in seconds/ { printf "%s", $2 } /Compiled procs/ { if (CP) { printf "\n" } else { CP = $2; printf " %s\n", $2 } }' bt.B.1.log > t1.dat

BTW,“奇怪的缩进”是因为在使用printf时未能输出换行符而未能从处理中过滤掉不需要的输入行。