Perl Regex string opperation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Regex string opperation
# 1  
Old 06-19-2009
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  
Old 06-19-2009
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  
Old 06-19-2009
Code:
while (<>){
 if ( $_ =~ /^len:/ ){
     @m = split /:\s+/,$_;
     $len=$m[-1];
 }
 if ($_ =~ /file size of/){
     @n = split /\s+/,$_;
     if ($n[-1] == $len){
       .....
     }
 }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with understanding this regex in a Perl script parsing a 'complex' string

Hi, I need some guidance with understanding this Perl script below. I am not the author of the script and the author has not leave any documentation. I supposed it is meant to be 'easy' if you're a Perl or regex guru. I am having problem understanding what regex to use :confused: The script does... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Programming

Perl regex

Hello folks, Looking for a quick help on regex in my perl script. here's the string i want to parse and get the 2nd field out of it. $str = " 2013-08-07 12:29 Beta ACTIVE"; I want to extract 'Beta' out of this string. This string will keep on changing... (2 Replies)
Discussion started by: jhamaks
2 Replies

4. Programming

Perl regex

HI, I'm new to perl and need simple regex for reading a file using my perl script. The text file reads as - filename=/pot/uio/current/myremificates.txt certificates=/pot/uio/current/userdir/conf/user/gamma/settings/security/... (3 Replies)
Discussion started by: jhamaks
3 Replies

5. Shell Programming and Scripting

perl regex string match issue..kindly help

i have a script in which i need to skip comments, and i am able to achieve it partially... IN text file: {**************************** {test : test...test } Script: while (<$fh>) { push ( @data, $_); } if ( $data =~ m/(^{\*+$)/ ){ } With the above match i am... (5 Replies)
Discussion started by: avskrm
5 Replies

6. Shell Programming and Scripting

Perl: Regex, string matching

Hi, I've a logfile which i need to parse and get the logs depending upon the user input. here, i'm providing an option to enter the string which can be matched with the log entries. e.g. one of the logfile entry reads like this - $str = " mpgw(BLUESOAPFramework):... (6 Replies)
Discussion started by: butterfly20
6 Replies

7. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

8. Shell Programming and Scripting

filtering out duplicate substrings, regex string from a string

My input contains a single word lines. From each line data.txt prjtestBlaBlatestBlaBla prjthisBlaBlathisBlaBla prjthatBlaBladpthatBlaBla prjgoodBlaBladpgoodBlaBla prjgood1BlaBla123dpgood1BlaBla123 Desired output --> data_out.txt prjtestBlaBla prjthisBlaBla... (8 Replies)
Discussion started by: kchinnam
8 Replies

9. Shell Programming and Scripting

Perl REGEX - How do extract a string in a line?

Hi Guys, In the following line: cn=portal.090710.191533.428571000,cn=groups,dc=mp,dc=rj,dc=gov,dc=br I need to extract this string: portal.090710.191533.428571000 As you can see this string always will be bettween "cn=" and "," strings. Someone know one regular expression to... (4 Replies)
Discussion started by: maverick-ski
4 Replies

10. Shell Programming and Scripting

Perl REGEX

Hi, Can anyone help me to find regular expression for the following in Perl? "The string can only contain lower case letters (a-z) and no more than one of any letter." For example: "table" is accepted, whether "dude" is not. I have coded like this: $str = "table"; if ($str =~ m/\b()\b/) {... (4 Replies)
Discussion started by: evilfreakz
4 Replies
Login or Register to Ask a Question