遍历当前目录下所有文件,并作如下操作:
1. 将 \r\n 替换为 \n
2. 删除行尾连续空白符
3. 将 .c .h 文件中的tab键替换为4个空格
#!/usr/bin/perl
use File::Find;
use File::Copy;
my @files;
my @dirs = ".";
my $extention = ".bak";
find(sub { push(@files, $File::Find::name) if -f && !/\.bak$/ }, @dirs);
for (@files) {
my $file = $_;
my $bakfile = $file . $extention;
if (!/\.s?o$/) {
print $file . "\n";
copy $file, $bakfile or die "Copy failed: $!";
open FO, $bakfile or die "Cannot open '$bakfile': $!";
open FN, ">$file" or die "Cannot open '$file': $!";
if (/\.(pl|[ch])$/) {
while () {
s/\t/ /g;
s/[ \r]+$//;
print FN $_ or die "write '$file' err: $!";
}
} else {
while () {
s/[ \t\r]+$//;
print FN $_ or die "write '$file' err: $!";
}
}
close FN;
close FO;
unlink $bakfile;
}
}
阅读(1269) | 评论(0) | 转发(0) |