0

I have a log file with this pattern

Time = 100

GAMG:  Solving for p, Initial residual = 0.2174470886, Final residual = 0.01001676933, No Iterations 1
GAMG:  Solving for p, Initial residual = 0.02692910633, Final residual = 0.0004864615367, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.005753534485, Final residual = 0.0001502172182, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.001526301953, Final residual = 5.384448346e-05, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.0004529702305, Final residual = 1.80754637e-05, No Iterations 1
time step continuity errors : sum local = 2.722585271, global = 0.09763545089, cumulative = 0.09763545089
ExecutionTime = 27.35 s  ClockTime = 31 s

Time = 101

GAMG:  Solving for p, Initial residual = 0.04799119366, Final residual = 0.002390404161, No Iterations 1
GAMG:  Solving for p, Initial residual = 0.02803010067, Final residual = 0.0009371130484, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.007210353379, Final residual = 0.0002494798057, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.002410795933, Final residual = 0.000116871876, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.0007431402835, Final residual = 1.649739876e-05, No Iterations 3
GAMG:  Solving for p, Initial residual = 0.0003489407136, Final residual = 1.262095062e-05, No Iterations 1
time step continuity errors : sum local = 0.7142966337, global = -0.1125594528, cumulative = -0.01492400189
ExecutionTime = 41.42 s  ClockTime = 45 s

Time = 102

The line after Time = is always Iterations 1 and I want to get get that line. I have tried these ideas:

With grep, if I write grep -A 2 "Time =" test.txt, it will show

Time = 100

GAMG:  Solving for p, Initial residual = 0.2174470886, Final residual = 0.01001676933, No Iterations 1

and

ExecutionTime = 27.35 s  ClockTime = 31 s

Time = 101

If I use awk like cat test.txt | awk '/Time/{getline;getline;print}' it will show

GAMG:  Solving for p, Initial residual = 0.2174470886, Final residual = 0.01001676933, No Iterations 1
Time = 101
Time = 102

Those Time = 101 and Time = 102 are the two lines after ExecutionTime.

Briefly, I only want to get the first non-empty line after Time = which I know it is always Iterations 1. How can I get that?

0
3

If you want to be absolutely sure that you print the first line ending in No Iterations 1 after a Time = ... line, you could use the following awk command:

awk '/^Time =/ {f=1} /No Iterations 1$/ && f {f=0; print}' test.txt

This awk command has two rules ({ ... }).

  • The first one is processed if the line matches the regular expression ^Time =, i.e. it starts with the string Time =. In this rule, we simply set a flag f to 1 to indicate that the "start line" of the pattern was found.
  • The next one is processed if the line matches the regular expression No Iterations 1$, i.e. it ends with the string No Iterations 1, and we have previously found the Time =-line. If both conditions are met, the line is printed, and the flag f reset so that we don't print any of the following lines that end in No Iterations 1.

A more "relaxed" variant, which simply prints the first non-empty line after the Time = ... statement (not checking if it contains the No Iterations 1 pattern) would be:

awk '/^Time =/ {f=1; next} NF>0 && f {f=0; print}' test.txt

This simply looks if the flag f is set, and that NF, the "number of fields" (or columns, if you will) in the line is larger than zero. Note that here, we have to add a next command in the first rule to skip execution to the next line, as otherwise the NF>0-rule would already be processed for the Time =-lines, effectively printing only these.

For your example, both print:

GAMG:  Solving for p, Initial residual = 0.2174470886, Final residual = 0.01001676933, No Iterations 1
GAMG:  Solving for p, Initial residual = 0.04799119366, Final residual = 0.002390404161, No Iterations 1
1
  • Yes thank you. I am curious to know how that is achieved. The cyntax is rather odd... I appreciate if you explain that in the answer. – mahmood Jul 22 '20 at 7:57

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.