0

I did some benchmarks and the output is a single file containing multiple results with the following format

[blaaa]
1st run

T/V   N  NB  P  Q   Time
==========================
2     3   4  1  2   1.02
[blaaaa...]
2nd run
T/V   N  NB  P  Q   Time
==========================
4     42 4  1  2    1.22

I want only to obtain the runtimes of the different runs I made. So is it possible to grep "Time" and print the next but one column? Or even to print out only

1.02
1.22

for my example above? I would prefer printing out only the three result columns without the blaa part :D

  • You mean the same column, next but one line? – steeldriver Apr 13 at 19:18
2

Assuming Time is always in the sixth field:

$ awk '$6 == "Time" {t = NR} t && NR == t+2 {print $6}' file
1.02
1.22

Alternatively you could use getline twice - quick'n'dirty:

awk '$6 == "Time" {getline; getline; print $6}' file

("dirty" because it doesn't check the getline return value).

  • Or use $NF if it's always the last column. – Kusalananda Apr 13 at 19:24
  • @Kusalananda yes good point – steeldriver Apr 13 at 19:25
0
$ grep -o 'Time\|[0-9\.]\+$' file
Time
1.02
Time
1.22
$ grep -o '[0-9\.]\+$' file
1.02
1.22
$ grep -A2 ^T/V file
T/V   N  NB  P  Q   Time
==========================
2     3   4  1  2   1.02
--
T/V   N  NB  P  Q   Time
==========================
4     42 4  1  2    1.22

The results are separated by -- in the last grep which can be removed with:

$ grep -A2 ^T/V file | grep -v '^--'

And with the ==== lines removed:

$ grep -A2 ^T/V file | grep -v '^--\|=\+'
0

You got two answers using awk and grep, you can also try sed:

sed -E '/===*/{N;s/.*([0-9]+\.[0-9]*)/\1/g};t;d' file_name

Here /===*/, will match ===.., then it will go to next line and find floating point numbers having decimal in between them, and will skip and delete all other lines.

It will print output like:

1.02
1.22

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.