cut 명령어 (AIX 5.3·6.1)

핵심 정리

  • 파일의 줄마다 바이트(-b), 문자(-c), 필드(-f)를 잘라 표준 출력에 씁니다. 셋 중 하나는 꼭 지정합니다.
  • 목록은 쉼표·빈칸·하이픈으로 씁니다. 1,4,7, 1-3,8, -5(1~5), 3-(3부터 끝까지).
  • 필드 구분자는 기본이 탭이고 -d 문자로 바꿉니다. -s는 구분자가 없는 줄을 뺍니다.
  • -n은 -b와 함께 써서 다중 바이트 문자가 쪼개지지 않게 합니다.
  • 줄을 가로로 고르려면 grep, 잘라 낸 열을 다시 붙이려면 paste를 씁니다.
  • AIX 5.3과 6.1 문서의 내용이 같습니다.
항목 내용
버전 AIX 5.3, AIX 6.1 (두 버전 내용 같음)
원문 IBM Commands Reference — 5.3 Volume 1, 6.1 Volume 1
파일 /usr/bin/cut
관련 명령어 paste, grep, awk

목적

파일의 줄마다 일부를 잘라 표준 출력에 씁니다.

영어 원문

Helps split the lines of a file.

구문

text
cut { -b List [ -n ] | -c List | -f List [ -s ] [ -d Character ] } [ File ... ]

설명

cut 명령은 파일의 줄마다 바이트, 문자, 필드를 잘라 표준 출력에 씁니다. File 매개변수를 지정하지 않으면 표준 입력을 읽습니다.

-b, -c, -f 중 하나는 꼭 지정해야 합니다. List 매개변수는 쉼표, 빈칸, 하이픈으로 나눈 정수 목록(오름차순)이며, 하이픈은 범위를 나타냅니다. 바이트, 문자, 필드 어디에든 쓸 수 있는 List의 예는 다음과 같습니다.

text
1,4,7
1-3,8
-5,10
3-

-5는 첫째부터 다섯째까지, 3-은 셋째부터 마지막까지를 줄여 쓴 것입니다.

필드를 자를 때는 List로 지정한 필드의 길이가 필드마다, 줄마다 다를 수 있습니다. 필드의 길이는 탭 같은 필드 구분 문자의 위치가 정합니다.

파일을 가로로 자르려면 grep 명령을, 잘라 낸 파일을 다시 붙이려면 paste 명령을 씁니다. 파일의 열 순서를 바꾸려면 cut과 paste를 함께 씁니다.

영어 원문

The cut command cuts bytes, characters, or fields from each line of a file and writes these bytes, characters, or fields to standard output. If you do not specify the File parameter, the cut command reads standard input.

You must specify either the -b, -c, or -f flag. The List parameter is a comma-separated, blank-separated, or hyphen-separated list of integer numbers (in increasing order). The hyphen separator indicates ranges. The following entries are some example List parameters which could refer to bytes, characters, or fields: 1,4,7 1-3,8 -5,10 3-

where -5 is a short form for the first through fifth and 3- is a short form for the third through last.

If using the cut command on fields, the length of the fields specified by the List parameter can vary from field to field and line to line. The position of the field delimiter character, such as a tab character, determines the length of a field.

You can also use the grep command to make horizontal cuts through a file and the paste command to put the files back together. To change the order of columns in a file, use the cut and paste commands.

플래그

플래그 설명
-b List 바이트 위치를 지정합니다. -n을 함께 지정하지 않으면 다중 바이트 문자의 경계를 무시합니다.
-c List 문자 위치를 지정합니다. 예를 들어 -c 1-72는 줄마다 앞의 72자를 씁니다.
-d Character -f를 쓸 때 Character를 필드 구분자로 씁니다. 빈칸처럼 셸에서 특별한 뜻이 있는 문자는 따옴표로 감쌉니다.
-f List 구분 문자(기본은 탭)로 나뉜 필드의 목록을 지정합니다. 예를 들어 -f 1,7은 줄마다 첫째와 일곱째 필드만 씁니다. 구분자가 없는 줄은 -s를 지정하지 않으면 그대로 내보냅니다(표의 소제목에 유용).
-n 다중 바이트 문자를 쪼개지 않습니다. -b와만 씁니다. 문자의 마지막 바이트가 -b의 List 범위 안에 있으면 그 문자를 쓰고, 아니면 뺍니다.
-s 구분 문자가 없는 줄을 출력하지 않습니다. -f와만 씁니다.
영어 원문
  • -b List — Specifies byte positions. These byte positions ignore multibyte character boundaries unless the -n flag is also specified.
  • -c List — Specifies character positions. For example, if you specify -c 1-72, the cut command writes out the first 72 characters in each line of the file.
  • -d Character — Uses the character specified by the Character variable as the field delimiter when you specify the -f flag. You must put quotation marks around characters with special meaning to the shell, such as the space character.
  • -f List — Specifies a list of fields assumed to be separated in the file by a delimiter character, which is by default the tab character. For example, if you specify -f 1,7, the cut command writes out only the first and seventh fields of each line. If a line contains no field delimiters, the cut command passes them through intact (useful for table subheadings), unless you specify the -s flag.
  • -n — Suppresses splitting of multibyte characters. Use only with the -b flag. If the last byte of a character falls within the range denoted by the List variable of the -b flag, the character is written; otherwise, the character is excluded.
  • -s — Suppresses lines that do not contain delimiter characters. Use only with the -f flag.

