0

I have a results file that contains:

==============================================
------------------------------
Begin SimulationInterface Evaluation    1
------------------------------
Parameters for evaluation 1:
                     -1.8961789208e+00 x1
                     -1.3853582017e+00 x2

Direct interface: invoking function 

Active response data for SimulationInterface evaluation 1:
Active set vector = { 1 }
                      2.4892772154e+03 response_fn_1

------------------------------
Begin SimulationInterface Evaluation    2
------------------------------
Parameters for evaluation 2:
                      3.7988695564e-01 x1
                      1.5859091288e+00 x2

Direct interface: invoking function 

Active response data for SimulationInterface evaluation 2:
Active set vector = { 1 }
                      2.0820416317e+02 response_fn_1

==================================

Now I want to extract only the integers x1, x2, and response_fn_1 from the results file to a separate file, say test. To extract lines with x1 and x2, I have:

sed -n '/Parameters for evaluation/{n;p;n;p}' initial.log

How do I transfer the integers from this line to an output test file so that x1 and x2 are in tabular form as:

x1                     x2
-1.8961789208e+00      -1.3853582017e+00
3.7988695564e-01        1.5859091288e+00
  • How should the output of response_fn_1 look like? – Freddy Aug 26 '19 at 0:08
  • 1
    That will also get extracted from the above results file such that the output 'test' file to be generated will have three columns (x1, x2, and response_fn). – AeroGuy5 Aug 26 '19 at 0:15
  • 1
    Please add these details to the question. Thanks! – Freddy Aug 26 '19 at 0:17
2

I'd suggest using awk for the whole thing:

$ awk '
    BEGIN {OFS="\t"; print "x1", "x2", "response_fn_1"} 
    $2 == "x1" {x1 = $1} 
    $2 == "x2" {x2 = $1} 
    $2 == "response_fn_1" {print x1, x2, $1}
' file | column -t
x1                 x2                 response_fn_1
-1.8961789208e+00  -1.3853582017e+00  2.4892772154e+03
3.7988695564e-01   1.5859091288e+00   2.0820416317e+02

The pipe through column -t is only for pretty-printing - the awk output itself is tab-delimited

| improve this answer | |
  • Thank you. Exactly what I wanted. – AeroGuy5 Aug 26 '19 at 0:55
0

If your results file is called 'input'

for i in x1 x2 response_fn_1 ; do echo $i > $i; grep $i input | awk '{print $1}' >> $i ; done ; paste x1 x2 response_fn_1 | column -t

gives:

x1                 x2                 response_fn_1
-1.8961789208e+00  -1.3853582017e+00  2.4892772154e+03
3.7988695564e-01   1.5859091288e+00   2.0820416317e+02

Note! This destructively creates files named x1 x2 and response_fn_1 in the directory you're working in. If these are real files they will be overwritten.

| improve this answer | |
0

We can do this using Perl in slurp mode and split the file into array around the line beginning with Begin (you may embellish it to suit your requirements). Then from each array element, save the first (zeroth in Perl), we detect lines with two fields only and save those in a hash %h keyed on the second field => value as first field. This we get by reversing the matches we get from the regex. And then we print.

$ perl -F'/^Begin.+$/m' -ln -0777 -ae'
    ($,, @A) = ($", qw/x1 x2 response_fn_1/);
    for(@F) {
        my %h = reverse /^\s*(\S+)\s+(\S+)$/mg;
        print(@A),next if ! $a++;
        print @h{@A};
    }
  ' initial.log | column -t
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.