[Slugnet] bash script help

Chris Henry chrishenry.ni at gmail.com
Fri Jun 20 22:50:46 SGT 2008


On Fri, Jun 20, 2008 at 7:45 AM, Chris Henry <chrishenry.ni at gmail.com> wrote:
> On Fri, Jun 20, 2008 at 7:43 AM, Chris Henry <chrishenry.ni at gmail.com> wrote:
>> #/usr/bin/perl
>>
>> var result = 0;
>> open(FILE, '...') || die 'Can't open file';
>>
>> while ( <FILE> ) {
>>  chomp;
>>  if ($_ =~ /(t=\d+\s)(\d+)/) {
>>    $result += $2;
>>    print $1, $result;
>
> Sorry, this line should be print $1, $result, "\n";
>
>>  } else {
>>    die 'File format is wrong.';
>>  }
>> }
>>

Ignore, too many syntax error, haven't used Perl for too long, here is
a tested and working version:

#!/usr/bin/perl

$result = 0;
$filename = shift || die 'No file is specified.';
open(FILE, $filename) || die 'Could not open file.';

while ( <FILE> ) {
    chomp;
    if ($_ =~ /(t=\d+\s+)(-?\d+)/) {
        $result += $2;
        print $1, $result, "\n";
    } else {
        die 'Wrong format.';
    }
}


>
>
> --
> Chris
> chrishenry.ni at gmail.com
> chrishenry at google.co
>



-- 
Chris
chrishenry.ni at gmail.com
chrishenry at google.co



More information about the Slugnet mailing list