종료 상태

값 뜻
0 모든 입력 파일을 잘 출력했습니다.
0보다 큼 오류가 났습니다.
영어 원문

This command returns the following exit values:

  • 0 — All input files were output successfully.
  • >0 — An error occurred.

예제

  1. 줄마다 필드 몇 개를 표시합니다. 시스템 암호 파일에서 로그인 이름과 사용자 전체 이름 필드, 곧 쌍점(-d :)으로 나뉜 첫째와 다섯째 필드(-f 1,5)를 보여 줍니다.
bash
cut -f 1,5 -d : /etc/passwd

/etc/passwd가 다음과 같으면,

text
su:*:0:0:User with special privileges:/:/usr/bin/sh
daemon:*:1:1::/etc:
bin:*:2:2::/usr/bin:
sys:*:3:3::/usr/src:
adm:*:4:4:System Administrator:/var/adm:/usr/bin/sh
pierre:*:200:200:Pierre Harper:/home/pierre:/usr/bin/sh
joan:*:202:200:Joan Brown:/home/joan:/usr/bin/sh

출력은 다음과 같습니다.

text
su:User with special privileges
daemon:
bin:
sys:
adm:System Administrator
pierre:Pierre Harper
joan:Joan Brown
  1. 빈칸으로 나눈 목록으로 필드를 표시합니다.
bash
cut -f "1 2 3" -d : /etc/passwd
text
su:*:0
daemon:*:1
bin:*:2
sys:*:3
adm:*:4
pierre:*:200
joan:*:202
영어 원문
  1. To display several fields of each line of a file, enter:
cut -f 1,5 -d : /etc/passwd

This displays the login name and full user name fields of the system password file. These are the first and fifth fields (-f 1,5) separated by colons (-d :). For example, if the /etc/passwd file looks like this:

su:*:0:0:User with special privileges:/:/usr/bin/sh
daemon:*:1:1::/etc:
bin:*:2:2::/usr/bin:
sys:*:3:3::/usr/src:
adm:*:4:4:System Administrator:/var/adm:/usr/bin/sh
pierre:*:200:200:Pierre Harper:/home/pierre:/usr/bin/sh
joan:*:202:200:Joan Brown:/home/joan:/usr/bin/sh

The cut command produces:

su:User with special privileges
daemon:
bin:
sys:
adm:System Administrator
pierre:Pierre Harper
joan:Joan Brown
  1. To display fields using a blank separated list, enter:
cut -f "1 2 3" -d : /etc/passwd

The cut command produces:

su:*:0
daemon:*:1
bin:*:2
sys:*:3
adm:*:4
pierre:*:200
joan:*:202

파일

파일 설명
/usr/bin/cut cut 명령이 들어 있습니다.

관련 명령어: grep, paste, sh

영어 원문
  • /usr/bin/cut — Contains the cut command.

Related information:

  • grep command
  • paste command
  • sh command
  • Files command
  • Input and output redirection overview

리눅스(GNU)와 다른 점

항목 AIX 5.3·6.1 GNU cut
빈칸으로 나눈 목록 -f "1 2 3"처럼 받음 쉼표만 받음
-n 다중 바이트 문자를 쪼개지 않음 받지만 무시함

AIX 5.3·6.1 문서에는 --complement, --output-delimiter, -z 같은 GNU 플래그가 없습니다. 출력 구분자를 바꾸려면 awk나 tr을 씁니다.