0

A file has a line pattern where a line that does not contains numbers occurs twice in the file.

Energy initial, next-to-last, final = 
 -87057.8200168321  -87208.7223900015  -87208.7197287879
Energy initial, next-to-last, final = 
 -87208.7197287879  -87208.7223900015  -87310.7197287879

I want to extract value -87310.7197287879 which is the value at the end of the line following the 2nd occurrence of the line that does not contain numbers.

How can I do this with Awk or Sed ?

2
  • awk 'END{print $NF}' file? Commented Nov 24, 2021 at 21:26
  • please edit and add more of your given sample input when you are talking about _repeated lines _, otherwise for the given input I would simply suggest to use awk 'NR%4==0 { print $NF }' infile Commented Nov 25, 2021 at 6:04

3 Answers 3

1
awk '/^Energy initial, next-to-last, final =/ 
    { if (secondline==0) { secondline=1; next; } else { getline; print $3; } }' inputfile

-87310.7197287879

1
  • Thanks, I edited this with awk '/Energy/{ if (secondline==0) { secondline=1; next; } else { getline; print $3; } }' and it worked. Commented Nov 24, 2021 at 21:18
0

An alternative in case you have such patterned occurrence happening once or more than once in your file:

$ awk '/^Energy initial, next-to-last, final =/ {a[NR]=1} 
       {if(NR >= 3 && a[NR-2] == a[NR] && a[NR] == 1) {toprint=1; next}}
       {if (toprint) {print $3; toprint=0}}' infile

No matter, Hauke's solution is far less ugly.

-2

Just a quicky:

> awk '{print $3}' /tmp/test.txt | grep -m1 -vE '[a-z]'
-87208.7197287879

The awk command prints the 3rd item ($3) from /tmp/test.txt and do a grep that does NOT match any letters ([a-z]) which eleminates the lines with text and stop after 1 selected lines (-m1)

1
  • No downvote from me. I guess you got the downvotes because the solution is exceptionally ugly. Commented Nov 24, 2021 at 21:38

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.