简体   繁体   English

根据日期前缀拆分文件?

[英]Split File based on date prefix?

I have this file.log我有这个文件.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

I want to split based on date partition so I get,我想根据日期分区进行拆分,所以我得到了,

Sep_16.log Sep_16.log

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

Sep_17.log Sep_17.log

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

I search in the forum, that it's supposed to be using csplit and regex ^.{6} , but the answer that I got only for the regex to be used as delimiter, which is not what I intended.我在论坛中搜索,它应该使用csplit和正则表达式^.{6} ,但我得到的答案只是将正则表达式用作分隔符,这不是我想要的。

Also, I want to split 10k rows per date partition, so the filename will be something like Sep_17_part001.log , which will then using something like prefix and suffix option.另外,我想为每个日期分区拆分 10k 行,因此文件名将类似于Sep_17_part001.log ,然后将使用前缀和后缀选项之类的东西。

Does anybody know the full command for doing this?有人知道这样做的完整命令吗? And if I do this one time thing on one log, how can I make it to run daily, without csplit overwrite previous days?如果我在一个日志上做这一次的事情,我怎样才能让它每天运行,而不用 csplit 覆盖前几天?

So in the end, I decided to create a simple Python script after searching through csplit documentation and find nothing that suitable to my needs.所以最后,在搜索了csplit文档后,我决定创建一个简单的 Python 脚本,并没有找到适合我需要的东西。

Something like,就像是,

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 PHP 的日期使用 preg_split 拆分日志文件? - Split a log file using preg_split based on dates with PHP? Laravel 日志文件基于日期 - Laravel log file based on date 如何使用Python中的日志记录将当前日期添加为日志文件中的前缀 - How to add current date as prefix in log file using logging from Python 根据IP列表将日志文件拆分为多个日志文件 - Split a log file into multiple log files based on a list of IPs 根据文件名复制IIS日志文件,今天是今天 - Copying an IIS log file based on file name, this is today's date 基于日期的日志文件,无需重复运行 - date based log file without reusing across runs 如何基于当前日期创建文件结构? - How am I able to create a file structure based on the current date? 需要一个脚本按月拆分大文件,该脚本可以根据日志的顺序确定年份 - Need a script to split a large file by month that can determine year based off order of the logs 如何在我的日志消息前加上当前日期和时间? - How do I prefix my log messages with the current date and time? 如何根据日期和时间命名日志文件? 如何打印所有异常以及如何在java中创建目录? - How to name a log file based on date and time? how to print all exception and how to make directory in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM