다음 내용이 포함된 로그 파일이 있습니다.
11-12-2014 - 03:03:59AM lat = 41.990516; lon = -93.430704<br>
11-12-2014 - 03:05:15AM lat = 41.001546; lon = -93.443352<br>
11-12-2014 - 03:11:50AM lat = 42.039054; lon = -93.442001<br>
11-12-2014 - 12:08:03AM lat = 41.937911; lon = -93.369249<br>
11-12-2014 - 12:11:29AM lat = 41.949656; lon = -93.329133<br>
11-12-2014 - 12:23:02AM lat = 42.025385; lon = -93.347026<br>
11-12-2014 - 12:29:10AM lat = 41.033341; lon = -93.380586<br>
11-12-2014 - 12:38:08AM lat = 41.036720; lon = -93.436851<br>
11-12-2014 - 12:45:20AM lat = 41.998129; lon = -93.400943<br>
11-12-2014 - 12:53:36AM lat = 41.961489; lon = -93.414624<br>
어떻게 하면 24시간제로 변환하고 올바르게 정렬할 수 있나요?
답변1
그리고 perl
:
$ perl -MTime::Piece -anle '
$F[2] = Time::Piece->strptime($F[2],"%r")->strftime("%H:%M:%S");
push @out, [$F[2]."-".join("-", reverse(split("-",$F[0]))), join(" ",@F)];
END {
print for map { $_->[1] }
sort { $a->[0] cmp $b->[0] } @out;
}' file
11-12-2014 - 00:08:03 lat = 41.937911; lon = -93.369249<br>
11-12-2014 - 00:11:29 lat = 41.949656; lon = -93.329133<br>
11-12-2014 - 00:23:02 lat = 42.025385; lon = -93.347026<br>
11-12-2014 - 00:29:10 lat = 41.033341; lon = -93.380586<br>
11-12-2014 - 00:38:08 lat = 41.036720; lon = -93.436851<br>
11-12-2014 - 00:45:20 lat = 41.998129; lon = -93.400943<br>
11-12-2014 - 00:53:36 lat = 41.961489; lon = -93.414624<br>
11-12-2014 - 03:03:59 lat = 41.990516; lon = -93.430704<br>
11-12-2014 - 03:05:15 lat = 41.001546; lon = -93.443352<br>
11-12-2014 - 03:11:50 lat = 42.039054; lon = -93.442001<br>
답변2
date
이를 수행하려면 GNU 명령을 사용할 수 있습니다 . 문자열을 받아 해당 날짜를 인쇄할 수 있습니다.
$ date -d "11/11/2014 04:12:03PM"
Tue Nov 11 16:12:03 CET 2014
그러나 다음을 좋아하지 않는다는 점에 유의하십시오 DD-MM-YYY
.
$ date -d "11-11-2014"
date: invalid date ‘11-11-2014’
따라서 먼저 sed
파일에서 를 실행하여 모든 것을 -
로 바꿉니다 /
. 그런 다음 이를 전달하여 read
각 필드를 별도의 변수에 넣고 변환하고 정렬합니다.
$ sed 's#-#/#g' file | while read date _ hour rest; do
echo "$(date -d "$date $hour" +"%F - %R:%S") $rest"
done | sort -h
2014-11-12 - 00:08:03 lat = 41.937911; lon = /93.369249<br>
2014-11-12 - 00:11:29 lat = 41.949656; lon = /93.329133<br>
2014-11-12 - 00:23:02 lat = 42.025385; lon = /93.347026<br>
2014-11-12 - 00:29:10 lat = 41.033341; lon = /93.380586<br>
2014-11-12 - 00:38:08 lat = 41.036720; lon = /93.436851<br>
2014-11-12 - 00:45:20 lat = 41.998129; lon = /93.400943<br>
2014-11-12 - 00:53:36 lat = 41.961489; lon = /93.414624<br>
2014-11-12 - 03:03:59 lat = 41.990516; lon = /93.430704<br>
2014-11-12 - 03:05:15 lat = 41.001546; lon = /93.443352<br>
이는 귀하의 예에서는 작동하지만 02
11월( ) 이전에 2월( )도 정렬 해야 하는 경우에는 11
실패합니다 . 따라서 요령은 날짜를 다음과 같이 인쇄하는 것입니다.에포크 이후 초, 정렬한 다음 삭제합니다.
$ sed 's#-#/#g' file | while read date _ hour rest; do
printf "%s\t%s %s\n" "$(date -d "$date $hour" +"%s")" "$date - $hour" "$rest"
done | sort | cut -f 2-
11/12/2014 - 12:08:03AM lat = 41.937911; lon = /93.369249<br>
11/12/2014 - 12:11:29AM lat = 41.949656; lon = /93.329133<br>
11/12/2014 - 12:23:02AM lat = 42.025385; lon = /93.347026<br>
11/12/2014 - 12:29:10AM lat = 41.033341; lon = /93.380586<br>
11/12/2014 - 12:38:08AM lat = 41.036720; lon = /93.436851<br>
11/12/2014 - 12:45:20AM lat = 41.998129; lon = /93.400943<br>
11/12/2014 - 12:53:36AM lat = 41.961489; lon = /93.414624<br>
11/12/2014 - 03:03:59AM lat = 41.990516; lon = /93.430704<br>
11/12/2014 - 03:05:15AM lat = 41.001546; lon = /93.443352<br>
11/12/2014 - 03:11:50AM lat = 42.039054; lon = /93.442001<br>
또는 날짜를 24시간 형식으로 인쇄하세요.
$ sed 's#-#/#g' file | while read date _ hour rest; do
printf "%s\t%s %s\n" "$(date -d "$date $hour" +"%s")" \
"$(date -d "$date $hour" +"%F - %R:%S")" "$rest"
done | sort | cut -f 2-
2014-11-12 - 00:08:03 lat = 41.937911; lon = /93.369249<br>
2014-11-12 - 00:11:29 lat = 41.949656; lon = /93.329133<br>
2014-11-12 - 00:23:02 lat = 42.025385; lon = /93.347026<br>
2014-11-12 - 00:29:10 lat = 41.033341; lon = /93.380586<br>
2014-11-12 - 00:38:08 lat = 41.036720; lon = /93.436851<br>
2014-11-12 - 00:45:20 lat = 41.998129; lon = /93.400943<br>
2014-11-12 - 00:53:36 lat = 41.961489; lon = /93.414624<br>
2014-11-12 - 03:03:59 lat = 41.990516; lon = /93.430704<br>
2014-11-12 - 03:05:15 lat = 41.001546; lon = /93.443352<br>
2014-11-12 - 03:11:50 lat = 42.039054; lon = /93.442001<br>
답변3
Python과 dateutil
모듈( pip install dateutil
)을 사용하면 날짜/시간 객체를 직접 정렬할 수 있습니다.
#! /usr/bin/env python
import sys
from dateutil.parser import parse
lines = []
for line in open(sys.argv[1]):
d, rest = line[:24], line[24:]
lines.append((parse(d), rest))
for x in sorted(lines):
print x[0], x[1],
에서 시작하다python program.py inputfile
이는 없이도 가능 dateutil
하지만 이를 사용하면 모호하지 않은 한 입력 시간 형식을 지정할 필요가 없다는 장점이 있습니다.
답변4
나는 다음을 awk
사용할 것이다 sort
:
awk '{while("date +%T -d" $3|getline x){$3=x}}1' logfile | sort -t- -n -k3 -k1 -k2
먼저 다른 날짜를 갖도록 로그를 약간 수정해 보겠습니다.
11-12-2010 - 03:03:59AM lat = 41.990516; lon = -93.430704
11-12-1998 - 03:05:15AM lat = 41.001546; lon = -93.443352
11-12-2030 - 03:11:50AM lat = 42.039054; lon = -93.442001
11-12-2014 - 12:08:03AM lat = 41.937911; lon = -93.369249
11-12-2014 - 12:11:29AM lat = 41.949656; lon = -93.329133
11-11-2014 - 12:23:02AM lat = 42.025385; lon = -93.347026
11-12-2011 - 12:29:10AM lat = 41.033341; lon = -93.380586
11-12-2011 - 12:38:08AM lat = 41.036720; lon = -93.436851
10-12-2014 - 12:45:20AM lat = 41.998129; lon = -93.400943
11-12-2014 - 12:53:36AM lat = 41.961489; lon = -93.414624
결과는 다음과 같습니다:
11-12-1998 - 03:05:15 lat = 41.001546; lon = -93.443352
11-12-2010 - 03:03:59 lat = 41.990516; lon = -93.430704
11-12-2011 - 00:29:10 lat = 41.033341; lon = -93.380586
11-12-2011 - 00:38:08 lat = 41.036720; lon = -93.436851
10-12-2014 - 00:45:20 lat = 41.998129; lon = -93.400943
11-11-2014 - 00:23:02 lat = 42.025385; lon = -93.347026
11-12-2014 - 00:08:03 lat = 41.937911; lon = -93.369249
11-12-2014 - 00:11:29 lat = 41.949656; lon = -93.329133
11-12-2014 - 00:53:36 lat = 41.961489; lon = -93.414624
11-12-2030 - 03:11:50 lat = 42.039054; lon = -93.442001
여기서의 요령은 이를 -
필드 구분 기호로 사용하고 먼저 세 번째 필드(연도), 첫 번째(월), 두 번째(일)로 정렬하는 것입니다. 모든 정렬은 숫자로 이루어집니다( -n
옵션).