0

I got output like that:

0.000
0.002
0.000
0.002
0.000
0.026
=====
0.026

Note, how the last line (0.026) is the endtime of the phases before. I will repeat that command every n seconds and I want that the next output is written to the next column like:

0.000   0.000
0.002   0.001
0.000   0.000
0.002   0.001
0.000   0.000
0.026   0.027
=====   =====
0.026   0.028

I know something with sed -i will solve my problem but I just can't get it work. Since it is very important to have exact same timestamp on this value I cant execute the command followed by the next one and then write the results to their place.

The command that gives me these values is:

curl -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null -s http://linux.com/
Vivian
  • 1,539
  • 14
  • 38
JMAD2016
  • 111
  • 1
  • 2
  • 9

2 Answers2

1

Using paste and command substitution you can add a new column side by side to previous output stored in any file:

paste -d '\t' prevfile.out <(                                                                        
curl -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n=====================\nTotal time:\t%{time_total}\n' -o /dev/null -s http://linux.com/
)

This will generate this output:

Lookup time:    0.005   Lookup time:    1.528
Connect time:   0.387   Connect time:   1.788
PreXfer time:   0.387   PreXfer time:   1.788
StartXfer time: 0.797   StartXfer time: 2.059
=====================   =====================
Total time: 0.797   Total time: 2.059
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Shell programs work by lines - you can move back to the start of the most recent line and rewrite it in bash by printing a carriage return (\r, for example echo -en "print this \roverwrite with this"), but you can't do that for lines before the most recent one.

You won't be able to get multi-line updates to terminal output directly.

Your best option would be to write some secondary program (probably easier to write it in something other than bash scripting) that takes two files and appends each line from the second to the matching line from the first. Then, save each new column to a temporary file, and run that additional program given some base file (starts as empty) and the new temporary file, each time a new column is saved. Fold them all together, and look at the final file after.

Vivian
  • 1,539
  • 14
  • 38
  • note that the answer above could be wrapped in a while loop and `sleep` at the bottom, and it could continue to add columns to the data indefinitely. That is the power of shell/util scripting (`paste` makes this much easier). Agree that at some point the O.P. will have to move the looped file out of the loop and process it a little more, but much easier than writing a custom program (IMHO). Good luck to all. – shellter Sep 22 '16 at 17:14