LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-06-2012, 03:50 AM   #1
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Rep: Reputation: 0
Parse data


Hi
How would you parse the following data in order to extract the "cycle" and the "heat"?
Code:
CYCLE:  3022 TIME: 429.654 TIME LEFT:  3.92D  GRAD.:     3.170 HEAT:-21965.53
CYCLE:  3023 TIME: 335.979 TIME LEFT:  3.92D  GRAD.:     3.817 HEAT:-21965.80
RESTART FILE WRITTEN,      TIME LEFT:  3.91D  GRAD.:     4.586 HEAT:-21965.55
CYCLE:  3025 TIME: 383.594 TIME LEFT:  3.91D  GRAD.:     2.877 HEAT:-21965.67
CYCLE:  3026 TIME: 308.740 TIME LEFT:  3.91D  GRAD.:     3.230 HEAT:-21966.36
What I'm after is a way of generating a list of x/y pairs, such as:

Code:
3022 -21965.53
3023 -21965.80
3024 -21965.55
I would preferably use Bash, but probably this requires a programming language.

Last edited by mzh; 11-06-2012 at 03:51 AM.
 
Old 11-06-2012, 03:55 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
May I ask how did you get the "3024" value in your output? It's not in your input file.
 
Old 11-06-2012, 04:06 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A simple awk one-liner should do the trick:
Code:
awk '/CYCLE/{sub(/HEAT:/,"",$NF); print $2, $NF}'
 
1 members found this post helpful.
Old 11-06-2012, 07:09 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
sed -r -n '/CYCLE/s/CYCLE:  ([0-9]*).*HEAT:(-[0-9]*\.[0-9]{2})/\1 \2/p'
(not totally robust---eg assumes that we always have ": " after CYCLE)
 
Old 11-07-2012, 02:33 AM   #5
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by millgates View Post
May I ask how did you get the "3024" value in your output? It's not in your input file.
Sorry I was not explicit on this. You're right, this is in part exactly the question, "how to get the 3024"?
 
Old 11-07-2012, 02:54 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Well, I don't kno how the file is organized exactly, but I would try to modify colucix's example like this:
Code:
awk '/CYCLE:/{c=$2-1}/HEAT:/{c++;sub(/HEAT:/,"",$NF);print c,$NF}'
 
Old 11-07-2012, 11:30 AM   #7
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by millgates View Post
Well, I don't kno how the file is organized exactly, but I would try to modify colucix's example like this:
Code:
awk '/CYCLE:/{c=$2-1}/HEAT:/{c++;sub(/HEAT:/,"",$NF);print c,$NF}'
hm, yes that counter solves it thanks
 
  


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
[SOLVED] how to parse a data file using bash script rjuny Programming 11 08-20-2012 08:09 PM
How can I configure a shell script which will parse ldap data in for each dn: vikki Linux - Server 2 12-28-2011 07:38 AM
How would you parse query string data in C? 809areacode Programming 3 09-20-2011 01:24 PM
[SOLVED] php: how to parse data into variables? J_Szucs Programming 4 04-17-2011 07:33 PM

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

All times are GMT -5. The time now is 12:37 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