0

I have files with naming convention

st009_out.abc1.dat
st009_out.abc2.dat
st009_out.abc3.dat 
..................
..................

I am writing Python code where I want to use data from the file to perform a math function and need to extract the second column from the file. I have tried it this way:

for k in range(1,10):
    file1=open('st009_out.abc'+str(k)+'.dat','r')
    ...........
    os.system("awk '{print $2}' st009_out.abc${k}.pmf > raj.dat")

but this is not working as it is not taking the value of k in the shell command.

How do I progress?

2 Answers 2

2

Try

os.system("awk '{print $2}' st009_out.abc"+str(k)+".pmf > raj.dat")
Sign up to request clarification or add additional context in comments.

2 Comments

@rajitha: Also, the split function in Python does exactly what awk does in this example.
Thank you.I got it using split function earlier, as i have been trying to use shell command in python and was stuck at this point. thank you once again for ur help.
1

You use the filename twice so set it only once

for k in range(1,10):
    name = 'st009_out.abc'+str(k)+'.dat'
    file1=open(name,'r')
    ...........
    os.system("awk '{print $2}' " + name  + " > raj.dat")

or better rewrite the awk in python

Comments

Your Answer

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