Need help translating from Shell to Python

This is the place for queries that don't fit in any of the other categories.

Need help translating from Shell to Python

Postby preston3271 » Thu Jun 06, 2013 9:50 pm

This is a bourne shell script
Code: Select all
/opt/OV/bin/OpC/utils/opcnode -list_groups |grep "Name" |awk ' {print ( $3 ) }' |while read NodeGrp
do

  /opt/OV/bin/OpC/utils/opcnode -list_ass_nodes group_name=NodeGrp |grep "Name" |awk ' {print ( $3 ) }' |xargs -I {} echo {} >> /var/opt/OV/shared/server/ov_admin/bin/data/new_NodeGrp
mv /var/opt/OV/shared/server/ov_admin/bin/data/new_NodeGrp /var/opt/OV/shared/server/ov_admin/bin/data/NodeGrp

done


I need help understanding how to use shell commands and awk type function within Python 3 to do the same. The examples in the "Learning Python and Python Cookbook," are not cutting it. I have seen the use of "import subprocess" to run shell commands, but I do see the awk equivalent other than the regular expression "re." And what is worse is that I write Perl script and I am trying to learn Python 3. Man! Not apples to apples here. Maybe apples to watermelons. :)
~
~
~
~
~
~
~
~
preston3271
 
Posts: 1
Joined: Thu Jun 06, 2013 9:25 pm

Re: Need help translating from Shell to Python

Postby ochichinyezaboombwa » Thu Jun 06, 2013 10:01 pm

awk in your simple case just extracts a column from each line of the input. The python equivalent would use the split() function (similar to Perl). The input (from the pipe) is read like this:
Code: Select all
for line in sys.stdin:
    ....
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: buran and 5 guests