Sponsored Content
Full Discussion: Perl Regex string opperation
Top Forums Shell Programming and Scripting Perl Regex string opperation Post 302327147 by dkozel on Friday 19th of June 2009 08:23:20 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ZIP_SET_FILE_EXTRA(3)					     Library Functions Manual					     ZIP_SET_FILE_EXTRA(3)

NAME
zip_set_file_extra - set extra field for file in zip LIBRARY
libzip (-lzip) SYNOPSIS
#include <zip.h> int zip_set_file_extra(struct zip *archive, zip_uint64_t index); "const char *extra" "int len" DESCRIPTION
The zip_set_file_extra function sets the extra field for the file at position index in the zip archive to extra of length len. If extra is NULL and len is 0, the file extra field will be removed. RETURN VALUES
Upon successful completion 0 is returned. Otherwise, -1 is returned and the error information in archive is set to indicate the error. ERRORS
zip_set_file_extra fails if: [ZIP_ER_INVAL] index is not a valid file index in archive, or len is less than 0 or longer than the maximum extra field length in a zip file (65535). [ZIP_ER_MEMORY] Required memory could not be allocated. SEE ALSO
libzip(3), zip_get_file_extra(3) AUTHORS
Dieter Baron <dillo@giga.or.at> and Thomas Klausner <tk@giga.or.at> NiH February 13, 2011 ZIP_SET_FILE_EXTRA(3)
All times are GMT -4. The time now is 12:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy