根据日期前缀拆分文件?
Split File based on date prefix?
我有这个file.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 覆盖前几天?
所以最后,我决定在搜索 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)
我有这个file.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 覆盖前几天?
所以最后,我决定在搜索 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)