Mayank Jha
Mayank Jha

Reputation: 1029

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?

Upvotes: 0

Views: 1020

Answers (2)

tdelaney
tdelaney

Reputation: 77337

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()

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249123

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.

Upvotes: 0

Related Questions