How to extract multiple values from a file in a single pass?
sed -n '/^Max/ { s/^.*=\s*//;h; };
/^Time/{ s/^.*=\s*//;G; s/\n/ /;p; }' infile
match-run syntax
/.../{ ... }
:
commands within{...}
will only run on the lines that matched with regex/pattern within/.../
;s/^.*=\s*//
:
deletes everything up-to last=
and whitespaces\s*
also if there was any.h
:
copy the result into hold-spaceG
:
append the hold-space to pattern-space with embedded newlines/\n/ /
:
replace that embedded newline with space in the pattern-spacep
:
print pattern-space; you can useP
command here instead too.0.000001 3.0355 0.000013 4.3644 0.000025 3.7319 1.32125 7.0695
A similar approach proposed by @stevesliva that is used s//<replace>/
which is shorthand to do substitution on the last match:
sed -n '/^Max.*=\s*/ { s///;h; };
/^Time.*=\s*/{ s///;G; s/\n/ /;p; }' infile
I can't guarantee it will be faster, but you could do something like this in awk:
awk -F' = ' '$1=="Max value of omega" {omega = $2} $1=="Time" {print omega,$2}' file
$ awk 'BEGIN{print "#time", "omega"} /^Max value of omega =/{omega=$NF; next} /^Time =/{print $NF, omega}' file
#time omega
0.000001 3.0355
0.000013 4.3644
0.000025 3.7319
1.32125 7.0695
but this will probably be faster:
$ grep -E '^(Max value of omega|Time) =' file |
awk 'BEGIN{print "#time", "omega"} NR%2{omega=$NF; next} {print $NF, omega}'
#time omega
0.000001 3.0355
0.000013 4.3644
0.000025 3.7319
1.32125 7.0695