0

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?

1
  • 2
    Why are you using all those other programs rather than writing it in Python ? They don't do anything you can't do in Python, you might as well write a shell script.
    – cdarke
    CommentedNov 11, 2016 at 6:40

2 Answers 2

2

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

    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.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.