Gblog

おもにTips

alert log の timestamp を各行の先頭に付与してファイル出力する

python です。

python はド素人です。

 

import os
import re
import sys

args = sys.argv

# ファイル名を引数に指定
alert_log = args[1]

# timestamp の初期値
timstr = '------------------------'

# エンコーディング(in/out 共通)
encode = "shift-jis"

# timestamp 文字列
stmstmp = ("2019-","2020-")
etmstmp = (" 2019"," 2020")

def alert_time(f):

    ifile = f 
    ofile = f + ".out"

    with open(ofile,"w",encoding=encode) as o:

        with open(ifile,"r",encoding=encode) as i:

            for line in i:
                line = line.strip()
                if line[0:5] in stmstmp or line[-5:] in etmstmp:
                    timstr = line
                elif  len(line) != 0:
                    o.write(timstr + ' // ' + line + "\n")


if __name__=="__main__":

    if len(args) != 2:
        print("Usage : > python alert_timemod.py ")
    else:
        alert_time(alert_log)

 

自己責任でお願いできればと思います。