[Gllug] parsing command output

Stuart Sears stuart at sjsears.com
Wed Feb 14 19:33:42 UTC 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

salsaman wrote:
> Hi all,
> I need a little bash script that should do the following -
> run a program and parse the output on stderr. It needs to write the
> fourth field of the 7th and 8th lines to a file. Fields are separated by
> one or more spaces and lines can also start with a space.
> 
> E.g. if the output of command "foo" on stderr is:
> 
> 1abc
> 2def
> 3abc
> 4def
> 5abc
> 6 mmmm
> 7abc    123    56 1024    xxx
> 8abc    123     56  768    yyy
> 9 aaa
> 
> then the result in out.txt should be:
> 1024
> 768
ripping off someone else's approach...
this is supposed to only send stderr through the pipe...
seems to work here.

command 3>&1 2>&3 > /dev/null | sed -n '7,8p'| awk '{print $4}' > outputfile

notes:

1. I found a lot of interesting stuff about I/O redirection thing about here

http://www.cpqlinux.com/redirect.html

although this is also interesting
http://www.linux.com/guides/abs-guide/ioredir

I sort of understand it, but I am probably going to have to go back and
read it all again a few times.

2. I am no awk expert (to say the least. I am not even an awk amateur.),
but it is outstanding at pulling out 'random' fields separated by
whitespace.
perhaps someone can enlighten me as to how I make it only print lines 7
and 8?
There has to be a way to do this. I'm fairly sure you could probably
write an entire adventure game in awk. or possibly even doom. :)

Regards

Stuart

- --
Stuart Sears RHCA RHCSS RHCX PEBKAC STFU
"Quit worrying about your health. It'll go away."
- - Robert Orben
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iD8DBQFF02QVamPtx1brPQ4RArmIAJ4tWy49kYPjtFhefAWuOGyFMEpUcwCeIQdz
GCb4aQjY9qIwlQ5SElD1P5c=
=Hi2Y
-----END PGP SIGNATURE-----
-------------- next part --------------
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug


More information about the GLLUG mailing list