LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I parse the output of a command that output to standard out? (https://www.linuxquestions.org/questions/programming-9/how-do-i-parse-the-output-of-a-command-that-output-to-standard-out-611799/)

gauol 01-07-2008 12:59 PM

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)

Poetics 01-07-2008 01:04 PM

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).

gnashley 01-07-2008 02:01 PM

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.

gauol 01-15-2008 09:28 AM

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

gauol 01-15-2008 09:29 AM

ps thanks for all the help so far and future

theNbomr 01-15-2008 10:00 AM

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.

ghostdog74 01-15-2008 10:26 AM

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


gauol 01-15-2008 10:43 AM

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!

PAix 01-15-2008 11:15 AM

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.

gauol 01-17-2008 01:54 PM

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.


All times are GMT -5. The time now is 05:45 PM.