GNU/Linux >> Linux の 問題 >  >> Linux

Linux / UNIX でファイルを管理する 10 の Cat コマンドの例

Cat コマンドは、Unix / Linux の世界に足を踏み入れたときに学んだ基本的なコマンドの 1 つです。

cat がファイルの内容を表示することは既にご存じでしょう。このコマンドでさらに何ができるでしょうか?

このチュートリアルでは、cat コマンドの実用的な使用例を 10 個示します。おそらく、これらの例のうち、あなたにとって目新しいものはほとんどないでしょう。または、これらの例を既に知っている場合は、単なる復習になるかもしれません.

1.ファイルの内容を表示する

ファイル名を引数として cat に渡すと、以下のようにファイルの内容が表示されます。

$ cat program.pl
#!/usr/bin/perl

if( 2 ge 3) {
print "greater\n";
}else {
print "lesser\n";
}

以下に示すように、複数のファイルの内容を表示することもできます。

$ cat program.pl program2.pl
#!/usr/bin/perl

if( 2 ge 3) {
print "greater\n";
}else {
print "lesser\n";
}
#!/usr/bin/perl

@arr = qw(1 2 3);
$ref = \@arr;
print ref $ref;

2.新しいファイルを作成

cat コマンドを使用すると、stdin から受け取った行を、リダイレクト シンボルを使用して新しいファイルにリダイレクトできます。

引数なしで単純に cat コマンドを入力すると、stdin コンテンツを受け取り、stdout に表示します。そのため、行を 1 回入力した後、Enter キーを押すと、以下に示すように、同じ行が後続の行に出力されます。

$ cat
cat command for file oriented operations.
cat command for file oriented operations.
cp command for copy files or directories.
cp command for copy files or directories.

以下に示すように、stdout を新しいファイルにリダイレクトすることもできます。

$ cat >cmd_usage.txt
cat command for file oriented operations.
cp command for copy files or directories.

$ cat cmd_usage.txt
cat command for file oriented operations.
cp command for copy files or directories.

場合によっては、ファイルにコンテンツを追加する必要がある場合があります。以下に示すように、>> リダイレクト記号を使用してください。

$ cat >>cmd_usage.txt
ls command to list out file and directory with its attributes.

$ cat cmd_usage.txt
cat command for file oriented operations.
cp command for copy files or directories.
ls command to list out file and directory with its attributes.

3.ファイルの内容をコピー

UNIX のリダイレクト シンボルは、標準のファイル記述子の内容を処理する際に重要な役割を果たします。これを使用すると、以下に示すように、あるファイルの内容を別のファイルにコピーできます。

$ cat program.pl >backup_prgm.pl

上記のように、出力リダイレクトを使用したため、標準出力に表示されるコンテンツは、backup_pgrm.pl という新しいファイルに送信されます。 backup_pgrm.pl の内容を表示:

$ cat backup_pgrm.pl
#!/usr/bin/perl

if( 2 ge 3) {
print "greater\n";
}else {
print "lesser\n";
}

4.複数のファイルの内容を連結

cat コマンドを使用すると、複数のファイルの内容を新しいファイルに連結できます。

たとえば、program.pl と program2.pl のコードは、新しいファイル all_pgrm.pl に結合されます。

$ cat program.pl program2.pl >all_pgrm.pl

上記のように、stdout がリダイレクトされ、program.pl と program2.pl の内容で新しいファイルが作成されました。 all_pgrm.pl の内容を確認します:

$ cat all_pgrm.pl
#!/usr/bin/perl

if( 2 ge 3) {
print "greater\n";
}else {
print "lesser\n";
}
#!/usr/bin/perl

@arr = qw(1 2 3);
$ref = \@arr;
print ref $ref;

5.行番号を表示

各行の前に行番号を付けてファイルの内容を表示するには、オプション -n を使用します。次の例では、program.pl からの行の行番号を出力します。

$ cat -n program.pl
1 #!/usr/bin/perl
2
3 
4 if( 2 ge 3) {
5 print "greater\n";
6 } else {
7 print "lesser\n";
8 }
9
10

上で気づいたように、空の行にも番号が付けられています。空でない行のみに番号を付ける場合は、オプション -b を次のように使用します。

$ cat -b program.pl
1 #!/usr/bin/perl

2     
3 if( 2 ge 3) {
4 print "greater\n";
5 } else {
6 print "lesser\n";
7 }

空白を含む行は空の行とは見なされず、2 番目の行にも同じことが適用されることに注意してください。

6. Stdin からの入力とともにファイルの内容を連結

