실용적인 활용 및 고급 사용법
이 마지막 단계에서는 실제 시나리오에서 nl 명령의 몇 가지 실용적인 응용 프로그램과 고급 사용법을 살펴보겠습니다.
여러 파일 번호 매기기
nl 명령어는 여러 파일을 한 번에 처리할 수 있습니다. 두 번째 파일을 만들어 보겠습니다.
nano commands2.txt
다음 내용을 추가합니다.
Additional Linux Commands
------------------------
find - search for files
tar - archive files
ssh - secure shell connection
df - disk free space
top - display system processes
nano 를 저장하고 종료합니다. 이제 두 파일을 함께 번호 매겨 보겠습니다.
nl sample.txt commands2.txt > combined_numbered.txt
결합된 결과를 봅니다.
cat combined_numbered.txt
두 파일이 순차적으로 번호가 매겨진 것을 볼 수 있습니다.
1 Linux Commands
2 -------------
3 cat - display file contents
4 ls - list directory contents
5 cd - change directory
6 grep - search for patterns
7 chmod - change file permissions
1 Additional Linux Commands
2 ------------------------
3 find - search for files
4 tar - archive files
5 ssh - secure shell connection
6 df - disk free space
7 top - display system processes
두 번째 파일에 대해 줄 번호가 다시 시작되는 것을 확인합니다. 파일 전체에서 연속적인 번호 매기기를 원하면 -i 옵션을 사용할 수 있습니다.
nl -i 1 -n ln sample.txt commands2.txt > continuously_numbered.txt
결과를 봅니다.
cat continuously_numbered.txt
두 파일 전체에서 연속적인 번호 매기기를 볼 수 있습니다.
1 Linux Commands
2 -------------
3 cat - display file contents
4 ls - list directory contents
5 cd - change directory
6 grep - search for patterns
7 chmod - change file permissions
8 Additional Linux Commands
9 ------------------------
10 find - search for files
11 tar - archive files
12 ssh - secure shell connection
13 df - disk free space
14 top - display system processes
다른 명령어와 결합
nl 명령어는 파이프를 사용하여 다른 Linux 명령어와 결합할 수 있습니다. 예를 들어, 명령 출력의 줄에 번호를 매길 수 있습니다.
ls -l /etc | nl > numbered_ls_output.txt
결과를 봅니다.
cat numbered_ls_output.txt
ls -l /etc의 출력에 줄 번호가 추가된 것을 볼 수 있습니다.
실제 사용 사례: 로그 파일에 줄 번호 추가
줄 번호 매기기는 로그 파일을 분석할 때 특히 유용합니다. nl을 사용하여 로그에 줄 번호를 추가하는 방법을 살펴보겠습니다.
## 먼저, 샘플 로그 파일을 만듭니다.
cat > sample_log.txt << EOF
[2023-07-01 10:15:22] INFO: System startup
[2023-07-01 10:15:24] INFO: Loading configuration
[2023-07-01 10:15:25] WARNING: Config file is outdated
[2023-07-01 10:15:28] ERROR: Failed to connect to database
[2023-07-01 10:15:30] INFO: Retrying database connection
[2023-07-01 10:15:33] INFO: Database connection established
[2023-07-01 10:15:35] INFO: System ready
EOF
이제 대괄호 안에 줄 번호가 포함된 사용자 정의 형식으로 줄 번호를 추가합니다.
nl -s ' [Line: ' -n ln -w 2 -b a sample_log.txt | sed 's/$/]/' > numbered_log.txt
이 명령어는 다음을 수행합니다.
- 사용자 정의 구분 기호 사용
-s ' [Line: '
- 선행 0 이 없는 왼쪽 정렬 숫자 사용
-n ln
- 너비를 2 자로 설정
-w 2
- 모든 줄에 번호 매기기
-b a
sed를 사용하여 각 줄의 끝에 닫는 대괄호 추가
결과를 봅니다.
cat numbered_log.txt
대괄호 안에 줄 번호가 있는 로그 항목을 볼 수 있습니다.
1 [Line: [2023-07-01 10:15:22] INFO: System startup]
2 [Line: [2023-07-01 10:15:24] INFO: Loading configuration]
3 [Line: [2023-07-01 10:15:25] WARNING: Config file is outdated]
4 [Line: [2023-07-01 10:15:28] ERROR: Failed to connect to database]
5 [Line: [2023-07-01 10:15:30] INFO: Retrying database connection]
6 [Line: [2023-07-01 10:15:33] INFO: Database connection established]
7 [Line: [2023-07-01 10:15:35] INFO: System ready]
이 형식은 문서 또는 토론에서 특정 로그 항목을 참조할 때 매우 유용할 수 있습니다.
이제 Linux 에서 기본 및 고급 줄 번호 매기기를 위해 nl 명령을 사용하는 방법에 대한 이해가 높아졌습니다. 이 기술은 텍스트 파일, 로그, 코드 및 문서를 사용할 때 유용할 것입니다.