[英]Split File based on date prefix?
我有這個文件.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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.