他のファイルの連結とともに stdin から行を読み取る可能性があります。したがって、ユーザーは必要なときにいつでも独自のコンテンツを入力できます。

次の例では、ファイルを結合するときに、最初に (stdin から) 数行を挿入できます。

$ cat - program.pl program2.pl >all_pgrm.pl
Contents from file : program.pl, program2.pl

上記のように、– は stdin から読み取ることができる場所です。したがって、stdin からの 1 行が all_pgrm.pl と呼ばれる新しいファイルの先頭に挿入され、後者の内容は program.pl および program2.pl ファイルからのものです:

$ cat -n all_pgrm.pl
1 Contents from file : program.pl, program2.pl
2 #!/usr/bin/perl
3
4
5 if( 2 ge 3) {
6 print "greater\n";
7 } else {
8 print "lesser\n";
9 }
10
11
12 #!/usr/bin/perl
13
14 @arr = qw(1 2 3);
15 $ref = \@arr;
16 print ref $ref;

7.空の出力行を繰り返し表示しない

場合によっては、ファイルを一覧表示するときに stdout に表示したくない空の行が繰り返し含まれていることがあります。 cat コマンドには、連続する空の出力行を 1 行にまとめて表示する -s というオプションがあります。

使用法 5 の最初の例 (つまり、各行の前に行番号を付けて表示) で気づいたように、ファイル program.pl には 9 と 10 の番号が付いた 2 つの連続した空の出力行があります。それらを表示したくない場合があります。空の出力行が繰り返されます。これは、以下に示すように抑制できます:

# cat -sn program.pl
1 #!/usr/bin/perl
2
3 
4 if( 2 ge 3) {
5 print "greater\n";
6 } else {
7 print "lesser\n";
8 }
9

上記の出力では、行 9 と行 10 がそれぞれ 1 つの空の行に抑制されます (つまり、行 9)。

8.行末とタブ文字を表示

猫がすべての行の最後に $ 文字を表示するようにすることができます。通常、cat -e オプションを使用してファイルの内容を一覧表示すると、ユーザーは各行の末尾にある空白を識別できません。

たとえば、ファイル program.pl で -e オプションを使用します。以下に示すように、このファイルの 3 行目 (つまり、program.pl) は実際には空の行ではなく、7 行目はそれぞれ空白で終わっています。

$ cat -ne program.pl
1 #!/usr/bin/perl$
2 $
3     $
4 if( 2 ge 3) {$
5 print "greater\n";$
6 } else {$
7 print "lesser\n";      $
8 }$
9 $
10 $

タブ文字を表示するには、オプション -T を使用します。 TAB 文字の ^I を表示します。以下に示すように、5 行目と 7 行目はタブ文字で始まります。

$ cat -neT program.pl
1 #!/usr/bin/perl$
2 $
3     $
4 if( 2 ge 3) {$
5 ^Iprint "greater\n";$
6 } else {$
7 ^Iprint "lesser\n";      $
8 }$
9 $
10 $

9.特定のパターンまでコンテンツを読む

ヒアドキュメントは cat コマンドと一緒に使用できます。たとえば、stdin から読み取る場合、特定のパターンを含む行まで読み取ることができます。以下の例では、行のブロックが stdin から (EOF まで) 読み取られ、標準出力に出力されます。

$ cat <<EOF
> mv command to move files and directories
> top command to display linux tasks
> EOF
mv command to move files and directories
top command to display linux tasks

10.ファイルの内容を逆に表示

この例は詐欺師です。これは実際には cat コマンドの例ではありませんが、関連しています。

tac は cat の逆です。ご想像のとおり、tac はファイルの内容を逆順に表示します (下から順に表示されます)。行内の文字を逆にしたいだけなら、rev コマンドを使用する必要があります。

たとえば、ファイル program.pl は次のように反転して表示されます:

$ tac program.pl 

}
	print "lesser\n";           
} else {
	print "greater\n";
if( 2 ge 3) {

#!/usr/bin/perl

Linux
  1. Linuxでファイルをダウンロードするための5つのWgetコマンドの例。

  2. 7 Linux で差分パッチ ファイルを適用するパッチ コマンドの例

  3. 14 Linux での便利な「cat」コマンドの例

  1. Linux での ln コマンドの例

  2. Linux での file コマンドの例

  3. UNIX/Linux での tail コマンドの例

  1. Linuxでの8つのヘッドコマンドの例

  2. Linuxでの8つのStatコマンドの例

  3. Linuxでの重要なCatコマンドの例