基于日期前缀拆分文件?



我有这个文件.log

Sep 16 16:18:49 abcd 123 456
Sep 16 16:18:49 abcd 123 567
Sep 17 16:18:49 abcd 123 456
Sep 17 16:18:49 abcd 123 567

我想根据日期分区进行拆分,所以我得到,

Sep_16.log

Sep 16 16:18:49 abcd 123 456
Sep 16 16:18:49 abcd 123 567

Sep_17.log

Sep 17 16:18:49 abcd 123 456
Sep 17 16:18:49 abcd 123 567

我在论坛中搜索,它应该使用csplit和正则表达式^.{6},但我得到的答案只是将正则表达式用作分隔符,这不是我想要的。

另外,我想为每个日期分区拆分 10k 行,因此文件名将类似于Sep_17_part001.log,然后将使用前缀和后缀选项之类的内容。

有人知道执行此操作的完整命令吗?如果我在一个日志上执行此一次性操作,如何使其每天运行,而不会覆盖前几天?

所以最后,在搜索了csplit文档后,我决定创建一个简单的 Python 脚本,但没有找到适合我需求的脚本。

像这样,

with open(args.logfile) as f:
for line in f:
timef = datetime.strptime(str(datetime.utcnow().year) + line[:6], '%Y%b %d').strftime('%Y%m%d')
t_dest_path = os.path.join(date_path, timef + '-browse.log')
with open(t_dest_path, "a") as fdest:
fdest.write(line)

最新更新