使用Python运行复杂的grep命令

使用Python运行复杂的grep命令,python,python-2.7,command-line,grep,Python,Python 2.7,Command Line,Grep,我正在尝试运行这个grep复合命令,它可以在cmd上正常工作 grep Rec sy.log | grep e612r2246s768 | grep 2013-07 | grep -oh "'.*'" | wc -c 但这里有点不对劲,我还看不出来: import commands commands.getstatus("""/bin/grep Rec /var/log/sy.log | /bin/grep e612r2246s768 | /bin/grep 2013-07 | /bin/gr

我正在尝试运行这个grep复合命令,它可以在cmd上正常工作

grep Rec sy.log | grep e612r2246s768 | grep 2013-07 | grep -oh "'.*'" | wc -c
但这里有点不对劲,我还看不出来:

import commands
commands.getstatus("""/bin/grep Rec /var/log/sy.log | /bin/grep e612r2246s768 | /bin/grep 2013-07 | /bin/grep -oh "'.*'" | /usr/bin/wc -c""")
Out[2]: 'ls: cannot access /bin/grep Rec /var/log/sy.log | /bin/grep e612r2246s768 | /bin/grep 2013-07 | /bin/grep -oh "\'.*\'" | /usr/bin/wc -c: No such file or directory'
使用子流程:

import subprocess
cmd = ["""/bin/grep Rec /var/log/sy.log | /bin/grep e612r2246s768 | /bin/grep 2013-07 | /bin/grep -oh "'.*'" | /usr/bin/wc -c"""]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
/home/www-data/gpslistener/scripts/<ipython-input-24-0881e54c5eab> in <module>()
----> 1 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)

/usr/lib/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
    677                             p2cread, p2cwrite,
    678                             c2pread, c2pwrite,
--> 679                             errread, errwrite)
    680 
    681         if mswindows:

/usr/lib/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
   1247                     if fd is not None:
   1248                         os.close(fd)
-> 1249                 raise child_exception
   1250 
   1251 

OSError: [Errno 2] No such file or directory
导入子流程
cmd=[“/bin/grep Rec/var/log/sy.log |/bin/grep e612r2246s768 |/bin/grep 2013-07 |/bin/grep-oh”.*“|/usr/bin/wc-c”]
proc=subprocess.Popen(cmd,stdout=subprocess.PIPE)
---------------------------------------------------------------------------
OSError回溯(最近一次调用上次)
/主页/www-data/gpslistener/scripts/in()
---->1 proc=subprocess.Popen(cmd,stdout=subprocess.PIPE)
/usr/lib/python2.7/subprocess.pyc in_uuuuuinit_uuuuuu(self、args、bufsize、executable、stdin、stdout、stderr、preexec_fn、close_fds、shell、cwd、env、universal_新行、startupinfo、creationflags)
677 p2cread,p2cwrite,
678 c2pread,c2pwrite,
-->679错误读取,错误写入)
680
681如果mswindows:
/usr/lib/python2.7/subprocess.pyc in_execute_child(self、args、execute、preexec_fn、close_fds、cwd、env、universal_新行、startupinfo、creationflags、shell、p2cread、p2cwrite、c2pread、c2pwrite、errread、errwrite)
1247如果fd不是无:
1248操作系统关闭(fd)
->1249抚养子女例外
1250
1251
OSError:[Errno 2]没有这样的文件或目录

PD:路径正常,谢谢

好的,这种方式对我很有效:

>>>import subprocess
>>>cmd = ["""/bin/grep 'Rec' /var/log/sy.log | /bin/grep e612r2246s768 | /bin/grep 2013-07 | /bin/grep -oh "'.*'" | /usr/bin/wc -c"""]
>>>print subprocess.check_output(cmd,shell=True)
>>>365829
对于与shell注入相关的,执行shell管道的最简单方法是传入
shell=True

cmd = r'''/bin/grep Rec /var/log/syslog | \
    /bin/grep e612r2246s768 |\
    /bin/grep 2013-07 |\
/bin/grep -oh "'.*'" |\
/usr/bin/wc -c'''
subprocess.check_output(cmd, shell=True)

“自2.6版以来已弃用:Python 3中的命令模块已被删除。请改用子流程模块。”请解释为什么在cmd???@AshishKarpe中出现3次“”,有点晚了,但对于任何想知道的人来说,它使您可以在字符串中使用任何类型的引号。例如,“hello”““world”“将输出hello”““world”。