I have a very long one-line shell command to be called by Python. The codes are like this:
# "first way"
def run_cmd ( command ):
print "Run: %s" % command
subprocess.call (command, shell=True)
run_cmd('''sort -n -r -k5 {3} |head -n 500|awk 'OFS="\t"{{if($2-{1}>0){{print $1,$2-{1},$3+{1},$4,$5}}}}' > {2}'''.format(top_count,extend/2,mid,summit))
These codes works, but it always complains like this:
sort: write failed: standard output: Broken pipe
sort: write error
awk: (FILENAME=- FNR=132) fatal: print to "standard output" failed (Broken pipe)
According to a previous answer, I need to use a longer script to finish this, like:
# "second way"
p1 = Popen("sort -n -r -k5 %s"%summit, stdout=PIPE)
p2 = Popen("head -n 500", stdin=p1.stdout, stdout=PIPE)
# and so on ..........
My questions are:
(1) whether the "second way" will be slower than "first way"
(2) if I have to write in "first way" anyway (because it's faster to write), how can I avoid the complain like broken pipe
(3) what might be the most compelling reason that I shouldn't write in "first way"