使用bash合并两个日志文件中的列

时间:2017-07-27 16:18:12

标签: bash text awk

我有以下格式的两个长日志。记录1:

@ autoscale onread none
@ with g0
@ g0 on
@ title "projection on eigenvectors (nm)"
@ xaxis  label "Time (ps)"
@ world xmin 6.94688e-310
@ world xmax 0.0479958
@ world ymin 1.84622
@ world ymax 4.02719
@ view xmin 0.15
@ view xmax 0.85
@ view ymin 0.15
@ view ymax 0.85
@ yaxis  label "vec 1"
@ xaxis tick major 0.01
@ xaxis tick minor 0.005
@ xaxis ticklabel start type spec
@ xaxis ticklabel start 1.85
@ yaxis tick major 1
@ yaxis tick minor 0.5
@ yaxis ticklabel start type spec
@ yaxis ticklabel start 2
    0.0000    2.23088
    0.0980    2.19816
    0.1137    2.26237
    0.0934    2.28405
    0.0926    2.26499
    .................
    0.0911    2.20825
    0.0873    2.32075

log 2

@ autoscale onread none
@ with g0
@ g0 on
@ title "projection on eigenvectors (nm)"
@ xaxis  label "Time (ps)"
@ world xmin 6.94067e-310
@ world xmax 0.0889754
@ world ymin -2.16935
@ world ymax 1.86708
@ view xmin 0.15
@ view xmax 0.85
@ view ymin 0.15
@ view ymax 0.85
@ yaxis  label "vec 2"
@ xaxis tick major 0.02
@ xaxis tick minor 0.01
@ xaxis ticklabel start type spec
@ xaxis ticklabel start -2.16
@ yaxis tick major 2
@ yaxis tick minor 1
@ yaxis ticklabel start type spec
@ yaxis ticklabel start -2
@ zeroxaxis bar on
@ zeroxaxis bar linestyle 3
    0.0000    1.08899
    0.1161    0.94107
    0.1056    0.54611
    0.1033    0.73843
    0.0987    1.06740
    .................
    0.1081    0.90706

从每对给定的日志中,我只需要从出现两列数字的部分中取出第二列(它可以出现在不同日志中的不同字符串数字上,但其结构相同且数量为带有2列数字的字符串也相同)。从而以旧格式生成新日志(参见下面两个给定日志的示例):

2.23088    1.08899
2.19816    0.94107
2.26237    0.54611
2.28405    0.73843
2.26499    1.06740

我曾经通过

这样做
paste log1.txt log2.txt | awk '/^[^;&@#]/{print $2, $4}' > result.log

但有时它会从第二个日志中获取一个错误的字符串(不是数字,而是字符串以@开头的标题中的内容),因此生成的日志包含错误。像

2.23088 zeroxaxis
2.19816 zeroxaxis
2.26237 2.15530
2.28405 1.93111
2.26499 1.45075
2.51312 1.54586
2.40316 1.50173
2.20825 1.92454
2.32075 1.89937
2.37953 1.38487
2.30873 1.44963
2.06744 1.40712

我将非常感谢有关如何修复它的建议!

格列勃

1 个答案:

答案 0 :(得分:2)

您可以使用此awk

awk 'NF != 2 || !/^[0-9. \t]+$/{next}
     NR==FNR{a[++i]=$2; next} {print a[++j], $2}' file1 file2

2.23088 1.08899
2.19816 0.94107
2.26237 0.54611
2.28405 0.73843
2.26499 1.06740
2.20825 0.90706
相关问题