sort 명령어 (AIX 5.3·6.1)
https://archive.horanglab.com/posts/aix-cmd-sort
핵심 정리
- 파일의 줄을 정렬하거나, 이미 정렬된 파일을 합치거나(
-m), 정렬돼 있는지 확인합니다(-c). 파일이 여럿이면 이어서 하나로 정렬합니다. - 플래그가 없으면 현재 로캘의 대조 순서로 줄 전체를 정렬합니다.
-A는 ASCII 바이트 순서로 정렬합니다. - 정렬 키는
-k 시작필드[.열][수식자][,끝필드[.열][수식자]]로 지정합니다. 옛 형식+건너뛸필드 -건너뛸필드도 받습니다(필드 번호가 하나 작음). - 자주 쓰는 플래그:
-n(숫자),-r(역순),-t 문자(필드 구분자),-u(같은 줄 하나만),-f(대소문자 무시),-d(사전 순서),-b(앞 공백 무시),-o 파일(출력 파일, 입력과 같아도 됨). - 한 줄의 필드는 최대 32개입니다. 임시 파일은
/var/tmp(안 되면/usr/tmp,/tmp)에 만들고,-T로 바꿉니다. - 6.1에서
-C(정렬 확인, 경고 메시지 없음)가 생겼습니다.
| 항목 | 내용 |
|---|---|
| 버전 | AIX 5.3, AIX 6.1 (6.1에서 -C 플래그가 더해짐) |
| 원문 | IBM Commands Reference — 5.3 Volume 5, 6.1 Volume 5 |
| 파일 | /usr/bin/sort, 임시 공간 /var/tmp·/usr/tmp·/tmp |
| 관련 명령어 | uniq, comm, join |
목적
파일을 정렬하고, 이미 정렬된 파일을 합치고, 파일이 정렬돼 있는지 확인합니다.
영어 원문
Sorts files, merges files that are already sorted, and checks files to determine if they have been sorted.
구문
sort [ -A ] [ -b ] [ -c ] [ -C ] [ -d ] [ -f ] [ -i ] [ -m ] [ -n ] [ -r ] [ -u ] [ -o OutFile ]
[ -t Character ] [ -T Directory ] [ -y [ Kilobytes ] ] [ -z RecordSize ]
[ [ + [ FSkip ] [ .CSkip ] [ b ] [ d ] [ f ] [ i ] [ n ] [ r ] ] [ - [ FSkip ] [ .CSkip ] [ b ] [ d ] [ f ] [ i ] [ n ] [ r ] ] ] ...
[ -k KeyDefinition ] ... [ File ... ]
-C는 6.1 플래그 표에만 있습니다(원문 구문에는 빠져 있습니다).
설명
sort 명령은 File 매개변수로 지정한 파일의 줄을 정렬해 표준 출력에 씁니다. 파일을 둘 이상 지정하면 이어 붙여 한 파일처럼 정렬합니다. 파일 이름 대신 -(빼기)를 쓰면 표준 입력을 뜻합니다. 파일 이름을 하나도 지정하지 않으면 표준 입력을 정렬합니다. 출력 파일은 -o 플래그로 지정합니다.
플래그를 지정하지 않으면 현재 로캘의 대조 순서에 따라 입력 파일의 줄 전체를 정렬합니다.
정렬 키
정렬 키는 입력 줄에서 필드 번호와 열 번호로 지정한 부분입니다. 필드는 필드 구분자로 나뉜 입력 줄의 부분입니다. 기본 필드 구분자는 연속된 공백 문자 하나 이상인데, 정렬할 때는 이 공백이 뒤 필드의 일부로 여겨집니다. -b 옵션으로 앞의 공백을 무시할 수 있습니다. 다른 필드 구분자는 -t 플래그로 지정합니다. C와 영어 로캘에서 공백 문자는 탭과 빈칸입니다.
정렬 키를 쓰면 먼저 첫 정렬 키의 내용으로 모든 줄을 정렬합니다. 그다음 첫 키가 같은 줄끼리 둘째 키로 정렬하는 식입니다. 정렬 키의 번호는 명령줄에 나온 순서입니다. 모든 키가 같은 두 줄은 현재 로캘의 대조 순서로 줄 전체를 비교합니다.
필드 안의 열 번호를 셀 때, 기본 필드 구분자의 공백은 뒤 필드의 일부로 셉니다. -t로 지정한 구분자 문자는 필드의 일부로 세지 않습니다. 앞의 공백은 -b로 무시할 수 있습니다.
정렬 키는 -k KeyDefinition이나, 앞으로 없어질 옛 형식 FSkip.CSkip으로 정의합니다.
-k로 정렬 키 정의하기
-k [ FStart [ .CStart ] ] [ Modifier ] [ , [ FEnd [ .CEnd ] ] [ Modifier ] ]
정렬 키는 FStart 필드의 CStart 열부터 FEnd 필드의 CEnd 열까지의 모든 문자입니다. FEnd가 없으면 줄의 마지막 문자까지, CEnd가 없으면 FEnd 필드의 마지막 문자까지입니다. KeyDefinition의 필드나 열 번호는 어느 것이든 생략할 수 있으며, 기본값은 다음과 같습니다.
| 변수 | 기본값 |
|---|---|
| FStart | 줄의 처음 |
| CStart | 필드의 첫 열 |
| FEnd | 줄의 끝 |
| CEnd | 필드의 마지막 열 |
Modifier에는 b, d, f, i, n, r 중 하나 이상을 씁니다. 수식자는 붙은 필드 정의에만 적용되며, 같은 글자의 플래그와 효과가 같습니다. 수식자 b는 붙은 필드 정의의 끝에만 적용됩니다. 예를 들어 -k 3.2b,3r는 셋째 필드의 공백이 아닌 둘째 열부터 셋째 필드 끝까지를 키로 하며, 이 키는 역순으로 정렬합니다. FStart·CStart가 줄 끝을 넘거나 FEnd·CEnd보다 뒤에 있으면 그 키는 무시합니다.
옛 형식 +FSkip으로 정렬 키 정의하기
[ +[FSkip1] [.CSkip1] [Modifier] ] [ -[FSkip2] [.CSkip2] [Modifier] ]
+FSkip1은 키의 첫 필드에 이르기까지 건너뛸 필드 수, .CSkip1은 그 필드 안에서 건너뛸 열 수입니다. -FSkip2는 키 바로 뒤의 첫 문자에 이르기까지 건너뛸 필드 수, .CSkip2는 그 필드 안에서 건너뛸 열 수입니다. 어느 것이든 생략할 수 있으며, 기본값은 다음과 같습니다.
| 변수 | 기본값 |
|---|---|
| FSkip1 | 줄의 처음 |
| CSkip1 | 0 |
| FSkip2 | 줄의 끝 |
| CSkip2 | 0 |
수식자는 -k의 것과 같습니다. +FSkip1.CSkip1의 숫자는 "건너뛸 수"라서 보통 키 자체의 필드·열 번호보다 하나 작습니다. 예를 들어 +2.1b -3r는 셋째 필드의 공백이 아닌 둘째 열부터 셋째 필드 끝까지를 키로 하고 역순으로 정렬합니다. +2.1b는 필드 두 개를 건너뛴 뒤 앞의 공백과 열 하나를 더 건너뛴다는 뜻입니다. +FSkip1.CSkip1이 줄 끝을 넘거나 -FSkip2.CSkip2보다 뒤에 있으면 그 키는 무시합니다.
참고: 한 줄의 필드는 최대 32개입니다.
영어 원문
The sort command sorts lines in the files specified by the File parameter and writes the result to standard output. If the File parameter specifies more than one file, the sort command concatenates the files and sorts them as one file. A -(minus sign) in place of a file name specifies standard input. If you do not specify any file names, the command sorts standard input. An output file can be specified with the -o flag.
If no flags are specified, the sort command sorts entire lines of the input file based upon the collation order of the current locale.
Sort Keys
A sort key is a portion of an input line that is specified by a field number and a column number. Fields are parts of input lines that are separated by field separators. The default field separator is a sequence of one or more consecutive blank characters. However, these blank characters are considered to be a part of the following field for sorting purposes. You can specify the -b option to ignore these leading blank characters. A different field separator can be specified using the -t flag. The tab and the space characters are the blank characters in the C and English Language locales.
When using sort keys, the sort command first sorts all lines on the contents of the first sort key. Next, all the lines whose first sort keys are equal are sorted upon the contents of the second sort key, and so on. Sort keys are numbered according to the order they appear on the command line. If two lines sort equally on all sort keys, the entire lines are then compared based upon the collation order in the current locale.
When numbering columns within fields, the blank characters in a default field separator are counted as part of the following field. Field separator characters specified by the -t flag are not counted as parts of fields. Leading blank characters can be ignored using the -b flag.
Sort keys can be defined using the following two methods:
-kKeyDefinitionFSkip.CSkip (obsolescent version).
Sort Key Definition Using the -k Flag
The -k KeyDefinition flag uses the following form:
-k [ FStart [ .CStart ] ] [ Modifier ] [ , [ FEnd [ .CEnd ] ][ Modifier ] ]
The sort key includes all characters beginning with the field specified by the FStart variable and the column specified by the CStart variable and ending with the field specified by the FEnd variable and the column specified by the CEnd variable. If Fend is not specified, the last character of the line is assumed. If CEnd is not specified the last character in the FEnd field is assumed. Any field or column number in the KeyDefinition variable may be omitted. The default values are:
- FStart — Beginning of the line
- CStart — First column in the field
- FEnd — End of the line
- CEnd — Last column of the field
If there is any space between the fields, sort considers them as separate fields.
The value of the Modifier variable can be one or more of the letters b, d, f, i, n, or r. The modifiers apply only to the field definition they are attached to and have the same effect as the flag of the same letter. The modifier letter b applies only to the end of the field definition to which it is attached. For example: -k 3.2b,3r
specifies a sort key beginning in the second nonblank column of the third field and extending to the end of the third field, with the sort on this key to be done in reverse collation order. If the FStart variable and the CStart variable fall beyond the end of the line or after the FEnd variable and the CEnd variable, then the sort key is ignored.
A sort key can also be specified in the following manner:
[+[FSkip1] [.CSkip1] [Modifier] ] [-[FSkip2] [.CSkip2] [Modifier]]
The +FSkip1 variable specifies the number of fields skipped to reach the first field of the sort key and the +CSkip variable specifies the number of columns skipped within that field to reach the first character in the sort key. The -FSkip variable specifies the number of fields skipped to reach the first character after the sort key, and the -CSkip variable specifies the number of columns to skip within that field. Any of the field and column skip counts may be omitted. The defaults are:
- FSkip1 — Beginning of the line
- CSkip1 — Zero
- FSkip2 — End of the line
- CSkip2 — Zero
The modifiers specified by the Modifier variable are the same as in the -k flag key sort definition.
The field and column numbers specified by +FSkip1.CSkip1 variables are generally one less than the field and column number of the sort key itself because these variables specify how many fields and columns to skip before reaching the sort key. For example: +2.1b -3r
specifies a sort key beginning in the second nonblank column of the third field and extending to the end of the third field, with the sort on this key to be done in reverse collation order. The statement +2.1b
specifies that two fields are skipped and then the leading blanks and one more column are skipped. If the +FSkip1.CSkip1 variables fall beyond the end of the line or after the -FSkip2.CSkip2 variables, then the sort key is ignored.
Note: The maximum number of fields on a line is 32.
플래그
참고: 정렬 키 정의보다 앞에 나온 -b, -d, -f, -i, -n, -r은 모든 정렬 키에 적용됩니다. -k KeyDefinition 뒤에는 이 플래그들을 따로 쓸 수 없고, KeyDefinition에 수식자로 붙이면 그 키에만 적용됩니다. +Fskip.Cskip이나 -Fskip.Cskip 키 정의 뒤에 오면 그 키에만 적용됩니다.
| 플래그 | 설명 |
|---|---|
-A |
현재 로캘의 대조 순서 대신 ASCII 대조 순서로 바이트 단위 정렬을 합니다. |
-b |
필드의 첫 열이나 마지막 열을 찾을 때 앞의 빈칸과 탭을 무시합니다. |
-c |
입력이 플래그로 지정한 순서 규칙대로 정렬돼 있는지 확인합니다. 바르게 정렬돼 있지 않으면 0이 아닌 값을 돌려줍니다. |
-C |
-c와 같지만, 순서가 어긋났거나 -u와 함께 쓸 때 같은 키가 있어도 표준 오류에 경고 메시지를 보내지 않습니다. 6.1부터 |
-d |
사전 순서로 정렬합니다. 비교할 때 글자, 숫자, 공백만 봅니다. |
-f |
비교하기 전에 소문자를 모두 대문자로 바꿉니다. |
-i |
비교할 때 인쇄할 수 없는 문자를 모두 무시합니다. |
-k KeyDefinition |
정렬 키를 지정합니다. 형식은 위 "-k로 정렬 키 정의하기"와 같습니다. 키 정의에 수식자를 붙이면 그 키에는 플래그가 적용되지 않습니다. |
-m |
여러 입력 파일을 합치기만 합니다. 입력은 이미 정렬돼 있다고 봅니다. |
-n |
숫자 필드를 산술 값으로 정렬합니다. 숫자 필드에는 앞의 공백, 빼기 기호(선택), 10진 숫자, 천 단위 구분 문자, 소수점 문자(선택)가 들어갈 수 있습니다. 숫자가 아닌 문자가 든 필드를 숫자 정렬하면 결과를 예측할 수 없습니다. |
-o OutFile |
표준 출력 대신 OutFile로 출력합니다. OutFile이 File과 같아도 됩니다. |
-r |
지정한 정렬의 순서를 뒤집습니다. |
-t Character |
Character를 필드 구분 문자 하나로 정합니다. |
-u |
정렬 키와 옵션에 따라 같다고 정렬되는 줄 묶음에서 한 줄만 남깁니다. |
-T Directory |
만드는 임시 파일을 모두 Directory에 둡니다. |
-y[Kilobytes] |
주 기억 장치를 Kilobytes만큼 써서 시작하고 필요하면 더 씁니다. 값이 최소보다 작거나 최대보다 크면 최소나 최대를 씁니다. -y를 생략하면 기본 크기로, -y0은 최소로, 값 없는 -y는 최대로 시작합니다. 기억 장치 크기는 성능에 크게 영향을 주며, 작은 파일을 큰 기억 장치로 정렬하는 것은 낭비입니다. |
-z RecordSize |
정렬할 줄 중에 기본 버퍼 크기보다 긴 줄이 있어도 비정상 종료하지 않게 합니다. -c나 -m을 지정하면 정렬 단계를 건너뛰고 시스템 기본 버퍼 크기를 쓰는데, 줄이 그보다 길면 비정상 종료합니다. -z는 정렬 단계에서 가장 긴 줄을 기록해 합치기 단계에서 충분한 버퍼를 잡게 합니다. RecordSize는 합칠 가장 긴 줄 이상의 바이트 값이어야 합니다. |
영어 원문
Note: A -b, -d, -f, -i, -n, or -r flag that appears before any sort key definition applies to all sort keys. None of the -b, -d, -f, -i, -n, or -r flags may appear alone after a -k KeyDefinition; if they are attached to a KeyDefinition variable as a modifier, they apply only to the attached sort key. If one of these flags follows a +Fskip.Cskip or -Fskip.Cskip sort key definition, the flag only applies to that sort key.
-A— Sorts on a byte-by-byte basis using ASCII collation order instead of collation in the current locale.-b— Ignores leading spaces and tabs to find the first or last column of a field.-c— Checks that input is sorted according to the ordering rules specified in the flags. A nonzero value is returned if the input file is not correctly sorted.-C— Checks that input is sorted according to the ordering rules specified in the flags except that a warning message shall not be sent to standard error if there is a disorder or, with-uoption, a duplicate key is detected.-d— Sorts using dictionary order. Only letters, digits, and spaces are considered in comparisons.-f— Changes all lowercase letters to uppercase before comparison.-i— Ignores all nonprinting characters during comparisons.-kKeyDefinition — Specifies a sort key. The format of the KeyDefinition option is: [ FStart [ .CStart ] ] [ Modifier ] [ , [ FEnd [ .CEnd ] ][ Modifier ] ] The sort key includes all characters beginning with the field specified by the FStart variable and the column specified by the CStart variable and ending with the field specified by the FEnd variable and the column specified by the CEnd variable. The value of the Modifier variable can be b, d, f, i, n, or r. The modifiers are equivalent to the flags of the same letter. When a modifier is attached to a key definition, then no flag is applied to it.-m— Merges multiple input files only; the input are assumed to be already sorted.-n— Sorts numeric fields by arithmetic value. A numeric field may contain leading blanks, an optional minus sign, decimal digits, thousands-separator characters, and an optional radix character. Numeric sorting of a field containing any nonnumeric character gives unpredictable results.-oOutFile — Directs output to the file specified by the OutFile parameter instead of standard output. The value of the OutFile parameter can be the same as the File parameter.-r— Reverses the order of the specified sort.-tCharacter — Specifies Character as the single field separator character.-u— Suppresses all but one line in each set of lines that sort equally according to the sort keys and options.-TDirectory — Places all temporary files that are created into the directory specified by the Directory parameter.-y[Kilobytes] — Starts the sort command using the number of kilobytes of main storage specified by the Kilobytes parameter and adds storage as needed. (If the value specified in the Kilobytes parameter is less than the minimum storage site or greater than the maximum, the minimum or maximum is used instead). If the-yflag is omitted, the sort command starts with the default storage size. The-y0flag starts with minimum storage, and the-yflag (with no Kilobytes value) starts with maximum storage. The amount of storage used by the sort command affects performance significantly. Sorting a small file in a large amount of storage is wasteful.-zRecordSize — Prevents abnormal termination if any of the lines being sorted are longer than the default buffer size. When the-cor-mflags are specified, the sorting phase is omitted and a system default buffer size is used. If sorted lines are longer than this size, sort terminates abnormally. The-zoption specifies recording of the longest line in the sort phase so adequate buffers can be allocated in the merge phase. RecordSize must designate a value in bytes equal to or greater than the longest line to be merged.
종료 상태
| 값 | 뜻 |
|---|---|
| 0 | 모든 입력 파일을 잘 출력했거나, -c를 지정했고 입력 파일이 바르게 정렬돼 있습니다. |
| 1 | -c에서 파일이 지정한 순서대로 정렬돼 있지 않거나, -c와 -u를 함께 지정했는데 키가 같은 입력 줄이 둘 있습니다. |
| 1보다 큼 | 오류가 났습니다. |
영어 원문
This command returns the following exit values:
- 0 — All input files were output successfully, or
-cwas specified and the input file was correctly sorted. - 1 — Under the
-coption, the file was not ordered as specified, or if the-cand-uoptions were both specified, two input lines were found with equal keys. >1— An error occurred.
예제
LC_ALL,LC_COLLATE,LANG환경 변수를En_US로 두고fruits파일을 정렬합니다. 각 열의 문자를 공백, 숫자, 특수 문자까지 하나씩 비교해 오름차순 사전식으로 정렬합니다.
LANG=En_US sort fruits
fruits 파일의 내용:
banana
orange
Persimmon
apple
%%banana
apple
ORANGE
출력:
%%banana
ORANGE
Persimmon
apple
apple
banana
orange
ASCII 대조 순서에서는 %(퍼센트)가 대문자보다, 대문자가 소문자보다 앞섭니다. 현재 로캘이 ASCII가 아닌 문자 집합을 쓰면 결과가 다를 수 있습니다.
- 사전 순서로 정렬합니다. 글자, 숫자, 공백만 비교합니다.
-d는 글자·숫자·공백이 아닌%를 무시하므로%%banana가banana곁에 놓입니다.
sort -d fruits
ORANGE
Persimmon
apple
apple
%%banana
banana
orange
- 대문자와 특수 문자가 든 줄을 비슷한 소문자 줄과 묶습니다.
-d는 특수 문자를,-f는 대소문자 차이를 무시합니다. 로캘이C면 출력은 다음과 같습니다.
sort -d -f fruits
apple
apple
%%banana
banana
ORANGE
orange
Persimmon
- 중복 줄을 지우며 정렬합니다.
-u가 중복 줄을 지워 줄마다 유일하게 만듭니다. 중복된apple만이 아니라banana와orange도 지워지는데,-d가%%를,-f가 대소문자를 무시하기 때문입니다.
sort -d -f -u fruits
apple
%%banana
ORANGE
Persimmon
- 예제 4처럼 정렬하되, 대소문자나 구두점이 다르면 지우지 않습니다.
+0 -d -f는 예제 3의-d -f와 같은 정렬을 하고, 뒤의+0이 같지 않은 줄을 가르는 비교를 한 번 더 해서-u가 지우지 못하게 합니다. 그래서%%banana와banana,ORANGE와orange가 따로 남고, 똑같은apple두 줄 중 하나만 지워집니다.
sort -u +0 -d -f +0 fruits
apple
%%banana
banana
ORANGE
orange
Persimmon
- 필드 구분 문자를 지정합니다. 줄마다 첫 쌍점 뒤의 글을 비교해
vegetables파일을 정렬합니다.+1은 첫 필드를 무시하고 둘째 필드부터 줄 끝까지 비교하라는 뜻이고,-t:는 쌍점이 필드 구분자라는 뜻입니다.
sort -t: +1 vegetables
vegetables 파일의 내용:
yams:104
turnips:8
potatoes:15
carrots:104
green beans:32
radishes:5
lettuce:15
로캘이 C일 때의 출력:
carrots:104
yams:104
lettuce:15
potatoes:15
green beans:32
radishes:5
turnips:8
숫자가 크기 순서가 아닌 것은, 사전식 정렬이 왼쪽부터 문자 하나씩 비교하기 때문입니다. 3이 5보다 앞서므로 32가 5보다 앞섭니다.
- 숫자로 정렬합니다.
vegetables파일을 둘째 필드의 숫자로 정렬합니다.
sort -t: +1 -n vegetables
radishes:5
turnips:8
lettuce:15
potatoes:15
green beans:32
carrots:104
yams:104
- 필드 둘 이상으로 정렬합니다. 둘째 필드로 숫자 정렬(
+1 -2 -n)을 하고, 그 안에서 첫 필드를 알파벳 역순(+0 -1 -r)으로 정렬합니다. 다음 둘 중 하나를 씁니다.
sort -t: +1 -2 -n +0 -1 -r vegetables
sort -t: -k2,2n -k1,1r vegetables
로캘이 C일 때의 출력입니다. 숫자 순서로 정렬되고, 숫자가 같은 줄은 알파벳 역순입니다.
radishes:5
turnips:8
potatoes:15
lettuce:15
green beans:32
yams:104
carrots:104
- 원본 파일을 정렬한 내용으로 바꿉니다. 정렬한 결과를
vegetables파일에 저장합니다(-o vegetables).
sort -o vegetables vegetables
영어 원문
- To sort the
fruitsfile with theLC_ALL,LC_COLLATE, or LANG environment variable set toEn_US, enter:
LANG=En_US sort fruits
This command sequence displays the contents of the fruits file sorted in ascending lexicographic order. The characters in each column are compared one by one, including spaces, digits, and special characters. For instance, if the fruits file contains the text:
banana
orange
Persimmon
apple
%%banana
apple
ORANGE
the sort command displays:
%%banana
ORANGE
Persimmon
apple
apple
banana
orange
In the ASCII collating sequence, the % (percent sign) precedes uppercase letters, which precede lowercase letters. If your current locale specifies a character set other than ASCII, your results may be different.
- To sort in dictionary order, enter:
sort -d fruits
This command sequence sorts and displays the contents of the fruits file, comparing only letters, digits, and spaces. If the fruits file is the same as in example 1, then the sort command displays:
ORANGE
Persimmon
apple
apple
%%banana
banana
orange
The -d flag ignores the % (percent sign) character because it is not a letter, digit, or space, placing %%banana with banana.
- To group lines that contain uppercase and special characters with similar lowercase lines, enter:
sort -d -f fruits
The -d flag ignores special characters and the -f flag ignores differences in case. With the LC_ALL, LC_COLLATE, or LANG environment variable set to C, the output for the fruits file becomes: apple apple %%banana banana ORANGE orange Persimmon
- To sort, removing duplicate lines, enter:
sort -d -f -u fruits
The -u flag tells the sort command to remove duplicate lines, making each line of the file unique. This command sequence displays: apple %%banana ORANGE Persimmon
Not only is the duplicate apple removed, but banana and ORANGE as well. These are removed because the -d flag ignores the %% special characters and the -f flag ignores differences in case.
- To sort as in example 4, removing duplicate instances unless capitalized or punctuated differently, enter:
sort -u +0 -d -f +0 fruits
Entering the +0 -d -f does the same type of sort that is done with -d -f in example 3. Then the +0 performs another comparison to distinguish lines that are not identical. This prevents the -u flag from removing them. Given the fruits file shown in example 1, the added +0 distinguishes %%banana from banana and ORANGE from orange. However, the two instances of apple are identical, so one of them is deleted. apple %%banana banana ORANGE orange Persimmon
- To specify the character that separates fields, enter:
sort -t: +1 vegetables
This command sequence sorts the vegetables file, comparing the text that follows the first colon on each line. The +1 tells the sort command to ignore the first field and to compare from the start of the second field to the end of the line. The -t: flag tells the sort command that colons separate fields. If vegetables contains: yams:104 turnips:8 potatoes:15 carrots:104 green beans:32 radishes:5 lettuce:15
Then, with the LC_ALL, LC_COLLATE, or LANG environment variable set to C, the sort command displays:
carrots:104
yams:104
lettuce:15
potatoes:15
green beans:32
radishes:5
turnips:8
Note that the numbers are not in numeric order. This happened when a lexicographic sort compares each character from left to right. In other words, 3 comes before 5, so 32 comes before 5.
- To sort numbers, enter:
sort -t: +1 -n vegetables
This command sequence sorts the vegetables file numerically on the second field. If the vegetables file is the same as in example 6, then the sort command displays:
radishes:5
turnips:8
lettuce:15
potatoes:15
green beans:32
carrots:104
yams:104
- To sort more than one field, enter:
sort -t: +1 -2 -n +0 -1 -r vegetables
OR
sort -t: -k2,2 n -k1,1 r vegetables
This command sequence performs a numeric sort on the second field (+1 -2 -n). Within that ordering, it sorts the first field in reverse alphabetic order (+0 -1 -r). With the LC_ALL, LC_COLLATE, or LANG environment variable set to C, the output looks like this:
radishes:5
turnips:8
potatoes:15
lettuce:15
green beans:32
yams:104
carrots:104
The command sorts the lines in numeric order. When two lines have the same number, they appear in reverse alphabetic order.
- To replace the original file with the sorted text, enter:
sort -o vegetables vegetables
This command sequence stores the sorted output into the vegetables file (-o vegetables).
파일
| 파일 | 설명 |
|---|---|
/usr/bin/sort |
sort 명령이 들어 있습니다. |
/var/tmp |
정렬하는 동안 쓰는 임시 공간입니다. |
/usr/tmp |
/var/tmp에 파일을 만들 수 없을 때 쓰는 임시 공간입니다. |
/tmp |
/var/tmp와 /usr/tmp에 파일을 만들 수 없을 때 쓰는 임시 공간입니다. |
관련 명령어: comm, join
영어 원문
- /usr/bin/sort — Contains the sort command.
- /var/tmp — Temporary space during the sort command processing.
- /usr/tmp — Temporary space during the sort command processing, if file cannot be created in /var/tmp.
- /tmp — Temporary space during the sort command processing, if file cannot be created in /var/tmp or /usr/tmp.
Related information:
- comm command
- join command
- Files command
- Input and output redirection
- National Language Support
리눅스(GNU)와 다른 점
| 항목 | AIX 5.3·6.1 | GNU sort |
|---|---|---|
+FSkip -FSkip 옛 형식 |
받음 | 최근 판은 POSIX 모드에서 받지 않음(-k를 씀) |
-A |
ASCII 바이트 순서 | 없음(LC_ALL=C로 같은 효과) |
-y·-z |
기억 장치 크기, 최대 줄 길이 | 없음(-S로 버퍼 크기) |
| 필드 수 | 한 줄에 최대 32개 | 제한 없음 |
AIX 5.3·6.1 문서에는 -h(사람이 읽는 크기), -V(버전 순서), -R(무작위), --parallel 같은 GNU 플래그가 없습니다.