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?

Unix & Linux Asked on November 21, 2021

1 AnswersIf 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
```

Answered by AdminBee on November 21, 2021

3 Asked on August 10, 2020 by ssiresha-garimella

1 Asked on August 9, 2020 by akhil-jalagam

0 Asked on August 6, 2020 by chloe

5 Asked on August 4, 2020 by ripper234

1 Asked on August 3, 2020 by mishal-ahmed

1 Asked on July 29, 2020 by mmaluff

Get help from others!

Recent Answers

- lab bhattacharjee on Constraints on $x^2 + b x + c = 0$ such that at least one root has a positive real part
- Ethan Bolker on A is greater than B by 25% then by what percentage B is less than A?
- Dominique on A is greater than B by 25% then by what percentage B is less than A?
- zkutch on A is greater than B by 25% then by what percentage B is less than A?
- NeitherNor on How to segment a group of symmetric points

Recent Questions

- Constraints on $x^2 + b x + c = 0$ such that at least one root has a positive real part
- Picking the set of coset representatives of a stabilizer under a group action
- Write condition if rectangle is inside another rectangle
- How to segment a group of symmetric points
- Prove $D(A):= {win H^{2}(0, 2pi): w(0) = w(2pi), w^{‘}(0) = w^{‘}(2pi) }$ is dense in $L^{2}(0,2pi).$

© 2021 InsideDarkWeb.com. All rights reserved.