0

hi,

can you please help me with a sample python code to retrive data from command prompt which appears after execution of some commanad

for eg: ./vershion.sh
gives ouput as

version: 7.0.0.9
type : NAS

thanks in advance

7
Contributors
12
Replies
125
Views
3 Years
Discussion Span
Last Post by manohar1111
Featured Replies
  • You can use the Command class in [this code snippet](http://www.daniweb.com/software-development/python/code/257449/a-command-class-to-run-shell-commands). You would write com = Command("./vershion.sh").run() if com.failed: print(com.error) else: print(com.output) Read More

  • > if there are more number of parameters, out of which we need to print version and type only > > can you please help with the code to get out this You have to post an example of how the other parameters look. There are many ways to do … Read More

2

You can use the Command class in this code snippet. You would write

com = Command("./vershion.sh").run()
if com.failed:
    print(com.error)
else:
    print(com.output)

Edited by Gribouillis

0

thanks a lot

this code works good but i need only some of the fields in output like version,type,etc,.

can you please help me with text parsing of required fields.

0

Well, you can use split()

s = """
version: 7.0.0.9
type : NAS
"""

lines = s.strip().split("\n")
lines = [tuple(x.strip() for x in y.split(":")) for y in lines]

print(lines)

""" my output -->
[('version', '7.0.0.9'), ('type', 'NAS')]
"""

Edited by Gribouillis

0

thank you for the code

here we assumed to get all the parameters in the output.
if there are more number of parameters, out of which we need to print version and type only

can you please help with the code to get out this

1

if there are more number of parameters, out of which we need to print version and type only

can you please help with the code to get out this

You have to post an example of how the other parameters look.
There are many ways to do this,if data is unformatet/changing regex can be and option.

import re

data = '''\
abc : 123
version: 7.0.0.9
type : NAS
ggg.2 : 4444
version: 8
type : FOO
hg.1234: test'''

result = re.findall(r"version.*|type.*", data)
print result
print zip(*[iter(result)]*2)
print [tuple(x.strip() for x in y.split(":")) for y in result] 

"""Output-->
['version: 7.0.0.9', 'type : NAS', 'version: 8', 'type : FOO']
[('version: 7.0.0.9', 'type : NAS'), ('version: 8', 'type : FOO')]
[('version', '7.0.0.9'), ('type', 'NAS'), ('version', '8'), ('type', 'FOO')]
"""

Edited by snippsat

0

here is the screen shot

Product List
--------------------------------------------------------------------------------




WXSI                      installed




WXDOPI                    installed
NDI                       installed

Installed Product
--------------------------------------------------------------------------------
Name                     eXtreme Scale
Version                  8.0.0.0
ID                       WXS
Build Level              cd11
Build Date               6/11/11




Architecture             AMD (64 bit)

Installation
--------------------------------------------------------------------------------
Product Directory        /abcd
Version Directory        /abcd/properties/version
DTD Directory            /abcd/properties/version/dtd
Log Directory            /abcd/logs
Backup Directory         /abcd/properties/version/nif/backup
TMP Directory            /tmp

from this i need version,id,architecture,Product Directory

please help me

-1

i had ran your script but it is not giving corrct results.

is there any other way to get required fields, can it is possible with awk command

if possible give me sample code

0

hello sir,

i have doubt that how to redirect the ouput text of a command (while working on unix cmd prompt) to file using python.please help me with sample code.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.