Returning multiple outputs of a single line based on previous repeated lines

Hello,
I am trying to return a time multiple times from a file that has varying output just before the time instance, i.e.

cat jumped
cat jumped 
cat jumped
time = 1.1
cat jumped
cat jumped
time = 1.2
cat jumped
cat jumped
time = 1.3

In this case i would like to output a time.txt file which would show

1.1
1.1
1.1
1.2
1.2
1.3
1.3

My current command only returns the single instance of the time result. Any help in returning the desired result would be great.

Note that i have tried both

awk '/time/ { time = $3 }; {print time };' inputfile >& time.txt

and the more simple,

grep 'time' inputfile | cut -d' ' -f3 >& time.txt

For exactly the sample given, try

awk '{CNT++} /time/ {for (;--CNT>0;) print $3}' file
1.1
1.1
1.1
1.2
1.2
1.3
1.3
1 Like

That works great! now i am trying to add an if statement to the beginning which will allow to choose which word is counted in the line

cat jumped
dog
cat jumped 
cat jumped
time = 1.1
cat jumped
cat jumped
dog
time = 1.2
cat jumped
cat jumped
dog
time = 1.3
1.1
1.1
1.1
1.2
1.2
1.3
1.3

so that dog is not counted and only cat is
I think it needs to be written, using RudiC's style

awk '{if($1=='cat') CNT++} /time/ {for (;--CNT>0;) print $3}' file

but this returns an error.

Use double quotes instead of single quotes around cat

1 Like

Try

awk '/cat/ {CNT++} /time/ {for (;--CNT>0;) print $3}' file 
1 Like

Try:

awk '/cat|time/{c++} /time/{while (--c>0) print $3}' file
1 Like

Scrutinizer, that last one works perfect, the other two were missing one line from each section some how. Thanks a bunch!