[英]How to use zcat with regex in perl?
I have compressed .gz files that I want to open by using: 我已经压缩了要使用的.gz文件:
./open.pl file.gz
Basically my goal is to only print out certain lines in the zip file that match my regex...The files are big so I just want to output to stdout for now...How would I do this? 基本上我的目标是只在zip文件中打印出与我的正则表达式匹配的某些行...文件很大,所以我现在只想输出到stdout ...我该怎么做? I was thinking somethig like:
我在想这样的东西:
while(<>){
zcat $_ | <my regex> ????
}
Also I'm not sure about the syntax for printing lines out that match regex.. 另外,我不确定用于打印与正则表达式匹配的行的语法。
Here's how I've done similar 这是我做类似的事情
open( $handle, "zcat $ARGV[0]|" ) or die("Can't open $ARGV[0]: $!");
while( <$handle> ) {
if( /regex_pattern/ ) {
print $_;
}
}
I doubt zcat
will operate correctly on newline-separated chunks of a gzipped stream. 我怀疑
zcat
是否可以在gzip压缩流的换行符分隔的块上正确运行。 Instead, you'll want to uncompress the stream as you read it. 相反,您将需要在读取流时解压缩流。 There are at least a couple of ways to do it:
至少有两种方法可以做到:
foreach my $argv (@ARGV) {
open my $gz, "zcat $argv |";
while (<$gz>) {
# now $_ is an uncompressed line from the file $argv
print if /my pattern/;
}
}
There's probably a solution using the :gz
IO layer, too, but I can't quite get that correct right now. 也可能有一个使用
:gz
IO层的解决方案,但是我现在还不能完全正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.