LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Process multiline input with awk (https://www.linuxquestions.org/questions/programming-9/process-multiline-input-with-awk-4175486240/)

greyarea 11-29-2013 06:11 AM

Process multiline input with awk
 
Hello, I've been trying this and I'm stuck. I've tried to search for awk multiline record processing but all the examples I've seen use a blank line as the field seperator.

Given the following input:

Time: 131127 13:41:50
Query_time: 12.249749
Time: 131127 13:41:59
Query_time: 5.156203
Time: 131127 13:42:46
Query_time: 32.596008
Time: 131127 13:44:30
Query_time: 99.876977
Time: 131127 13:45:20
...
...

I want to extract fields 2 & 3 from the first line and field 2 from the second line, and repeat until EOF.

Can somebody suggest an approach as I'm at a loss.

Regards,

grail 11-29-2013 07:13 AM

How about using, when /Time/ print 2 & 3 and when /Query/ print just 2 :)

danielbmartin 11-29-2013 07:31 AM

With this InFile ...
Code:

Time: 131127 13:41:50
Query_time: 12.249749
Time: 131127 13:41:59
Query_time: 5.156203
Time: 131127 13:42:46
Query_time: 32.596008
Time: 131127 13:44:30
Query_time: 99.876977

... this awk ...
Code:

awk '{printf $2" "$3" "; getline; print $2}' $InFile >$OutFile
... produced this OutFile ...
Code:

131127 13:41:50 12.249749
131127 13:41:59 5.156203
131127 13:42:46 32.596008
131127 13:44:30 99.876977

Daniel B. Martin


All times are GMT -5. The time now is 09:34 PM.