回答編集履歴

1

追記

2021/05/28 08:40

投稿

KojiDoi
KojiDoi

スコア13692

test CHANGED
@@ -11,3 +11,121 @@
11
11
  perl -MTime::Piece -le 'print sort {($aa)=$a=~/[(.*)]/; ($bb)=$b=~/[(.*)]/; (Time::Piece->strptime($aa, "%d/%b/%Y:%H:%M:%S:%S %z")->datetime()) cmp (Time::Piece->strptime($bb, "%d/%b/%Y:%H:%M:%S:%S %z")->datetime())} <>'
12
12
 
13
13
  ```
14
+
15
+
16
+
17
+ # 追記: GNU sortコマンドで頑張る
18
+
19
+
20
+
21
+ `-M`なんて知らなんだわと思い、試してみましたが、普通にやるとうまく行きませんでした。
22
+
23
+
24
+
25
+ ```
26
+
27
+ # テストデータ
28
+
29
+ $ perl -le 'print join("\n", map {ucfirst($_)} qw/jan apr feb jun mar dec oct/)'
30
+
31
+ Jan
32
+
33
+ Apr
34
+
35
+ Feb
36
+
37
+ Jun
38
+
39
+ Mar
40
+
41
+ Dec
42
+
43
+ Oct
44
+
45
+ ```
46
+
47
+
48
+
49
+ ```
50
+
51
+ $ perl -le 'print join("\n", map {ucfirst($_)} qw/jan apr feb jun mar dec oct/)' | sort
52
+
53
+ Apr
54
+
55
+ Dec
56
+
57
+ Feb
58
+
59
+ Jan
60
+
61
+ Jun
62
+
63
+ Mar
64
+
65
+ Oct
66
+
67
+ $ perl -le 'print join("\n", map {ucfirst($_)} qw/jan apr feb jun mar dec oct/)' | sort -M
68
+
69
+ Apr
70
+
71
+ Dec
72
+
73
+ Feb
74
+
75
+ Jan
76
+
77
+ Jun
78
+
79
+ Mar
80
+
81
+ Oct
82
+
83
+ ```
84
+
85
+
86
+
87
+ どうもlocaleを環境変数にて設定する必要があるらしいです。
88
+
89
+
90
+
91
+ ```
92
+
93
+ $ perl -le 'print join("\n", map {ucfirst($_)} qw/jan apr feb jun mar dec oct/)' |LC_TIME=C sort -M
94
+
95
+ Jan
96
+
97
+ Feb
98
+
99
+ Mar
100
+
101
+ Apr
102
+
103
+ Jun
104
+
105
+ Oct
106
+
107
+ Dec
108
+
109
+ ```
110
+
111
+
112
+
113
+ ```
114
+
115
+ $ cat test.log | LC_TIME=C sort -t '[' -k2.8n -k2.4,2.6M -k2.1,2.2n
116
+
117
+ 10.222.11.11 - - [08/Feb/2020:17:21:21:21 +0800] "POST /set.python HTTP/1.1 300 "-"
118
+
119
+ 10.222.11.11 - - [07/Jan/2021:18:21:21:21 +0800] "POST /set.python HTTP/1.1 300 "-"
120
+
121
+ 10.222.11.11 - - [10/Mar/2021:20:12:21:21 +0800] "POST /set.python HTTP/1.1 300 "-"
122
+
123
+ 10.222.11.11 - - [05/Apr/2021:03:21:21:21 +0800] "POST /set.python HTTP/1.1 300 "-"
124
+
125
+ 10.222.11.11 - - [09/Jun/2021:19:13:21:21 +0800] "POST /set.python HTTP/1.1 300 "-"
126
+
127
+ 10.222.11.11 - - [22/Oct/2021:02:20:21:21 +0800] "POST /set.python HTTP/1.1 300 "-"
128
+
129
+ 10.222.11.11 - - [21/Dec/2021:01:11:21:21 +0800] "POST /set.python HTTP/1.1 300 "-"
130
+
131
+ ```