tags:

views:

113

answers:

2

I am trying to call gawk (the GNU implementation of AWK) from Python in this manner.

import os
import string
import codecs

ligand_file=open( "2WTKA_ab.txt", "r" ) #Open the receptor.txt file
ligand_lines=ligand_file.readlines() # Read all the lines into the array
ligand_lines=map( string.strip, ligand_lines ) 
ligand_file.close()

for i in ligand_lines:
    os.system ( " gawk %s %s"%( "'{if ($2==""i"") print $0}'", 'unique_count_a_from_ac.txt' ) )

My problem is that "i" is not being replaced by the value it represent. The value "i" represents is an integer and not a string. How can I fix this problem?

A: 

Your problem is in the quoting, in python something like "some test "" with quotes" will not give you a quote. Try this instead:

os.system('''gawk '{if ($2=="%s") print $0}' unique_count_a_from_ac.txt''' % i)
WoLpH
Thanks a ton!!! Solved :)
forextremejunk
+4  A: 

That's a non-portable and messy way to check if something is in a file. Imagine you have 1000 lines, you will be making system call to gawk 1000 times. It's super inefficient. You are using Python, so do them in Python.

....
ligand_file=open( "2WTKA_ab.txt", "r" ) #Open the receptor.txt file
ligand_lines=ligand_file.readlines() # Read all the lines into the array
ligand_lines=map( str.strip, ligand_lines ) 
ligand_file.close()
for line in open("unique_count_a_from_ac.txt"):
    sline=line.strip().split()
    if sline[1] in ligand_lines:
         print line.rstrip()

Or you can also use this one liner if Python is not a must.

gawk 'FNR==NR{a[$0]; next}($2 in a)' 2WTKA_ab.txt  unique_count_a_from_ac.txt
ghostdog74
+1 AMEN! Do they not show people the costs of process creation in school anymore?!
JUST MY correct OPINION