LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-29-2013, 06:11 AM   #1
greyarea
LQ Newbie
 
Registered: Jan 2008
Distribution: Centos5/RHEL5
Posts: 27

Rep: Reputation: 0
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,
 
Old 11-29-2013, 07:13 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
How about using, when /Time/ print 2 & 3 and when /Query/ print just 2
 
Old 11-29-2013, 07:31 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiline and complex text input for bash/ awk/perl help btacuso Linux - Newbie 5 03-06-2013 11:44 AM
[SOLVED] Calling sort from within awk on multiline input sarenace Programming 5 11-24-2012 07:07 AM
Extarct tags with multiline values from XML file using sed/Awk gbms Linux - Newbie 3 03-27-2012 10:18 AM
[SOLVED] Awk & multiline webhope Programming 9 05-24-2010 03:03 AM
user input howto using awk cmontr Programming 11 09-29-2007 07:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration