1. 程式人生 > >Python——過濾錯誤日誌

Python——過濾錯誤日誌

#!/usr/bin/python

import os
import sys

file = open(sys.argv[1], "r")
for eachLine in file:
    eachLine = eachLine.strip()
    npos_id = eachLine.find("Tid")
    if npos_id > 0:
        tid = eachLine[npos_id+4 : npos_id+36]
        cmd = "fgrep -h" + tid + " /var/log/message-2017010* >> error.log"
        os.system(cmd)

功能:

   通過Tid過濾錯誤日誌的原始語句,用於後續執行。

fgrep用法:

       -h, --no-filename
              Suppress the prefixing of file names on output.  This is the default when there is only one file (or only standard input) to search.

       -i, --ignore-case
              Ignore case distinctions in both the PATTERN and the input files.  (-i is specified by POSIX.)

-h: 不列印日誌檔名

-l: 只打印檔名