大分前になるけれど、上記の記事で、 Linux / Unix で用いられる「cat」コマンドの使われ方について紹介されていた。
catは、専らファイルの内容の出力に使われているイメージがある。しかし、catは「concatenate」(連結する)の略であり、本来はファイルを連結する為のコマンドだった(らしい)。
Linux / UNIX のcatに似たWindowsのコマンドとして「type」が在る。このtypeコマンドでも、上記記事で紹介された使い方の一部は実現出来る。以下でそれらを紹介する。
なお、typeはMS-DOSにも存在するが、そちらではどうかは確認していない(確認出来る環境が手元に無い)。
Display the contents of a file
typeのもっとも基本的な使い方であろう。引数としてファイルを指定すると、その内容を表示する。
> type program.pl
#! /usr/bin/perl
if( 2 > 3) {
print "greater\n";
}else {
print "lesser\n";
}
cat同様、typeも複数のファイルを指定する事が出来る。ただし、catとは異なり、対象のファイルのパスも表示される(こちらも参照する事)。
> type program.pl program2.pl
program.p1
#!/usr/bin/perl
if(2 > 3) {
print "greater\n";
} else {
print "lesser\n";
}
program2.pl
#!/usr/bin/perl
@arr = qw(1 2 3);
$ref = \@arr;
print ref $ref;
Create a New File
typeでも、新規にテキストファイルを作る事が出来る。ただし、catとは異なり引数として「con」を指定する必要がある。
> type con type command for file oriented operations. type command for file oriented operations. copy command for copy files or directories. copy command for copy files or directories.
(終了するにはCTRL+CCTRL+Zの後Enterを入力する。)
上記の例の場合、プロンプト行の後、奇数行目がユーザーが入力した内容、偶数行目が出力された内容となる。何らかの文字列を入力し改行したとたん、同一内容の文字列が出力される事になる。
conは「console」の意味で、コンソール入出力の為にWindowsで予約されている名前である。即ち、「type con」は「コンソールからの入力の内容を出力する」という意味となる。
「type con」だけでは何の役にも立たないが、リダイレクトと組み合わせる事で、有意なテキストファイルを作る事が出来る。ごく短いメモの作成であれば、これで充分だ。
> type con > cmd_usage.txt type command for file oriented operations. copy command for copy files or directories. > type cmd_usage.txt type command for file oriented operations. copy command for copy files or directories. > type con >> cmd_usage.txt dir command to list out file and directory with its attributes. > type cmd_usage.txt type command for file oriented operations. copy command for copy files or directories. dir command to list out file and directory with its attributes.
Copy File Content
リダイレクトを用いれば、ファイルのコピーも行う事が出来る。
> type program.pl > backup_prgm.pl
> type backup_prgm.pl
#! /usr/bin/perl
if( 2 > 3) {
print "greater\n";
}else {
print "lesser\n";
}
Concatenate Contents of Multiple Files
typeに複数のファイルを渡せる事は既に書いたが、それをリダイレクト処理する事でファイルの内容の連結が可能だ。
> type program.pl program2.pl > all_pgrm.pl
program.pl
program2.pl
> type all_pgrm.pl
#!/usr/bin/perl
if( 2 > 3) {
print "greater\n";
}else {
print "lesser\n";
}
#!/usr/bin/perl
@arr = qw(1 2 3);
$ref = \@arr;
print ref $ref;
「あれ、typeに複数ファイルを渡した場合は、ファイルパスも出力されてなかったっけ」と思った貴方は鋭い。しかし心配要らない。上記の例の通り、ファイルパスの出力はリダイレクトの対象とならない。ファイルの内容は標準出力に出力されるが、ファイルパスの出力先は標準エラー出力だからだ。
Concatenate File Contents along with Input from Stdin
これは、出来る場合と出来ない場合がある。
引数にconを使用すればコンソール入力の内容を出力出来るので、それを応用する。
> type program.pl program2.pl con > all_pgrm.pl
program.pl
program2.pl
con
何か適当に入力してCTRL+Cを押す
con
> type all_pgrm.pl
#!/usr/bin/perl
if(2 > 3) {
print "greater\n";
} else {
print "lesser\n";
}
#!/usr/bin/perl
@arr = qw(1 2 3);
$ref = \@arr;
print ref $ref;
何か適当に入力してCTRL+Cを押す
しかし、conを引数の最後以外に使った場合、conで入力した内容までしか連結出来ない。
> type con program.pl program2.pl > all_pgrm.pl 何か適当に入力してCTRL+Cを押す > type all_pgrm.pl 何か適当に入力してCTRL+Cを押す
これは恐らく、コンソールからの入力を終了させる為に使うCTRL+Cが、厳密には「コマンドの強制終了」を行うショートカットキーだからだと思われる。
> type program.pl program2.pl con > all_pgrm.pl
program.pl
program2.pl
con
何か適当に入力してCTRL+ZとEnterを押す
con
> type all_pgrm.pl
#!/usr/bin/perl
if(2 > 3) {
print "greater\n";
} else {
print "lesser\n";
}
#!/usr/bin/perl
@arr = qw(1 2 3);
$ref = \@arr;
print ref $ref;
何か適当に入力してCTRL+ZとEnterを押す
尤も、この点を除いても、厳密にはcatのそれとは異なる。 ここで言う「コンソール入力」とは、実は「標準入力」とは異なるからだ。例えば以下のコマンドは、予想に反してechoの引数を出力しない。標準入力の内容は無視され、通常の"type con"と同じく入力待ちになる。
> echo "パイプを用いた入力" | type con
元ネタの記事に在って、typeで実現出来なかったのは以下の5つ。
- 5. Display Line Numbers
- 7. Don't display Repeated empty Output Lines
- 8. Display end of Line and TAB characters
- 9. Read content until a Specific Pattern
- 10. Display file Content in Reverse
最後の項目は、そもそも数に含めるべきなのか甚だ疑問だけれど。
しかし、typeでもコンソールからの入力を受け付ける事やファイルの連結が出来る事は、意外と知られていないのではないだろうか。
- (終了するには
CTRL+CCTRL+Zの後Enterを入力する。) -
CTRL+Cは、実行中のコマンドを中断する時に用いるショートカットキーCTRL+Zは、EOF を入力するショートカットキーである。なお、何故かCTRL+Zは行頭、且つその後Enterを押さねば正確に機能しないようである( Windows XP にて確認)。
catではCTRL+Dを使うべきだが、typeはそれに対応していない。

CTRL+Zが使えるのを確認、記事の内容を修正しました。