[CentralOH] parsing Windows command line output
William McVey
wam at cisco.com
Wed Jul 23 17:47:49 CEST 2008
On Wed, 2008-07-23 at 09:30 -0400, Mark Peters wrote:
> Use the default option to split:
The problem is that some of his fields (e.g. first and second) have
embedded whitespace in the actual field values. If his second column
didn't fluctuate between spaces and non-spaces, he would have been able
to do something like:
>>> line= "Quest SQL Watch Agent ORIONMFG1 APPS MFGSQL-S1A Online"
>>> line.rsplit(None, 4)
['Quest SQL Watch Agent', 'ORIONMFG1', 'APPS', 'MFGSQL-S1A', 'Online']
However, that breaks on lines like:
>>> line= "Cluster IP Address ORIONMFG-SAN1 MFGSQL-S1A Online"
>>> line.rsplit(None, 4)
['Cluster IP', 'Address', 'ORIONMFG-SAN1', 'MFGSQL-S1A', 'Online']
Perhaps ORIONMFG-SAN1 is a special case and could be mapped to "ORIONMFG SAN1":
>>> line.replace("ORIONMFG-SAN1", "ORIONMFG SAN1").rsplit(None, 4)
['Cluster IP Address', 'ORIONMFG', 'SAN1', 'MFGSQL-S1A', 'Online']
Ordinarily, parsing columnar output screams for string slices; however
your format is particularly nasty in that the columns are *mostly* fixed
width, but will (on a case by case basis) overflow the fixed width when
a field is too long and turn into a delimited output format with an
ambiguous field delimiter. A good example of this is that line that
starts with "Quest SQL Watch Agent". If it weren't for the possibility
of field overflow, you could do something like:
for line in file("qry1.txt").readlines():
fields = line[:21], line[21:42], line[42:58], line[58:]
resource, group, node, status = [x.strip() for x in fields]
As it happens, this routine *will* parse the output you provided, but be
aware that any resources that are longer than 21 characters will likely
cause the routine to choke. Unless I knew that there was no possibility
of field overflow, I'd try to stick to the rsplit() option, even if it
requires a hack to map particular values into a more accommodating
format.
-- William
P.S. I'd be really surprised if there is something that the cluster
command is doing that couldn't be done directly within python using the
pywin32 api.
More information about the CentralOH
mailing list