python批量把时间戳批量变成日期时间
tformat.py#!/bin/pythonimport timeimport sysdata = sys.stdin.readlines()for line in data:seconds = int(line.split(':')[0])s_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(seconds))print s_tim
tformat.py
#!/usr/bin/python
import time
import sys
import re
def tf(line):
timestemp = re.findall(r'(16\d{8})\D', line)
for x in timestemp:
s_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(int(x)))
line = line.replace(x,s_time)
#s_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(seconds))
print line.rstrip()
return
if len(sys.argv)==1 :
data = sys.stdin.readlines()
for line in data:
tf(line)
else :
fp = open(sys.argv[1], "r")
while True:
line = fp.readline()
if not line:
break
tf(line)
使用方法:
chmod a+x ./tformat.py
cat file | ./tformat.py
或者
tformat.py file
另一种用法:
leon@ubuntu:~/work$ cat <<EOF | ./line.py
1646481238: Client 357734500178325 has exceeded timeout, disconnecting.
1646481238: Socket error on client 357734500178325, disconnecting.
1646481545: New client connected from 223.73.212.49 as 357734500178325 (c1, k600).
EOF
2022-03-05 11:53:58 Client 357734500178325 has exceeded timeout, disconnecting.
2022-03-05 11:53:58 Socket error on client 357734500178325, disconnecting.
2022-03-05 11:59:05 New client connected from 223.73.212.49 as 357734500178325 (c1, k600).
更多推荐
所有评论(0)