いくつかのメール ログの抜粋があり、まず電子メール アドレスで並べ替え、次に日付で並べ替えたいと考えています。
入力データの例:
$ cat test3.txt
Oct 10 14:00:00 [email protected] bounced
Oct 10 13:00:00 [email protected] deferred
Oct 10 14:30:00 [email protected] bounced
Oct 10 12:00:00 [email protected] deferred
Oct 9 12:00:00 [email protected] deferred
Oct 9 14:00:00 [email protected] bounced
Oct 10 12:30:00 [email protected] deferred
Oct 10 13:30:00 [email protected] deferred
Oct 9 13:00:00 [email protected] deferred
現在のバージョンのファイルはスペースで区切られています。したがって、最初に 4 番目の列で並べ替え、次に 1 番目 (月)、2 番目 (数値)、3 番目 (タイムスタンプに特別な処理が必要でない限り、数値だと思います) で並べ替えたいと考えています。これが私の最善の試みです。
$ sort -k 4,4 -k 1,1M -nk 2 test3.txt
Oct 9 12:00:00 [email protected] deferred
Oct 9 13:00:00 [email protected] deferred
Oct 9 14:00:00 [email protected] bounced
Oct 10 12:00:00 [email protected] deferred
Oct 10 12:30:00 [email protected] deferred
Oct 10 13:00:00 [email protected] deferred
Oct 10 13:30:00 [email protected] deferred
Oct 10 14:00:00 [email protected] bounced
Oct 10 14:30:00 [email protected] bounced
「-k 4,4」キー引数のみを含めると、電子メールに従って適切にソートされますが、他のキーを追加すると無視されるようです。簡単にするために、この例では最初の列は無視できますが、2 番目の列によるソートが 4 番目の列によるソートよりも優先されるという問題は依然として存在します。
何が間違っているのでしょうか?
答え1
疑問がある場合は、--debug
フラグを使用してください:
xb@dnxb:/tmp$ sort -k 4,4 -k 1,1M -nk 2 test3.txt --debug
sort: using ‘en_SG.UTF-8’ sorting rules
sort: key 3 is numeric and spans multiple fields
Oct 9 12:00:00 [email protected] deferred
^ no match for key
___
_
_________________________________________
Oct 9 13:00:00 [email protected] deferred
^ no match for key
___
_
_________________________________________
Oct 9 14:00:00 [email protected] bounced
^ no match for key
___
_
________________________________________
これは動作するはずです:
xb@dnxb:/tmp$ sort -b -k4,4 -k1M -k2n -k3n test3.txt --debug
sort: using ‘en_SG.UTF-8’ sorting rules
sort: key 3 is numeric and spans multiple fields
sort: key 4 is numeric and spans multiple fields
Oct 10 12:00:00 [email protected] deferred
________________
___
__
__
_________________________________________
Oct 10 13:00:00 [email protected] deferred
________________
___
__
__
_________________________________________
...
xb@dnxb:/tmp$ sort -b -k4,4 -k1M -k2n -k3n test3.txt
Oct 10 12:00:00 [email protected] deferred
Oct 10 13:00:00 [email protected] deferred
Oct 10 14:00:00 [email protected] bounced
Oct 10 12:30:00 [email protected] deferred
Oct 10 13:30:00 [email protected] deferred
Oct 10 14:30:00 [email protected] bounced
Oct 9 12:00:00 [email protected] deferred
Oct 9 13:00:00 [email protected] deferred
Oct 9 14:00:00 [email protected] bounced
xb@dnxb:/tmp$
あなたは-nk 2
間違っていますinfo sort
。
A position in a sort field specified with ‘-k’ may have any of the
option letters ‘MbdfghinRrV’ appended to it, in which case no global
ordering options are inherited by that particular field.
それでオプション文字 n
に追加する必要がありk
ます位置順序は重要です。