-1

I need to run a proccess in the terminal to grab the output from it.

import subprocess
subprocess.check_output(["my_util", "some_file.txt", " | grep 'Some data1' | awk '{print $2}'"])

#or
subprocess.check_output(["my_util", "full_path/some_file.txt", "| grep 'Some data1'", "| awk '{print $2}'"])

And nothing happens in REPL, while running it in the terminal manually gives me the proper output.

update:

the output from sublime text:

my_util FAIL formats: can't open input pipe `| grep 'Sample data1'': premature EOF
my_util FAIL formats: can't open input pipe `| awk '{print $2}'': premature EOF
Traceback (most recent call last):
  File "/test1.py", line 4, in <module>
    "| grep 'Sample data1'", "| awk '{print $2}'"])
  File "/usr/lib/python2.7/subprocess.py", line 544, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '["my_util", "full_path/some_file.txt", "| grep 'Some data1'", "| awk '{print $2}'"]' returned non-zero exit status 2
Mataba
  • 97
  • 1
  • 10
  • Are you sure you didn't get an exception? – Brionius Aug 16 '13 at 05:03
  • As an aside, `grep 'foo' | awk '{ bar }'` is better written `awk '/foo/{ bar }'` unless you are using `grep` options which significantly alter its functionality. See [UUCA](http://partmaps.org/era/unix/award.html#grep) – tripleee Aug 16 '13 at 08:44

2 Answers2

1

os.system can be used instead of subprocess

import os

os.system("my_util some_file.txt | grep 'Some data1' | awk '{print $2}'" )
oldmonk
  • 739
  • 4
  • 10
  • did you try it? it causes an error because of "print" operator which Python treats as its operator. – Mataba Aug 16 '13 at 16:38
  • I've tested it on my server, using 'cat' instead of my_util. Everything is ok. Version of python is 2.6.6. – oldmonk Aug 17 '13 at 00:29
0

I don't think you can pipe commands like that in subprocess.

Here's a question with answers describing how to execute piped commands with subprocess.

Here's another description of how to do it.

Community
  • 1
  • 1
Brionius
  • 13,858
  • 3
  • 38
  • 49