http://docs.python.org/library/subprocess.html
[1] using subprocess
e.g. subprocess.call( ["grep", "-w", "query","file"])
e.g. subprocess.call( ["grep", "-w", "query","file"])
import subprocess
import os
import os
from subprocess import Popen
from subprocess import PIPE
from subprocess import PIPE
q1 = "xxx"
q2 = "yyy"
file = "file
##
# grep -w q1 file | grep q2
#
p1 = Popen(["grep","-w",q1,file], stdout=PIPE)
p2 = Popen(["grep", q2], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]
print output
[2] using os.system
import os
cmdln = "grep -w " + q1_c + " " + db + " | grep -w " + q2_c + "| awk -F\"\t\" '{print $1}' "
os.system( cmdln )
---> this is not "quiet mode". quite problematic
[3] using commands
cmdln = "grep -w " + q1_c + " " + db + " | grep -w " + q2_c + "| awk -F\"\t\" '{print $1}' "
out = commands.getstatusoutput( cmdln ) # or simply getoutput
status = out[0] # this is error code
bingo = out[1] # retrieved data
p1 = Popen(["grep","-w",q1,file], stdout=PIPE)
p2 = Popen(["grep", q2], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]
print output
[2] using os.system
import os
cmdln = "grep -w " + q1_c + " " + db + " | grep -w " + q2_c + "| awk -F\"\t\" '{print $1}' "
os.system( cmdln )
---> this is not "quiet mode". quite problematic
[3] using commands
cmdln = "grep -w " + q1_c + " " + db + " | grep -w " + q2_c + "| awk -F\"\t\" '{print $1}' "
out = commands.getstatusoutput( cmdln ) # or simply getoutput
status = out[0] # this is error code
bingo = out[1] # retrieved data