如何按特定列日期和时间或仅日期列按asc或desc顺序重新排序文件。
{"hostname":"sg2","role":"epay","eventid":"ALERT_ORACLE","date":"02/08/2011","time":"01:30:00"},
{"hostname":"sg1","role":"pay","eventid":"ALERT_DF","date":"11/24/2010","time":"19:33:01"},
{"hostname":"sg3","role":"ipay","eventid":"ALERT_SMF","date":"12/11/2012","time":"02:30:00"},
通常使用“sort -gk(列号)”,在这种情况下无法捕获日期和时间列或仅记录日期,可能需要一些分隔符字符串或相关。
Expetected view is:
{"hostname":"sg1","role":"pay","eventid":"ALERT_DF","date":"11/24/2010","time":"19:33:01"},
{"hostname":"sg2","role":"epay","eventid":"ALERT_ORACLE","date":"02/08/2011","time":"01:30:00"},
{"hostname":"sg3","role":"ipay","eventid":"ALERT_SMF","date":"12/11/2012","time":"02:30:00"},
答案 0 :(得分:3)
将sort
与:
一起用作分隔符,并按列5M
排序以进行日期比较:
$ sort -t: -k5M file
{"hostname":"sg1","role":"pay","eventid":"ALERT_DF","date":"11/24/2010","time":"19:33:01"},
{"hostname":"sg2","role":"epay","eventid":"ALERT_ORACLE","date":"02/08/2011","time":"01:30:00"},
{"hostname":"sg3","role":"ipay","eventid":"ALERT_SMF","date":"12/11/2012","time":"02:30:00"},
答案 1 :(得分:3)
如果你选择了ISO日期格式(YYYY-mm-dd),你的生活会更容易,但是
sort_by_date() {
awk -F \" '{
split($16, date, "/")
split($20, time, ":")
timestamp = mktime(date[3]" "date[1]" "date[2]" "time[1]" "time[2]" "time[3])
print timestamp "\t" $0
}' "$1" |
sort -k1,1n |
cut -f2-
}
sort_by_date filename
要降序,请使用sort -k1,1nr