2013-06-25 2 views
0

어떻게 2 개의 다른 열로 정렬 할 수 있습니까? 예를 들어, 나는 다음 종류의 열을 기준으로 정렬 칼럼 (6)에 의해 처음으로 원하는 4계층 적으로 열을 정렬하는 방법은 무엇입니까?

column4 column5 column6 
lae2894  603 user1 
e2894  2096 user1 
e2894  2096 user1 
e2894  2096 user1 
lae2894  603 user1 
lae2894  603 user1 

그 다음 명령으로 분류되었습니다

sort -t, -k6 users.txt > sorted-user.txt 

하지만 다음과 같아야합니다 원하는 출력 :

column4  column5  column6 
e2894   603  user1 
e2894   2096  user1 
e2894   2096  user1 
laee2894  2096  user1 
lae2894   603  user1 
lae2894   603  user1 
+0

안녕하세요. stackoverflow에 오신 것을 환영합니다. 그렇다면 어떤 소프트웨어 또는 프로그래밍 언어에 대해 이야기하고 있습니까? –

+0

그는 유닉스 정렬 도구에 대해 이야기하고 있습니다. – XapaJIaMnu

+0

@phresnel unix/linux shell – SheetJS

답변

1

대부분의 버전 sort은 여러 가지 주요 사양을 허용합니다. 이 시도 :

을 당신이 당신의 예제 명령이 지정된 이후로 난 당신이 붙여 넣기 한 "예"데이터가 진짜를 대표하지 않는 것을 의미한다 (실제 입력 파일이 구분 기호로 ,를 사용하는 있으리라 믿고있어 당신의 파일의 ...). 또한 단일 숫자 (예 : -k6)와 같은 주요 사양은 해당 필드에서 시작하여 줄 끝까지 확장되는 단일 키를 의미하므로 정렬 할 단일 필드를 지정하려면 위의 구문.

1

sort에 대한 -k 옵션은 여러 번 나타날 수 있습니다. 시도 :

sort -t, -k6,6 -k4,4 inputfile 

자세한 내용은 sort invocation을 참조하십시오.

> ‘-k pos1[,pos2]’ 
> ‘--key=pos1[,pos2]’ 

Specify a sort field that consists of the part of the line between pos1 and pos2 (or the end of the line, if pos2 is omitted), inclusive. 
Each pos has the form ‘f[.c][opts]’, where f is the number of the field to use, and c is the number of the first character from the beginning of the field. Fields and character positions are numbered starting with 1; a character position of zero in pos2 indicates the field's last character. If ‘.c’ is omitted from pos1, it defaults to 1 (the beginning of the field); if omitted from pos2, it defaults to 0 (the end of the field). opts are ordering options, allowing individual keys to be sorted according to different rules; see below for details. Keys can span multiple fields. 

Example: To sort on the second field, use --key=2,2 (-k 2,2). See below for more notes on keys and more examples. See also the --debug option to help determine the part of the line being used in the sort. 
+0

감사 devnull. 나는 너의 배치를 시험했다. 그것은 작동합니다. – user2452340

관련 문제