Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

Perl, multiple match string in multiple files

I would like to use

 myscript.pl targetfolder/* > result.csv

to grep some number from multiple ASCII files.

The data table is like

| 44.2 | 3123.7 | 3123 |
+--------+--------+--------+

--> this is the end of data table is like

myscript.pl

#!/usr/bin/env perl

use warnings;
use strict;
use Data::Dumper;  # for debugging

$Data::Dumper::Useqq=1;

#####start######

Title1();
Title2();

print "
"; 

#####Grep#######

foreach my $currentfile (@ARGV) {     # ARGV is the target files list
    print Dumper($currentfile);       # debug
    open my $filehanlder, '<', $currentfile or die "$currentfile: $!";   

    while ($r <= $#fswf) {      #judge end of the open file
        Value1();
        Value2();
        Print1();
        Print2();
        print "
";             
        $r++;
    }                 #go next line output

    Close $filehanlder; 
}

#####sub#######
sub Title1 {
    print "title1,title2";
}

sub Title2{
    print "title5,title6,title7,title8";
}


sub Value1 {
    my ($line);
    while ($line = <$filehanlder>)) {
        if ($line =~ /^|sMachine:(S+)s+Release:(S+)s+/) {
            our ($machine) = $1;our ($sw) = $2;
        }
    }
}


sub Value2 {
    my ($line);
    while ($line = <$filehanlder>)) {
        if ($line =~ /^|sProductionsResults+|s(S+)s+|/) {
            next if 1..4;
            my (@b) = "";
            $r = 1
            @result1 = @result2 = @result3 = @result4 = "";

            while ($line !~ /+-/) {                                 
                chomp $line; 
                @b = split / *| */, $line;
                our ($result1[$r]) = $b[1];
                our ($result2[$r]) = $b[2];
                our ($result3[$r]) = $b[3];
                our ($result4[$r]) = $b[4];
                $r++;
                $line = (<$filehanlder>);
                @b = "";
            }
        }
    }
}

##I need a value as file counter, but not sure where to put it.

Sub Print1 {
    print "$machine,$sw,";   # this keeps same cross lines from same file
}

Sub Print2 {
    print "$result1[$r],$result2[$r],$result3[$r],$result4[$r],";  # change every line    
}

#####sub#######

I don't know is this correct way to pass the $filehander to the subroutine and pass it throught different subroutine.

@Dave Cross: Thanks for pointing out. Exactly as you said. If I do loop in the subroutine, then one subroutine will go to the end of file, other subroutine get nothing. So shall I do while loop in the main ? Or shall I do open in every subroutines? so I can reset the filehandler to the 1st line of the file in every subroutine. If I have multiple @result as I grep in the sub values2 , how I can print them with the max lines number of these @result. For example, I have @result5[7] ,@result6[12], so I would like to print 12 lines record, the first 7 lines with result5 grep result, the last 5 line ,result5 column keeps empty and result6 column continue printout.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your filehandle is just stored in a scalar variable ($filehanlder) so it can be passed into a subroutine in exactly the same way as any other variable.

some_subroutine($filehanlder);

And, inside the subroutine:

sub some_subroutine {
  my ($fh) = @_;

  # do something with $fh
}

But I think you have more serious problems to worry about. You have two subroutines that have a while (<$filehanlder>) loop in them. The first of those to be called, will go to the end of the file, leaving the second with no data to process.

You probably want to rethink the design of this code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...