![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with a perl subroutine regex | jmd2004 | Shell Programming and Scripting | 11 | 12-03-2008 01:11 AM |
| Perl REGEX | evilfreakz | Shell Programming and Scripting | 4 | 11-05-2008 05:20 AM |
| regex on first string in a variable. | Endo | UNIX for Dummies Questions & Answers | 1 | 09-03-2008 08:28 AM |
| q with Perl Regex | JamesGoh | Shell Programming and Scripting | 8 | 07-24-2008 02:23 AM |
| Perl regex question | figaro | Shell Programming and Scripting | 10 | 07-18-2008 04:45 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Perl Regex string opperation
I'm working on a basic log parser in perl. Input file looks like: Code:
len: 120713 foo bar file size of: testdir1/testdir1/testdir1/testdir1/testfile0 is 120713 Of course there are tens of thousands of lines... I'm trying to compare the len and filesize values. Code:
#!/usr/bin/perl
use strict;
use warnings;
open FH, "/home/dkozel/testresults" or die $!;
my @lines = <FH>;
my @lengths = grep(/len:/, @lines);
my @sizes = grep(/file size of/, @lines);
for( my $index = 0; $index < scalar(@lengths); $index++) {
my $length = $lengths[$index];
$length =~ s/\d+$//;
my $size = $sizes[$index];
$size =~ s/\d+$//;
if ($length != $size) {
print "$len doesn't equal $size";
}
}
close FH;
$size =~ s/\d+$// does exactly the opposite of what I want. I tried using !~ but that didn't return anything. Tips? Many thanks. ---------- Post updated at 05:23 PM ---------- Previous update was at 04:51 PM ---------- Found a working solution. It seems strange to have to use the if statements given that I know that the result is there. Code:
#!/usr/bin/perl
use strict;
use warnings;
open FH, "/home/dkozel/testresults" or die $!;
my @lines = <FH>;
my @lens = grep(/len:/, @lines);
my @sizes = grep(/file size of/, @lines);
for( my $index = 0; $index < scalar(@lens); $index++) {
my $len = $1 if ( $lens[$index] =~ /(\d+)$/ );
my $size = $1 if ( $sizes[$index] =~ /(\d+)$/ );
print "$len is not equal to $size\n" if $len != $size;
}
close FH;
Is there a way to simply directly assign it without the If statement? Thanks. |
|
||||
|
The s/// operator is for substitution. You are telling it to replace '\d+$' with ''. Since you're trying to match the numbers and keep just them, you need to remove everything else and carry the numbers forward. Also, you need to use chomp() to remove the newline from each line. Here is the modified code to make this work. I added an else statement at the end just to show it gives the correct result. Code:
for ( my $index = 0 ; $index < scalar(@lengths) ; $index++ ) {
chomp( my $length = $lengths[$index] );
$length =~ s/^.*: (\d+)$/$1/;
chomp( my $size = $sizes[$index] );
$size =~ s/^.* (\d+)$/$1/;
if ( $length != $size ) {
print "$length doesn't equal $size\n";
}
else {
print "$length are equal $size\n";
}
}
|
![]() |
| Bookmarks |
| Tags |
| perl, regex |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|