我有一个包含多个文件的目录。文件命名如下:A11111,A22222,A33333,B11111,B22222,B33333等。我想读取这些文件,对内容执行某些格式化选项并将其写入输出文件。但是对于所有以A开头的文件,我只想要一个输出文件,对于所有以B开头的文件,我想要一个输出文件,依此类推。是否可以使用perl脚本执行此操作?
答案 0 :(得分:1)
以下示例应该是一个良好的开端:
#!/usr/bin/perl
use strict;
use warnings;
my $dir = '.';
opendir my $dh, $dir or die "Cannot open $dir: $!";
my @files = sort grep { ! -d } readdir $dh;
closedir $dh;
$dir =~ s/\/$//;
foreach my $file (@files) {
next if $file !~ /^[A-Z](\d)\1{4}$/;
my $output = substr($file, 0, 1);
open(my $ih, '<', "$dir/$file") or die "Could not open file '$file' $!";
open(my $oh, '>>', "$dir/$output") or die "Could not open file '$output' $!";
$_ = <$ih>;
# perform certain formating with $_ here
print $oh $_;
close($ih);
close($oh);
}
在第next if $file !~ /^[A-Z](\d)\1{4}$/;
行,它会跳过所有非必需格式的文件名,第一个字符是大写字母,第二个是数字,另外4个字符与第一个数字相同。
答案 1 :(得分:0)
如果你在linux上工作,请使用`cat file1 file2 ...&gt;大文件
否则这里有一个小脚本可以帮助你
use strict;
use warnings;
# get the directory from the commandline
# and clean ending /
my $dirname = $ARGV[0];
$dirname =~ s/\/$//;
# get a list of all files in directory; ignore all files beginning with a .
opendir(my $dh, $dirname) || die "can't opendir $dirname: $!";
my @files = grep { /^[^\.]/ && -f "$dirname/$_" } readdir($dh);
closedir $dh;
# loop through the files and write all beginning with
# A to file A, B to file B, etc. extent the regex to fit your needs
foreach my $file (@files) {
if ($file =~ /([AB])\d+/) {
open(IN, "< $dirname/$file") or die "cant open $dirname/$file for reading";
open(OUT, ">> $dirname/$1") or die "cant open $dirname/$1 for appending";
print OUT <IN>;
close(OUT);
close(IN);
} else {
print "$file didn't match\n";
}
}