[英]How to get the data from log file with different months in unix?
我想在不同月份和日期的日志文件中兩次獲取數據。假設如果日志文件中沒有我的星際時間,那么我想從最近一次的日志文件中提取數據。 如果輸入的結束時間未出現在日志文件中,它也必須在結束時間之前結束。
我的日志文件數據
Apr 10 16 02:07:20 Data 1
Apr 11 16 02:07:20 Data 1
May 10 16 04:11:09 Data 2
May 12 16 04:11:09 Data 2
Jun 11 16 06:22:35 Data 3
Jun 12 16 06:22:35 Data 3
我正在使用的解決方案是
awk -v start="$StartTime" -v stop="$EndTime" 'start <= $StartTime && $EndTime <= stop' $file
在哪里,我將開始時間存儲在$StartTime
,將結束時間存儲在$EndTime
但是Iam沒有得到確切的輸出。 請幫忙。
可能是這樣的:
$ BashVarStart="16 05 10 00 00 00" # the same format that awk function will reformat to
$ BashVarStop="16 06 11 00 00 00"
$ awk -v start="$BashVarStart" -v stop="$BashVarStop" -F"[ :]" -v OFS=\ '
function reformatdate(m,d,y,h,mm,s) { # basically throw year to the beginning
monstr="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"; # numerize the months
split(monstr,monarr," "); # split monstr to an array to enumerate the months
# monarr[1]="Jan", monarr[2]="Feb" etc
for(i in monarr) { # iterate over all month numbers in monarr index
if(monarr[i]==m) # when month number matches
m=sprintf("%02d",i) # zeropad if month number below 10: 9 -> 09
};
return y" "m" "d" "h" "mm" "s # return in different order
}
start < reformatdate($1,$2,$3,$4,$5,$6) && stop > reformatdate($1,$2,$3,$4,$5,$6)
' test.in
May 10 16 04:11:09 Data 2
May 12 16 04:11:09 Data 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.