【问题标题】:How to append column to existing file in awk?如何将列附加到awk中的现有文件?
【发布时间】:2015-04-28 20:11:28
【问题描述】:

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

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

以秒为单位的 40 条 Time 记录和 Compiled procs 记录(点代表无用的行)。 如何将值为 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 的值只出现一次,最好出现在第一行,并且不使用中间文件。 到目前为止,我设法以秒为单位打印所有 Time 值,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 个。

【问题讨论】:

  • 一堆只包含点的行是没有用的,摆脱它们并简单地提供一些真实的、可测试的输入和相关的预期输出。另外,不要对每次出现的编译过程都使用相同的数字,在输入中改变它,这样当它出现在输出中时我们就可以知道它来自哪里。
  • 已更新,提供了一些真实数据;保持预期输出(忽略第二个上的点)。
  • 你能把它清理干净以显示我们真正需要看到的东西吗?我们不需要您告诉我们忽略的内容,也不需要查看您更新之前的内容。

标签: awk


【解决方案1】:

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

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

顺便说一句,“奇怪的缩进”是由于在使用 printf 时未能输出换行符并且未能从处理中过滤掉不需要的输入行。

【讨论】:

  • 是的,它工作得很好,但我之前必须删除 4 次出现的帽子 (^)。对于帽子,对于第一个问题,我收到了“除以零尝试”错误消息,而对于第二个问题,它没有输出任何内容。摘掉帽子就解决了问题。
  • 我从答案中的正则表达式中删除了“^”。
  • 感谢您的帮助!
猜你喜欢
  • 2013-01-23
  • 2020-04-29
  • 1970-01-01
  • 2017-06-30
  • 2023-03-07
  • 2016-10-23
  • 2010-12-10
相关资源
最近更新 更多