查找.gz文件的某些行匹配情况

来源:互联网 发布:射手 mac 字幕 编辑:程序博客网 时间:2024/06/10 21:54

查找一个压缩的.gz中是否有指定匹配某一行。


#!/bin/perl#print ("grep gz and find finish simulation line. \n");$log_gz = 'test.log.gz';print ("log of gz is $log_gz \n");open (FILE, "gunzip -c $log_gz | tail -1000 |") or die("can not open $log_gz");open (STATUS, ">status.log");$test_cnt = 0;while(<FILE>){chomp();if(/finish\sat\ssimulation\stime/){print STATUS ("$log_gz find end mark.\n");print ("$log_gz find end mark.\n");last;}$test_cnt++;print("$test_cnt \n");}close (FILE);close (STATUS);

技巧:

1,gunzip -c 不真正解压.gz文件,而是检查该文件,不会生成多余的文件。

2,使用通道技术: | tail -1000 只查找自己感兴趣的最后1000行。

3,最后一个 | 是不能少的。

4,找到了可以使用last退出while循环。


另外可以使用 zgrep “start” ./xxx.gz

调用perl脚本:

system("zgrep “start” ./xxx.gz >> out.log");




同样的也可以处理非gz文件,如果处理文本文件:使用cat命令

open (FILE, "cat $log_gz | tail -1000 |") or die("can not open $log_gz");


0 0