2016-03-03 334 views
1

我有一个巨大的logfile1(Linux),日期格式如下&我想提取过去24小时的数据&用shell脚本写入不同的文件。请帮我完成任务?Shell脚本(提取最近24小时的日志)

---------- 
03/03/2016 05:40:42 AM QWTRAB1 AMQ7315: Failed to put message to accounting queue. Reason(2053 
---------- 
03/03/2016 05:40:42 AM QWTRAB1 AMQ7315: Failed to put message to accounting queue. Reason(2053 
---------- 
03/03/2016 05:40:46 AM QWTRAB1 AMQ7315: Failed to put message to accounting queue. Reason(2053 
---------- 
03/03/2016 05:40:46 AM QWTRAB1 AMQ7315: Failed to put message to accounting queue. Reason(2053 

回答

1

你可以做同样的事情到Split access.log file by dates using command line tools

split.awk

{ 
    split($1,array,"[:/]"); 
    year = array[3] 
    month = array[2] 
    day = array[1] 

    print > FILENAME"-"year"_"month"_"day".txt" 
} 

命令:

awk -f split.awk your_log_file.log 
+0

./test.sh + $的awk'BEGIN {\ n个分割( “一月二月三月四月五月六月七月八月九月十月十一月十二月”,月,““)\ n作为(一= 1; a <= 12; a ++)\ nm [months [a]] = a \ n} \ n {\ n split($ 4,array,“[:/]”); \ n year = array [3] \ n month = sprintf(“%02d”,m [array [2]])\ n \ n print> /apphome/mqm/logs/NewFile"-"year"_"month".txt"\n}'/ apphome/mqm/logs/MQLOGS.OUT.0303160800.txt awk:cmd。行:10:(FILENAME =/apphome/mqm/logs/MQLOGS.OUT.0303160800.txt FNR = 1)致命:除零试图 – Krishna

+0

@Krishna,我已经更新了我的答案。如果您的日志文件中真的有'------'行,您必须先将它们过滤出来... – MaxU

0

尝试awk命令提供的日期范围

awk '$0 >= "03/03/2016 05:40" && $0 <= "03/02/2016 05:40"' 

UPDATE:

​​3210

如果你想要得到的日志前一天。你可以每天运行一次crontab脚本。将给定的脚本保存在一个名为logparser.sh的文件中。提供主日志文件作为参数。您可以使用以前的日期在新的日志文件名称中找到解析的数据。

尝试$./logparser.sh logfilename.log

+0

我想每天都用cron作业做这件事 – Krishna

+0

@Krishna我已经更新了答案。你可以运行这个脚本作为crontab! – Rocoder

+0

看起来像它不给在写格式输出 – Krishna