简体   繁体   中英

How to execute multiple bash commands from Python?

I have a grep query:

grep  "8=ABC\.4\.[24].*\|10=[0-9]+" output.txt |grep "35=8"| cut -d "|" -f 12 >redirect.txt

How do I execute the same from inside a python script? I know for a simple grep it works as follows:

sed_process = subprocess.Popen(['sed', sed_query,fopen], stdout=subprocess.PIPE) 
grep_query = "8=ABC\.4\.[24].*\|10=[0-9]+"
grep_process = subprocess.Popen(['grep', grep_query], stdin=sed_process.stdout, stdout=subprocess.PIPE)

I'm confused as to how to combine 2 grep commands and a cut command and redirect it to an output file?

As addressed in the comments, this could all be implemented in python without calling anything. But if you want to make external calls, just keep chaining like you did in your example. The final stdout is an open file to finish off with the redirection. Notice I also close parent side stdout so that it doesn't keep an extra entry point to the pipe.

import subprocess as subp

p1 = subp.Popen(["grep", "8=ABC\.4\.[24].*\|10=[0-9]+", "output.txt"],
    stdout=subp.PIPE)
p1.stdout.close()
p2 = subp.Popen(["grep", "35=8"], stdin=p1.stdout, stdout=subp.PIPE)
p2.stdout.close()
p3 = subp.Popen(["cut", "-d", "|", "-f", "12"], stdin=p2.stdout, 
    stdout=open('redirect.txt', 'wb'))
p3.wait()
p2.wait()
p1.wait()

For cut it's exactly the same as for grep . To redirect to a file at the end, simply open() a file and pass that as stdout when running the cut command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM