먼저 이메일 주소를 기준으로 정렬한 다음 날짜를 기준으로 정렬하고 싶은 일부 메일 로그 발췌 내용이 있습니다.
입력 데이터 예시:
$ 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
현재 버전의 파일은 공백으로 구분됩니다. 그래서 내가 원하는 것은 네 번째 열을 기준으로 먼저 정렬한 다음 첫 번째(월), 두 번째(숫자), 세 번째(타임스탬프에 특별한 처리가 필요하지 않는 한 숫자)를 기준으로 정렬하는 것입니다. 이것이 최선의 시도입니다.
$ 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" 키 인수만 포함하면 전자 메일에 따라 잘 정렬되지만 다른 키를 추가하면 무시되는 것 같습니다. 단순화를 위해 이 예에서는 첫 번째 열을 무시할 수 있습니다. 두 번째 열을 기준으로 한 정렬이 네 번째 열보다 우선한다는 점에서 문제가 여전히 존재합니다.
내가 도대체 뭘 잘못하고있는 겁니까?
답변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
및 해당 항목 에 추가해야 합니다.위치. 순서가 중요합니다.