The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-19-2009
dkozel dkozel is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 9
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.
  #2 (permalink)  
Old 06-19-2009
bwhitehd bwhitehd is offline
Registered User
  
 

Join Date: Jun 2009
Location: Texas
Posts: 28
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";
    }
}

  #3 (permalink)  
Old 06-19-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,558

Code:
while (<>){
 if ( $_ =~ /^len:/ ){
     @m = split /:\s+/,$_;
     $len=$m[-1];
 }
 if ($_ =~ /file size of/){
     @n = split /\s+/,$_;
     if ($n[-1] == $len){
       .....
     }
 }
}

Reply

Bookmarks

Tags
perl, regex

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0