perl

処理内容 命令 解説等
ある条件の場合(例では2バイト目から4
バイトがaaaaまたはbbbb)、マッチファイルに出力、条件以外はアンマッチファイルに出力、結果は、ログファイルに出力
open(INPUT , “in.txt”);
open(OTMAT ,”> match.txt”);
open(OTUNMAT,”> unmatch.txt”);
open(OTLOG ,”> log.txt”);
while($in = <input > ){
$ctr_in = $ctr_in + 1;
if ( substr($in,2-1,4) eq “aaaa”
or substr($in,2-1,4) eq “bbbb”){
print OTMAT $in;
$ctr_match = $ctr_match + 1
}
else{
print OTUNMAT $in;
$ctr_unmatch = $ctr_unmatch + 1
}
}
print OTLOG “in=$ctr_in match=$ctr_match unmatch=$ctr_unmatch”;
close;
障害が発生した場合、大量データの中から、データを抜き取ったり、抜き 取ったデータを加工して戻したりする場合、これは、使うと思い、挙げてみました。
aixでは、なぜか、「grep > ファイル名」では、出力ファイルの後ろが切れてしまう為、perlかawkで組むしかないと思いました。

perlでよく間違えるのが、開始位置。なので、最初からマイナス1しておけば、理解しやすい。