[cmd_test@btjeon-naver ~]$ nl distros.txt | head
1 SUSE 10.2 12/07/2006
2 Fedora 10 11/25/2008
3 SUSE 11.0 06/19/2008
4 Ubuntu 8.04 04/24/2008
5 Fedora 8 11/08/2007
6 SUSE 10.3 10/04/2007
7 Ubuntu 6.10 10/26/2006
8 Fedora 7 05/31/2007
9 Ubuntu 7.10 10/18/2007
10 Ubuntu 7.04 04/19/2007
nl은 단순히 줄 번호를 매기는 간단한 작업을 하지만 꽤 신기한 주요 옵션들이 있다.
<nl 마크업>
마크업 | 설명 |
\:\:\: | 논리적 페이지의 머리말 시작 |
\:\: | 논리적 페이지의 본문 시작 |
\: | 논리적 페이지의 꼬리말 시작 |
위 각 마크업 요소는 해당 줄에 단독으로 나타내야 한다.
<nl 주요 옵션>
옵션 | 의미 |
-b style | 본문의 줄 번호에 스타일을 적용한다. style은 다음 중 하나를 사용할 수 있다. - a : 모든 줄에 번호를 붙인다. - t : 공백 줄이 아닌 경우에만 번호를 붙인다. (기본값) - n : 번호를 붙이지 않는다. - pregexp : 기본 정규 표현식과 일치하는 줄에만 번호를 붙인다. |
-f style | 꼬리말의 줄 번호에 스타일을 적용한다. 기본값은 n. |
-h style | 머리말의 줄 번호에 스타일을 적용한다. 기본값은 n. |
-i number | 페이지 번호의 증가량을 number로 설정한다. 기본값은 1. |
-n format | 줄 번호 포맷을 설정한다. format은 다음 중 하나를 사용할 수 있다. - ln : 왼쪽 정렬 - rn : 오른쪽 정렬 (기본값) - rz : 왼쪽에 0을 붙여서 오른쪽 정렬 (ex:0000001) |
-p | 각 논리 페이지의 시작 부분에서 페이지 번호를 재설정 못하게 한다. |
-s string | string을 구분자로 각 줄 번호의 끝에 추가한다. 기본값은 탭 문자. |
-v number | 각 논리 페이지의 첫째 줄 번호를 number로 설정한다. 기본값은 1. |
-w width | width를 줄 번호 필드의 너비로 설정한다. 기본값은 6. |
vi distos-nl.sed
-----------------------------------------------------------------------------------------
# sed script to produce Linux distributions report
1 i\
\\:\\:\\:\
\
Linux Distribution Report\
\
Name Ver. Released\
---- ---- --------\
\\:\\:
s/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/
$ a\
\\:\
\
End Of report
-----------------------------------------------------------------------------------------
sed, nl을 결합한 보고서를 만들기 위한 script 파일
[cmd_test@btjeon-naver ~]$ sort -k 1,1 -k 2n distros.txt | sed -f distros-nl.sed | nl
Linux Distribution Report
Name Ver. Released
---- ---- --------
1 Fedora 5 2006-03-20
2 Fedora 6 2006-10-24
3 Fedora 7 2007-05-31
4 Fedora 8 2007-11-08
5 Fedora 9 2008-05-13
6 Fedora 10 2008-11-25
7 SUSE 10.1 2006-05-11
8 SUSE 10.2 2006-12-07
9 SUSE 10.3 2007-10-04
10 SUSE 11.0 2008-06-19
11 Ubuntu 6.06 2006-06-01
12 Ubuntu 6.10 2006-10-26
13 Ubuntu 7.04 2007-04-19
14 Ubuntu 7.10 2007-10-18
15 Ubuntu 8.04 2008-04-24
16 Ubuntu 8.10 2008-10-30
End Of report
기본적으로 본문 영역에만 줄 번호가 매겨진다.
[cmd_test@btjeon-naver ~]$ sort -k 1,1 -k 2n distros.txt | sed -f distros-nl.sed | nl -n rz
Linux Distribution Report
Name Ver. Released
---- ---- --------
000001 Fedora 5 2006-03-20
000002 Fedora 6 2006-10-24
000003 Fedora 7 2007-05-31
000004 Fedora 8 2007-11-08
000005 Fedora 9 2008-05-13
000006 Fedora 10 2008-11-25
000007 SUSE 10.1 2006-05-11
000008 SUSE 10.2 2006-12-07
000009 SUSE 10.3 2007-10-04
000010 SUSE 11.0 2008-06-19
000011 Ubuntu 6.06 2006-06-01
000012 Ubuntu 6.10 2006-10-26
000013 Ubuntu 7.04 2007-04-19
000014 Ubuntu 7.10 2007-10-18
000015 Ubuntu 8.04 2008-04-24
000016 Ubuntu 8.10 2008-10-30
End Of report
[cmd_test@btjeon-naver ~]$ sort -k 1,1 -k 2n distros.txt | sed -f distros-nl.sed | nl -w 3 -s ' '
Linux Distribution Report
Name Ver. Released
---- ---- --------
1 Fedora 5 2006-03-20
2 Fedora 6 2006-10-24
3 Fedora 7 2007-05-31
4 Fedora 8 2007-11-08
5 Fedora 9 2008-05-13
6 Fedora 10 2008-11-25
7 SUSE 10.1 2006-05-11
8 SUSE 10.2 2006-12-07
9 SUSE 10.3 2007-10-04
10 SUSE 11.0 2008-06-19
11 Ubuntu 6.06 2006-06-01
12 Ubuntu 6.10 2006-10-26
13 Ubuntu 7.04 2007-04-19
14 Ubuntu 7.10 2007-10-18
15 Ubuntu 8.04 2008-04-24
16 Ubuntu 8.10 2008-10-30
End Of report
다른 옵션을 주기 (줄 번호에 0 붙이기, 줄 번호 필드 길이 조정)
[cmd_test@btjeon-naver ~]$ echo "The quick brown fox jumped over the lazy dog." | fold -w 12
The quick br
own fox jump
ed over the
lazy dog.
fold 명령어는 텍스트 행을 지정된 길이로 나눈다. (한 줄의 길이를 12자로 지정했다. 기본값은 80자)
[cmd_test@btjeon-naver ~]$ echo "The quick brown fox jumped over the lazy dog." | fold -w 12 -s
The quick
brown fox
jumped over
the lazy
dog.
-s 옵션을 주면 줄의 끝에 도달하기 전 마지막 공백에서 자른다.
[cmd_test@btjeon-naver ~]$ info fmt > fmt-info.txt
info: Writing node (coreutils.info.gz)fmt invocation...
info: Done.
[cmd_test@btjeon-naver ~]$ vi fmt-info.txt
----------------------------------------------------------------------------------------
'fmt' reads from the specified FILE arguments (or standard input if
none are given), and writes to standard output.
By default, blank lines, spaces between words, and indentation are
preserved in the output; successive input lines with different
indentation are not joined; tabs are expanded on input and introduced on
output.
'fmt' prefers breaking lines at the end of a sentence, and tries to
avoid line breaks after the first word of a sentence or before the last
word of a sentence. A "sentence break" is defined as either the end of
a paragraph or a word ending in any of '.?!', followed by two spaces or
end of line, ignoring any intervening parentheses or quotes. Like TeX,
'fmt' reads entire "paragraphs" before choosing line breaks; the
algorithm is a variant of that given by Donald E. Knuth and Michael F.
Plass in "Breaking Paragraphs Into Lines", 'Software--Practice &
Experience' 11, 11 (November 1981), 1119-1184.
:wq!
----------------------------------------------------------------------------------------
fmt 테스트 파일 준비
[cmd_test@btjeon-naver ~]$ fmt -w 50 fmt-info.txt | head
'fmt' reads from the specified FILE arguments
(or standard input if
none are given), and writes to standard output.
By default, blank lines, spaces between words,
and indentation are
preserved in the output; successive input lines
with different indentation are not joined; tabs
are expanded on input and introduced on output.
[cmd_test@btjeon-naver ~]$ fmt -cw 50 fmt-info.txt | head
'fmt' reads from the specified FILE arguments
(or standard input if none are given), and writes
to standard output.
By default, blank lines, spaces between words,
and indentation are preserved in the output;
successive input lines with different indentation
are not joined; tabs are expanded on input and
introduced on output.
fmt도 fold처럼 텍스트를 자른다. 그러나 기본적으로 공백 줄과 들여쓰기를 유지하면서 텍스트를 합치거나 채운다.
(-c 옵션을 주면 좀 더 포맷을 유지하며 자를 수 있다.)
<fmt 주요 옵션>
옵션 | 의미 |
-c | crown margin 모드로 동작하게 된다. 이는 문단 첫 두 줄의 들여쓰기를 유지한다. 그 다음 줄부터는 두 번째 줄의 들여쓰기에 맞게 정렬된다. |
-p string | string을 접두어로 줄을 시작하게 만든다. string의 내용은 각 줄 앞에 붙여진다. 예를 들어 -p '# ' 옵션을 사용하면 주석으로 만들 수 있다. |
-s | 분할 모드. 이 모드에서는 각 줄은 지정된 "열 너비에 딱 맞게" 분할될 것이다. 짧은 줄은 너비를 채우기 위해서 합쳐지지는 않는다. 이 모드는 합쳐지길 원하지 않는 코드와 같은 텍스트를 구성할 때 유용하다. |
-u | 간격을 균등하게 유지한다. 이는 전통적인 타자기 스타일의 텍스트를 구성하게 될 것이다. 이는 단어 사이는 하나의 공백, 문장 사이는 두 개의 공백으로 처리한다. 이 모드는 강제로 왼쪽과 오른쪽 여백을 정렬하는 양쪽 정렬을 제거하려고 할 경우 유용하다. |
-w width | width 값을 기준으로 열을 구성한다. 기본값은 75자다. |
[cmd_test@btjeon-naver ~]$ cat > fmt-code.txt
# This file contains code with comments.
# This line is a comment.
# Followed by another comment line.
# And another.
This, on the other hand, is a line of code.
And another line of code.
And another.
[cmd_test@btjeon-naver ~]$ fmt -w 50 fmt-code.txt
# This file contains code with comments.
# This line is a comment. # Followed by another
comment line. # And another.
This, on the other hand, is a line of code.
And another line of code. And another.
[cmd_test@btjeon-naver ~]$ fmt -w 50 -p '# ' fmt-code.txt
# This file contains code with comments.
# This line is a comment. Followed by another
# comment line. And another.
This, on the other hand, is a line of code.
And another line of code.
And another.
주석 부근의 줄만 포맷을 유지하며 변경된 것을 볼 수 있다.
[cmd_test@btjeon-naver ~]$ pr -l 15 -w 65 distros.txt
2022-06-02 10:46 distros.txt Page 1
SUSE 10.2 12/07/2006
Fedora 10 11/25/2008
SUSE 11.0 06/19/2008
Ubuntu 8.04 04/24/2008
Fedora 8 11/08/2007
...
2022-06-02 10:46 distros.txt Page 4
Fedora 5 03/20/2006
pr 명령어는 텍스트에 페이지 매기기를 위해 사용된다. 65열 15행의 페이지를 정의한 옵션을 사용했다. 파일 수정 시간, 파일명, 페이지 번호가 포함된 기본 머리말을 생성한다.
[cmd_test@btjeon-naver ~]$ printf "I formatted the string: %s\n" foo
I formatted the string: foo
[cmd_test@btjeon-naver ~]$ printf "I formatted '%s' as a string.\n" foo
I formatted 'foo' as a string.
bash 쉘에도 C와 같은 printf가 내장되어 있다.
(printf " format" arguments 이런 식으로 사용한다. 커맨드 라인에 직접 사용되기보다는 표 자료와 같은 형식을 지정하기 위해 스크립트에서 흔히 사용된다.)
<주요 printf 자료형 인자>
지정자 | 설명 |
d | 부호를 가진 10진수 형태로 만든다. |
f | 부동 소수점 수 형태로 만든다. |
o | 8진수 형태로 만든다. |
s | 문자열 형태로 만든다. |
x | 소문자 a에서 f를 사용해 16진수 형태로 만든다. |
X | x와 동일하지만 대문자를 사용한다. |
% | 상수 기호 %를 출력한다. (즉, "%%"를 명시) |
[cmd_test@btjeon-naver ~]$ printf "%d, %f, %o, %s, %x, %X\n" 380 380 380 380 380 380
380, 380.000000, 574, 380, 17c, 17C
출력 결과를 조정하기 위해서 다수의 선택 요소들이 변환 지정자에 추가될 수 있다. 완전한 변환 지정은 다음과 같이 구성된다.
%[flags][width][-precision]conversation_specification
<printf 변환 지정 요소>
구성요소 | 설명 |
flags | 다섯 가지 플래그가 존재한다. - # : 출력을 위해 대체 포맷을 사용한다. 이는 자료형에 따라 다양하다. o(8진수) 변환은 출력 결과에 0이 붙는다. x와 X(16진수) 변환은 출력 결과에 따라 각각 0x, 0X가 앞에 붙는다. - 0 : 출력 결과에 0을 추가한다. 이는 000380처럼 필드 앞을 0으로 채운다. - - : 출력 결과를 왼쪽으로 정렬한다. 기본값은 오른쪽 정렬이다. - (스페이스) : 양수 앞에 공백을 생성한다. - + : 양수에 부호를 붙인다. printf는 기본적으로 음수엔만 부호를 붙인다. |
width | 최소 필드 너비를 지정한다. |
-precision | 부동 소수점 수의 소수점 뒷자리를 출력하기 위해 정밀도 자릿수를 지정한다. 문자 변환일 때는 출력될 문자 수를 지정한다. |
<printf 변환 지정 예제>
인자 | 포맷 | 결과 | 설명 |
380 | "%d" | 380 | 단순한 정수 형태로 나타낸다. |
380 | "%#x" | 0x17c | 대체 포맷 플래그를 사용하여 16진수 형태의 정수로 나타낸다. |
380 | "%05d" | 00380 | 0으로 시작하는 정수 형태로 나타내고 최소 필드 너비는 5자다. |
380 | "%05.5f" | 380.00000 | 5자리의 정밀도와 패딩을 가진 부동 소수점 수 형태로 나타낸다. 포맷된 수의 실제 너비가 최소 필드 너비 5보다 크기 때문에 패딩은 적용되지 않는다. |
380 | "%010.5f" | 0380.00000 | 최소 필드 너비가 10으로 증가했기에 패딩이 적용된다. |
380 | "%+d" | +380 | + 플래그는 양수에 부호를 붙인다. |
380 | "%-d" | 380 | - 플래그는 왼쪽 정렬을 한다. |
abcdefghijk | "%5s" | abcdefghijk | 문자열에 최소 필드 너비가 지정된다. |
abcdefghijk | "%.5s" | abcde | 문자열에 정밀도를 적용하면 해당 크기만큼 자른다. |
[cmd_test@btjeon-naver ~]$ printf "%s\t%s\t%s\n" str1 str2 str3
str1 str2 str3
[cmd_test@btjeon-naver ~]$ printf "Line: %05d %15.3f Result: %+15d\n" 1071 3.14156295 32589
Line: 01071 3.142 Result: +32589
[cmd_test@btjeon-naver ~]$ printf "<html>\n\t<head>\n\t\t<title>%s</title>\n\t</head>\n\t<body>\n\t\t<p>%s</p>\n\t</body>\n</html>\n" "Page Title" "Page Content"
<html>
<head>
<title>Page Title</title>
</head>
<body>
<p>Page Content</p>
</body>
</html>
다양한 포맷 사용 사례
출처 : 리눅스 커맨드라인 완벽 입문서
'IT일반 > Linux' 카테고리의 다른 글
Command Line - 쉘 스크립트 기초 (0) | 2022.06.03 |
---|---|
Command Line - 프로그램 컴파일 (0) | 2022.06.03 |
Command Line - 텍스트 편집 (0) | 2022.06.01 |
Command Line - 정규 표현식 (0) | 2022.06.01 |
Command Line - 파일 보관 및 백업 (0) | 2022.05.31 |