制御文字を出力するperlスクリプト
2009年10月28日 09:52必要な制御文字だけを出力するスクリプトを作りました。
制御文字が含まれているデータを扱う際にちょと使ったものでーす。
引数無しで実効すると使い方を表示します。
$ ./control_character_output.pl Usage: ./control_character_output.pl [+all] [+ Abbr...] [- Abbr...] Control Character Abbreviation: ACK BEL BS CAN CR DC1 DC2 DC3 DC4 DEL DLE EM ENQ EOT ESC ETB ETX FF FS GS HT LF NAK NUL RS SI SO SOH STX SUB SYN US VT
すべての制御文字を出力する時は引数に +all を指定します。
以下の例では制御文字が出力されているかを目視で確認するために od コマンドを利用しています。
$ ./control_character_output.pl +all | od -c 0000000 001 \r \t 024 036 003 177 032 \b 035 \a 033 004 \0 037 \n 0000020 030 002 005 031 034 022 026 006 021 017 \f 023 \v 016 025 027 0000040 020 0000041
改行だけを出したい場合は + の後に cr や lf を指定します。
$ ./control_character_output.pl + CR lf | od -c 0000000 \r \n 0000002
逆に改行以外を出したい場合は - を付けて cr や lf を指定します。
$ ./control_character_output.pl - CR lf | od -c 0000000 001 \t 024 036 003 177 032 \b 035 \a 033 004 \0 037 030 002 0000020 005 031 034 022 026 006 021 017 \f 023 \v 016 025 027 020 0000037
指定する文字の詳細は -h を引数に指定すると出力されます。
$ ./control_character_output.pl -h NUL => NULl 空文字 SOH => Start Of Heading ヘッダ開始 STX => Start of TeXt テキスト開始 ETX => End of TeXt テキスト終了 EOT => End Of Transmission 伝送終了 ENQ => ENQuiry 問い合わせ ACK => ACKnowledge 肯定応答 BEL => BELI ベル BS => Back Space 一文字後退 HT => Horizontal Tabulation 水平タブ LF => Line Feed 改行 VT => Vertical Tabulation 垂直タブ FF => Form Feed 改ページ CR => Carriage Return 復帰 SO => Shift Out シフトアウト SI => Shift In シフトイン DLE => Data Link Escape データリンク拡張 DC => Device Control 1 装置制御1 DC => Device Control 2 装置制御2 DC => Device Control 3 装置制御3 DC => Device Control 4 装置制御4 NAK => Negative AcKnowledge 否定応答 SYN => SYNchronous idle 同期信号 ETB => End of Transmission Block ブロック転送終了 CAN => CANcel キャンセル EM => End of Medium 媒体終端 SUB => SUBstitute character 置換 ESC => ESCape エスケープ FS => File Separator ファイルセパレータ GS => Group Separator グループセパレータ RS => Record Separator レコードセパレータ US => Unit Separator ユニットセパレータ DEL => DELete 削除
下記がスクリプトのソースコードです。
control_character_output.pl
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
my ($output_switch) = grep /[-+]|\+all/, @ARGV;
my @output_character = grep s/(\w\w\w?)/\U$1/, @ARGV;
my %control_character_of = (
'NUL' => "\x{00}", # NULl 空文字
'SOH' => "\x{01}", # Start Of Heading ヘッダ開始
'STX' => "\x{02}", # Start of TeXt テキスト開始
'ETX' => "\x{03}", # End of TeXt テキスト終了
'EOT' => "\x{04}", # End Of Transmission 伝送終了
'ENQ' => "\x{05}", # ENQuiry 問い合わせ
'ACK' => "\x{06}", # ACKnowledge 肯定応答
'BEL' => "\x{07}", # BELI ベル
'BS' => "\x{08}", # Back Space 一文字後退
'HT' => "\x{09}", # Horizontal Tabulation 水平タブ
'LF' => "\x{0A}", # Line Feed 改行
'VT' => "\x{0B}", # Vertical Tabulation 垂直タブ
'FF' => "\x{0C}", # Form Feed 改ページ
'CR' => "\x{0D}", # Carriage Return 復帰
'SO' => "\x{0E}", # Shift Out シフトアウト
'SI' => "\x{0F}", # Shift In シフトイン
'DLE' => "\x{10}", # Data Link Escape データリンク拡張
'DC1' => "\x{11}", # Device Control 1 装置制御1
'DC2' => "\x{12}", # Device Control 2 装置制御2
'DC3' => "\x{13}", # Device Control 3 装置制御3
'DC4' => "\x{14}", # Device Control 4 装置制御4
'NAK' => "\x{15}", # Negative AcKnowledge 否定応答
'SYN' => "\x{16}", # SYNchronous idle 同期信号
'ETB' => "\x{17}", # End of Transmission Block ブロック転送終了
'CAN' => "\x{18}", # CANcel キャンセル
'EM' => "\x{19}", # End of Medium 媒体終端
'SUB' => "\x{1A}", # SUBstitute character 置換
'ESC' => "\x{1B}", # ESCape エスケープ
'FS' => "\x{1C}", # File Separator ファイルセパレータ
'GS' => "\x{1D}", # Group Separator グループセパレータ
'RS' => "\x{1E}", # Record Separator レコードセパレータ
'US' => "\x{1F}", # Unit Separator ユニットセパレータ
'DEL' => "\x{7F}", # DELete 削除
);
if (! $output_switch) {
my @addr = sort keys %control_character_of;
print <<"END_OF_USAGE";
Usage: $0 [+all] [+ Abbr...] [- Abbr...]
Control Character Abbreviation: @addr
END_OF_USAGE
}
elsif ($output_switch eq '+') {
for (@output_character) {
if ($control_character_of{$_}) {
print $control_character_of{$_};
}
}
}
elsif ($output_switch eq '-') {
for (@output_character) {
delete $control_character_of{$_};
}
for (keys %control_character_of) {
print $control_character_of{$_};
}
}
elsif ($output_switch eq '+all') {
for (keys %control_character_of) {
print $control_character_of{$_};
}
}
elsif ($output_switch eq '-h') {
open my $script, '<', "$0" or die "Can't open: $!";
while (my $i = <$script>) {
if ( $i =~ /^\s+'([A-Z]{2,3}).+#\s+(.+)$/ ) {
printf "%-3s => %s\n", $1, $2;
}
}
}
__END__
何かのお役に立てれば幸でーす。
