如何格式化subprocess.run()方法中的grep输出

时间:2019-12-11 23:26:23

标签: python-3.x grep subprocess

我有调用远程主机以在服务器日志中查找异常的方法,并且我正在从本地计算机执行python代码。我得到了输出,但随着输出,我的搜索字符串也附加到了日志文件中。

如何确保我的搜索字符串不属于subprocess.run()输出?

这是我的代码段

def lookForExceptions(inFileName, filterStr):
    searchString = filterStr[0]
    command = 'ssh 10.10.10.0 "grep -w {} {}"'.format(searchString, inFileNames)

    proc = subprocess.run(command, check=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                      universal_newlines=True)
    output = proc.stdout
    print(output.splitlines())

在上述方法inFileNames = / var / log / server.log中,并且searchString = java.lang.NullPointerException。此方法将输出打印如下

/var/log/server.log:[#|2019-12-11T22:23:18.930+0000|INFO|glassfish 4.1|java.lang.NullPointerException|_ThreadID=1046;_ThreadName=configuration-listener(26);licenseModuleType=JAVA}.|#]`

它可以打印1000多行,并且我不希望在输出中显示“ /var/log/server.log:”,我只需要实际的日志内容即可,

[#|2019-12-11T22:23:18.930+0000|INFO|glassfish 4.1|java.lang.NullPointerException|_ThreadID=1046;_ThreadName=configuration-listener(26);licenseModuleType=JAVA}.|#]

我尝试使用':'定界符进行拆分,但它还将我的日志条目也转换为列表拆分时间戳。

0 个答案:

没有答案
相关问题