LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 01-07-2008, 12:59 PM   #1
gauol
LQ Newbie
 
Registered: Jun 2002
Location: NH
Distribution: Kubuntu
Posts: 24

Rep: Reputation: 15
How do I parse the output of a command that output to standard out?


I am currently trying to take the output, written to standard out, of one of our programs and reformat it in a way that is more useful to those in my group. It has proven difficult to get the actual program re-written (the usual delays) so I believe I should able to accomplish this via a shell script. The one restriction I have is that this will using Busy Box with a limited toolset.

Anyway the real point of this question is figure out… When doing this type of parsing via a shell script do I need to send the output to a file a then parse that or can I parse it directly from standard out?

I have a background in programming but haven’t used in some time so its time get into it. I see this as a both easy (the hard work is done via the program) and hard (I will have to do conditional testing to more the data correctly).

Many Thanks (if you need more info, I'm sure you will, just let me know)
 
Old 01-07-2008, 01:04 PM   #2
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,178

Rep: Reputation: 49
You can merely pipe the output to a new command (awk or sed, for instance), or you can wrap the entire script process within another script (such as a perl script that can parse the output using backticks).
 
Old 01-07-2008, 02:01 PM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,746

Rep: Reputation: 458Reputation: 458Reputation: 458Reputation: 458Reputation: 458
For very short output you can just use something like this:
VALUE="$(your_command)"
For mulitple lines you'll find it easier to use a pipe or redirect the output to a file for further processing. Sometimes it's useful to use the 'tee' program for this, if you want to see the output as well as put it in a file.
 
Old 01-15-2008, 09:28 AM   #4
gauol
LQ Newbie
 
Registered: Jun 2002
Location: NH
Distribution: Kubuntu
Posts: 24

Original Poster
Rep: Reputation: 15
I think I could use an assist, its been so long since I tried any of this, the output of the program looks like this

devid=0x5a2f
swtguud=4446k10800418brr
chiswt 24 "4446k10800418brr" # "swt90is24rs-m Vold" vmaliid 1

devid=0x5a2f
swtguud=4446k10800418brr
chiswt 24 "4446k10800418brr" # "swt90is24rs-m Vold" vmaliid 1

it can get much larger depending on the number of nodes. What like to do is send all this output to a file in the format of just one line.

devid=0x5a2f,swtguud=4446k10800418brr,chiswt 24 "4446k10800418brr",# "swt90is24rs-m Vold" vmaliid 1

How might I go about this?

I see this as the first step and afterwards I will parse this file for the output I need
 
Old 01-15-2008, 09:29 AM   #5
gauol
LQ Newbie
 
Registered: Jun 2002
Location: NH
Distribution: Kubuntu
Posts: 24

Original Poster
Rep: Reputation: 15
ps thanks for all the help so far and future
 
Old 01-15-2008, 10:00 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Assuming that your data records are separated by double newlines as you showed, this should work:
Code:
yourProgram | perl -e '$/ = "\n\n"; while( <> ){ @rec=split /\n/, $_; print join(",", @rec), "\n"; }'
--- rod.
 
Old 01-15-2008, 10:26 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
GNUawk
Code:
# awk 'BEGIN{RS="";OFS=","} {$1=$1}1' file
devid=0x5a2f,swtguud=4446k10800418brr,chiswt,24,"4446k10800418brr",#,"swt90is24rs-m,Vold",vmaliid,1
devid=0x5a2f,swtguud=4446k10800418brr,chiswt,24,"4446k10800418brr",#,"swt90is24rs-m,Vold",vmaliid,1
 
Old 01-15-2008, 10:43 AM   #8
gauol
LQ Newbie
 
Registered: Jun 2002
Location: NH
Distribution: Kubuntu
Posts: 24

Original Poster
Rep: Reputation: 15
theNbomr
Unfortunately perl is not available on this system thanks though

ghostdog74
worked beautifully but I forgot that the output changes so i will have to re think this. Many thanks!
 
Old 01-15-2008, 11:15 AM   #9
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
OK, so an oversight isn't exactly the end of the world.

If you share your output, trying to make sure that it's truely representative of your file, then we can have a bit of a think about it while you are doing the same. That way if you come up empty or with a less than optimal solution then there might be a ready solution to your problem all thought out ahead of time.
 
Old 01-17-2008, 01:54 PM   #10
gauol
LQ Newbie
 
Registered: Jun 2002
Location: NH
Distribution: Kubuntu
Posts: 24

Original Poster
Rep: Reputation: 15
well after toying around the the system i found that the sys already has a cvs file with this info in it so there is no need for me to reinvent the wheel. Now have to develop a script to parse this file. Thanks for the help and I sure I'll be back when I dig into that.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
command to sort by floats in fourth column of standard output jhwilliams Linux - Software 4 06-22-2007 01:27 PM
can't capture 'at' command standard output to a file bartoni Linux - General 3 02-28-2006 03:52 PM
how to pipe and parse output of a command learnfast Linux - Newbie 2 06-15-2005 04:55 AM
How to redirect standard output of piped command to log file andrewstr Linux - General 10 02-04-2004 02:07 PM
parse output ky-lab_rat Linux - Newbie 7 01-23-2004 01:47 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration