Perl 文本文件中的多个排序
使用perl查找不区分大小写的搜索,因此如果在行首检测到“!”,将开始新的排序(仅在节上) 变成Perl 文本文件中的多个排序,perl,Perl,使用perl查找不区分大小写的搜索,因此如果在行首检测到“!”,将开始新的排序(仅在节上) 变成 [test file] ! Sort Section ! a e g r ! New Sort Section 1 2 3 d h 这里有一种方法: use strict; use warnings; my $filename = shift or die 'filename!'; my @sections; my $current; # input open my $fh, '<', $f
[test file]
! Sort Section
!
a
e
g
r
! New Sort Section
1
2
3
d
h
这里有一种方法:
use strict;
use warnings;
my $filename = shift or die 'filename!';
my @sections;
my $current;
# input
open my $fh, '<', $filename or die "open $filename: $!";
while ( <$fh> ) {
if ( m/^!/ ) {
$current = [ $_ ];
push @sections, $current;
}
else {
push @$current, $_;
}
}
close $fh;
# output
for ( @sections ) {
print shift @$_; # print first line
print sort @$_; # print rest
}
使用严格;
使用警告;
my$filename=shift或die'filename!';
我的@sections;
我的$current;
#输入
打开我的$fh,“这里有一种方法:
use strict;
use warnings;
my $filename = shift or die 'filename!';
my @sections;
my $current;
# input
open my $fh, '<', $filename or die "open $filename: $!";
while ( <$fh> ) {
if ( m/^!/ ) {
$current = [ $_ ];
push @sections, $current;
}
else {
push @$current, $_;
}
}
close $fh;
# output
for ( @sections ) {
print shift @$_; # print first line
print sort @$_; # print rest
}
使用严格;
使用警告;
my$filename=shift或die'filename!';
我的@sections;
我的$current;
#输入
使用输出文件打开我的$fh,“另一个。更重要的是,不要将整个文件加载到内存中:
use strict;
use warnings;
sub output {
my( $lines, $fh ) = @_;
return unless @$lines;
print $fh shift @$lines; # print first line
print $fh sort { lc $a cmp lc $b } @$lines; # print rest
return;
}
# ==== main ============================================================
my $filename = shift or die 'filename!';
my $outfn = "$filename.out";
die "output file $outfn already exists, aborting\n" if -e $outfn;
# prereqs okay, set up input, output and sort buffer
open my $fh, '<', $filename or die "open $filename: $!";
open my $fhout, '>', $outfn or die "open $outfn: $!";
my $current = [];
# process data
while ( <$fh> ) {
if ( m/^!/ ) {
output $current, $fhout;
$current = [ $_ ];
}
else {
push @$current, $_;
}
}
output $current, $fhout;
close $fhout;
close $fh;
使用严格;
使用警告;
子输出{
我的($line,$fh)=@;
返回,除非@美元行;
打印$fh shift@$行;#打印第一行
打印$fh排序{lc$a cmp lc$b}@$行;#打印其余行
返回;
}
#==main============================================================
my$filename=shift或die'filename!';
my$outfn=“$filename.out”;
如果-e$OUTN,则“输出文件$OUTN已存在,正在中止\n”;
#prereqs正常,设置输入、输出和排序缓冲区
打开我的$fh、、$OUTN或死亡“打开$OUTN:$!”;
my$current=[];
#过程数据
而(){
如果(m/^!/){
输出$current,$fhout;
$current=[$U8;];
}
否则{
推送@当前,$\;
}
}
输出$current,$fhout;
关闭$fhout;
收盘价$fh;
另一个,使用输出文件。更重要的是,不要将整个文件加载到内存中:
use strict;
use warnings;
sub output {
my( $lines, $fh ) = @_;
return unless @$lines;
print $fh shift @$lines; # print first line
print $fh sort { lc $a cmp lc $b } @$lines; # print rest
return;
}
# ==== main ============================================================
my $filename = shift or die 'filename!';
my $outfn = "$filename.out";
die "output file $outfn already exists, aborting\n" if -e $outfn;
# prereqs okay, set up input, output and sort buffer
open my $fh, '<', $filename or die "open $filename: $!";
open my $fhout, '>', $outfn or die "open $outfn: $!";
my $current = [];
# process data
while ( <$fh> ) {
if ( m/^!/ ) {
output $current, $fhout;
$current = [ $_ ];
}
else {
push @$current, $_;
}
}
output $current, $fhout;
close $fhout;
close $fh;
使用严格;
使用警告;
子输出{
我的($line,$fh)=@;
返回,除非@美元行;
打印$fh shift@$行;#打印第一行
打印$fh排序{lc$a cmp lc$b}@$行;#打印其余行
返回;
}
#==main============================================================
my$filename=shift或die'filename!';
my$outfn=“$filename.out”;
如果-e$OUTN,则“输出文件$OUTN已存在,正在中止\n”;
#prereqs正常,设置输入、输出和排序缓冲区
打开我的$fh、、$OUTN或死亡“打开$OUTN:$!”;
my$current=[];
#过程数据
而(){
如果(m/^!/){
输出$current,$fhout;
$current=[$U8;];
}
否则{
推送@当前,$\;
}
}
输出$current,$fhout;
关闭$fhout;
收盘价$fh;
跳过了不区分大小写的特性。您可以像这样提供排序例程:print sort{lc$a cmp lc$b}@$35;print rest
这是否将输出保存到相同的文件名(需要),是否与win32兼容?它将数据打印到标准输出,您可以使用重定向将其写入所需的位置。您还可以通过添加另一个打开/关闭输出文件句柄的调用将内容写入文件,可能会转到$inputfilename.out
。不过,我强烈建议不要破坏源文件。如果您还想在其中加入一些重命名逻辑,Perl内置了rename
。跳过了不区分大小写的特性。您可以像这样提供排序例程:print sort{lc$a cmp lc$b}@$35;print rest
这是否将输出保存到相同的文件名(需要),是否与win32兼容?它将数据打印到标准输出,您可以使用重定向将其写入所需的位置。您还可以通过添加另一个打开/关闭输出文件句柄的调用将内容写入文件,可能会转到$inputfilename.out
。不过,我强烈建议不要破坏源文件。如果你想在那里也加入一些重命名逻辑,Perl内置了重命名
。在我的手机atm上,我有一个基本的排序,但无法在我的手机atm上运行节位,我有一个基本排序,但无法运行节位我注意到unix格式的文本文件在输出中变成dos格式,是否有修复(保留原始文本格式)。。脚本在win32环境下运行,感谢您的努力:)修复程序是在文件句柄上调用binmode
。我注意到unix格式化的文本文件在输出中变成dos格式,是否有修复程序(保留原始文本格式)。。脚本在win32环境下运行,感谢您的努力:)修复方法是在文件句柄上调用binmode
。