実用的なアプリケーションと高度な使い方
この最後のステップでは、実際のシナリオにおける nl
コマンドの実用的なアプリケーションと高度な使い方を探っていきます。
複数のファイルに番号を付ける
nl
コマンドは一度に複数のファイルを処理することができます。まず、2 つ目のファイルを作成しましょう。
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
2 つ目のファイルでは行番号がリセットされていることに注意してください。ファイル間で連続した番号付けを行いたい場合は、-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
を使用してログに行番号を追加する方法を見てみましょう。
## First, create a sample log file
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: '
を使用します。
- 先頭にゼロを付けない左寄せの番号
-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
コマンドをどのように使用するかをよく理解できたでしょう。このスキルは、テキストファイル、ログ、コード、およびドキュメントを扱う際に役立つでしょう。