spacup
spacup

Reputation: 21

Grep stops suddenly

I am trying to parse a file in Python, using grep but it always stops at the same line and I am enable to understand why. I tried three different ways :

process = os.popen("grep -A1 "+name+" "+new_hairpins+" | grep -oE '.{0,6}"+seq+".{0,6}'")
results = process.readlines()
process.close()

then

process = subprocess.Popen("grep -A1 "+name+" "+new_hairpins+" | grep -oE '.{0,6}"+seq+".{0,6}'",stdout=PIPE, stderr=PIPE, shell=True)
process.wait()
process_result = process.communicate()
results = filter(None, process_result[0].split("\n"))

and through a temp file

os.system("grep -A1 "+name+" "+new_hairpins+" | grep -oE '.{0,6}"+seq+".{0,6}' > tmp.txt")
with open("tmp.txt","r") as f :
    results = f.readlines()

but the script always fails at the same line. I manually tried this line directly in bash, and it worked....!

So could it be a memory issue from grep and how could I fix this problem ? Thanks a lot !

Upvotes: 1

Views: 92

Answers (2)

spacup
spacup

Reputation: 21

I finally found the problem : my fault !

I realized that the file new_hairpins I used in the grep and that I generated just before in the code wasn't closed with .close()....

As it was working on the 1879 first lines, I didn't think the problem could come from this file...

Anyway, thanks for your help guys!

Upvotes: 0

falsetru
falsetru

Reputation: 369384

You need to quote command line argument because there's a space in between:

"grep -A1 '"+name+" "+new_hairpins+"' | grep ....
          ^                         ^

Otherwise, name, new_hairpins will be treated as separated arguments.

Upvotes: 2

Related Questions