まず、メールアドレスで並べ替えてから、日付で並べ替える予定のいくつかのメールログの抜粋があります。
入力データの例:
$ 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番目の列に基づいてソートし、次に最初の列(たとえば月)、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
それに添付する必要があります場所。順序が重要です。