0

I am trying to execute the following command line via Python.

Cat File |grepcidr -f file.name >>output.file

if I use subprocess.call

subprocess.call(["cat", "File", "|", "grepcidr -f", "file.name", ">>output.file"])

It just tries to Cat everything.

Any help would be appreciated.

Pranjal Bikash Das
  • 1,092
  • 9
  • 27
k3eper
  • 41
  • 1
  • 8
  • 1
    possible duplicate of [Python subprocess: how to use pipes thrice?](http://stackoverflow.com/questions/9655841/python-subprocess-how-to-use-pipes-thrice) – Wooble Jan 10 '13 at 16:05

1 Answers1

0

Trivial but non-portable way:

subprocess.call(["/bin/sh","-c","cat File | grepcidr -f file.name >>output.file"])

It requires /bin/sh, which should not be a problem if you rely on cat anyway. I think it's possible to build a family of cooperating subprocesses yourself: you'd have to do what /bin/sh does with pipes and redirections, but in python.

Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
  • Thank you Anton :), just need to loop through all the file.names now ^_^ – k3eper Jan 10 '13 at 16:10
  • With /bin/sh, every shell construct becomes available. Maybe shell's `for` loop will be easier to write. – Anton Kovalenko Jan 10 '13 at 16:13
  • Sorry im quite new to Python, what do you mean Shell's for loop? Im basically trying to grepcird a file with an associated file and ouput the results for each into a new file. – k3eper Jan 10 '13 at 16:20
  • It has nothing to do with python, everything to do with /bin/sh (that's why I recommend it if you're new to python, but less new to /bin/sh). `subprocess.call(["/bin/sh","-c","for file in *.myext; do cat $file | ...... >> output.file; done"])` – Anton Kovalenko Jan 10 '13 at 16:23
  • Ok so this should work? subprocess.call(["/bin/sh","-c","for file in *.txt do cat $file |grepcidr -f $file >>$file+1.txt"]) – k3eper Jan 10 '13 at 16:29
  • Try it interactively in the shell prompt first. I think that you don't want the same $file in `cat $file | grepcidr -f $file`. And the syntax is `for VAR in LIST; do command; ...; done"` -- don't forget semicolon and final `done`. – Anton Kovalenko Jan 10 '13 at 16:40
  • using `cat file | whatever` is kind of silly anyway; `whatever < file` works just as well, and doesn't misuse `cat`. – Wooble Jan 10 '13 at 18:22