0

I am trying to port a shell script to python. Used various methods from google search, but none of them seems to work.

This is the shell command.

version=`awk '{print $5}' file_name | tr -d ")" `

These are the methods tried.

version = subprocess.call(['awk','{print $5}','file_name','|','tr','-d'], shell=True)
version = os.system("`awk '{print $5}' file_name | tr -d ")" `", shell=True)
version = commands.getstatusoutput(" awk '{print $5}' file_name | tr -d ")"  ")

Neither of the above commands worked. Could someone please help me with it.

2
  • 2
    If your idea of "porting" a shell script to Python is to wrap the shell calls up with os.system() then there's pretty much no point in the exercise whatsoever. Commented Apr 18, 2018 at 18:37
  • I agree with you. But, initially I wanted to make the script run as a python script and later port each possible function to use python library. It does look like a double work, I'll now try directly to use python instead of shell calls(I am new to using python, but that's no excuse I guess). Commented Apr 19, 2018 at 0:49

1 Answer 1

2

Your examples have various quoting errors.

The most straightforward solution would be:

subprocess.call('awk \'{print $5}\' file_name | tr -d ")"', shell=True)

but that's not recommended, because file_name can contain spaces (you can get around by shell=False and proper use of list of arguments, but it's getting unreadable).

I'd suggest str.replace(')', '') insted of tr -d, and something likex.split()[4] for x in open('file_name') instead of awk, to get a pure python version.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.