简体   繁体   中英

Merging columns from two log files using bash

I have two long logs in the following format. Log 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

From each pair of the given logs I need to take only the second column from the part where the two columns of digits appears (it can happens on the different string numbers in different logs however its structure is the same and the number of the strings with the 2 column of the digits also the same). thus producing a new log in the old format (see below an example for two given logs):

2.23088    1.08899
2.19816    0.94107
2.26237    0.54611
2.28405    0.73843
2.26499    1.06740

I used to do it via

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

but sometimes it take a wrong strings (not the digits but something from the header where the strings begins with @) from the second log, so the resulting log contains mistakes. like

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

I will be thus thankful for the suggestions how to fix it!

Gleb

You can use this 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM