[Linux] 문자열 앞, 마지막 부분 출력하기

리눅스 head 명령어를 통해 문자열의 앞부분을 확인하거나 tail 명령어로 마지막 부분을 출력할 수 있습니다.

 

 

테스트 시 활용할 text 파일 내용

# cat text
An apple is a fruit.
The sky is blue.
Today is Monday.
Cutting paper with scissors.
Trees grow on the ground.
The sun rises in the east.
The earth is round.
The winter night air is cold.
Drinking water every morning.
The bus has already left.

 

 

문자열 앞부분 출력하기

  • 문자열 앞부분 두번째 줄까지 출력하기
  • -n 옵션을 사용하거나 직접 -2 숫자를 지정할 수 있습니다.
# cat text | head -n 2
An apple is a fruit.
The sky is blue.

# cat text | head -2
An apple is a fruit.
The sky is blue.

 

  • 문자열 앞부분부터 특정 바이트 단위까지 출력하기
  • -c 옵션 사용
# cat text | head -c 50
An apple is a fruit.
The sky is blue.
Today is Mon

 

 

문자열 마지막 부분 출력하기

  • 문자열 마지막 부분 세번째 줄까지 출력하기
  • -n 옵션을 사용하거나 직접 -2 숫자를 지정할 수 있습니다.
# cat text | tail -n 3
The winter night air is cold.
Drinking water every morning.
The bus has already left.

# cat text | tail -3
The winter night air is cold.
Drinking water every morning.
The bus has already left.

 

  • 문자열 마지막 부분부터 특정 바이트 단위까지 출력하기
  • -c 옵션 사용
# cat text | tail -c 50
ng water every morning.
The bus has already left.

'Linux' 카테고리의 다른 글

[Linux] RPM 패키지 상세 정보 확인하기  (0) 2023.05.09
[Linux] Rocky 설치하기  (0) 2023.05.03
[Linux] 특정 문자열 기준으로 출력하기  (0) 2022.09.22

+ Recent posts