Perl 解析可能会或可能不会 gzip 的文本文件
Perl to parse text file which may or may not be gzipped
我正在使用 Perl 来解析一个文本文件,该文件可能会压缩也可能不会压缩:
my $FILE;
if ( $file =~ /\.gz$/ ) {
open( $FILE, "gunzip -c $file |" ) || die $!;
} else {
open( $FILE, '<', $file ) || die $!;
}
有没有更简单的方法来处理这个问题?
open $FILE, "zcat -f $file|" or die $!;
man zcat
-f --force
If the input data is not in a format recognized by gzip, and if the option --stdout is also given, copy the input data without change to the standard output: let zcat behave as cat.
核心模块IO::Uncompress::Gunzip returns一个文件句柄,如果文件是uncompressed/unknown默认传递一个文件句柄给原始文件,这对我有用;
use IO::Uncompress::Gunzip qw($GunzipError);
# Random file, plain text, and gzipped
my $file=('file.in', 'file.in.gz')[rand 2];
print "Picked $file\n";
# Get file Handle
my $FILE = IO::Uncompress::Gunzip->new( $file )
or die "Problem found with $file: $GunzipError";
while(<$FILE>) { print; }
我正在使用 Perl 来解析一个文本文件,该文件可能会压缩也可能不会压缩:
my $FILE;
if ( $file =~ /\.gz$/ ) {
open( $FILE, "gunzip -c $file |" ) || die $!;
} else {
open( $FILE, '<', $file ) || die $!;
}
有没有更简单的方法来处理这个问题?
open $FILE, "zcat -f $file|" or die $!;
man zcat
-f --force
If the input data is not in a format recognized by gzip, and if the option --stdout is also given, copy the input data without change to the standard output: let zcat behave as cat.
核心模块IO::Uncompress::Gunzip returns一个文件句柄,如果文件是uncompressed/unknown默认传递一个文件句柄给原始文件,这对我有用;
use IO::Uncompress::Gunzip qw($GunzipError);
# Random file, plain text, and gzipped
my $file=('file.in', 'file.in.gz')[rand 2];
print "Picked $file\n";
# Get file Handle
my $FILE = IO::Uncompress::Gunzip->new( $file )
or die "Problem found with $file: $GunzipError";
while(<$FILE>) { print; }