Sponsored Content
Top Forums Programming Regular Expression matching in PERL Post 302170538 by Yogesh Sawant on Tuesday 26th of February 2008 01:58:13 AM
Old 02-26-2008
instead of slurping the whole file in one array and then filtering out the lines, run a loop over the file and read one line at a time. here yoy can put the lines in respective arrays as per the category, with the help of regex
something like this would help you:
Code:
while (<>) {
    if (m/^\s*LENGTH:\s*/) {
        push (@length_array, $_);
    }
    elsif (m/^\s*TEXT:\s*/) {
        push (@text_array, $_);
    }
    elsif (m/^\s*COMMENT:\s*/) {
        push (@comment_array, $_);
    }
}


Last edited by Yogesh Sawant; 02-26-2008 at 03:05 AM.. Reason: added the sample code
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expression matching a new line

I have written a script to test some isdn links in my network and I am trying to format the output to be more readable. Each line of the output has a different number of digits as follows... Sitename , spid1 12345678901234 1234567890 1234567 , spid2 1234567890 1234567890 1234567 Sitename , ... (1 Reply)
Discussion started by: drheams
1 Replies

2. Shell Programming and Scripting

regular expression in perl

hi, i want to extract the sessionID from this line. QnA Session Id : here the output should be-- QnA_SessionID=128589 Thanks NT (3 Replies)
Discussion started by: namishtiwari
3 Replies

3. Shell Programming and Scripting

Help: Regular Expression for Negate Matching String

Hi guys, as per subject I am having problem with regular expressions. Example, if i got a string "javax.servlet.http.HttpServlet.service" that may occurred anywhere within a text file. How can I used the negate pattern matching of regular expression? I tried the below pattern but it... (4 Replies)
Discussion started by: DrivesMeCrazy
4 Replies

4. Shell Programming and Scripting

Regular expression matching in BASH (equivalent of =~ in Perl)

In Perl I can write a condition that evaluates a match expression like this: if ($foo =~ /^bar/) { do blah blah blah } How do I write this in shell? What I need to know is what operator do I use? The '=~' doesn't seem to fit. I've tried different operators, I browsed the man page for... (3 Replies)
Discussion started by: indiana_tas
3 Replies

5. Shell Programming and Scripting

Regular expression matching

Hi, I have a variable in my script that gets its value from a procstack output. It could be a number of any length, or it could just be a '1' with 0 or more white spaces around it. I would like to detect when this variable is just a 1 and not a 1234, for example. This is as far as I got: ... (3 Replies)
Discussion started by: tmf33uk
3 Replies

6. Shell Programming and Scripting

Matching single quote in a regular expression

I trying to match the begining of the following line in a perl script with a regular expression. $ENV{'ORACLE_HOME'} I tried this regluar expession: /\$ENV\{\'ORACLE_HOME\'\}/ Instead of match, I got a blank prompt > It seems to be a problem with the single quote. If I take it... (11 Replies)
Discussion started by: JC9672
11 Replies

7. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

8. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

9. UNIX for Dummies Questions & Answers

delete lines matching a regular expression

I have a very large file (over 700 million lines) that has some lines that I need to delete. An example of 5 lines of the file: HS4_80:8:2303:19153:193032 153 k80:138891 HS4_80:8:2105:5544:43174 89 k88:81949 165 k88:81949 323 0 * = 323 0 ... (6 Replies)
Discussion started by: pathunkathunk
6 Replies

10. Shell Programming and Scripting

regular expression matching whole words

Hi Consider the file this is a good line when running grep '\b(good|great|excellent)\b' file5 I expect it to match the line but it doesn't... what am i doing wrong?? (ultimately this regex will be in a awk script- just using grep to test it) Thanks, Storms (5 Replies)
Discussion started by: Storms
5 Replies
Syntax::Keyword::Gather(3pm)				User Contributed Perl Documentation			      Syntax::Keyword::Gather(3pm)

NAME
Syntax::Keyword::Gather - Provide a gather keyword VERSION
version 1.001000 SYNOPSIS
use Syntax::Keyword::Gather; my @list = gather { # Try to extract odd numbers and odd number names... for (@data) { if (/(one|three|five|seven|nine)$/) { take qq{'$_'} } elsif (/^d+$/ && $_ %2) { take $_ } } # But use the default set if there aren't any of either... take @defaults unless gathered; } or to use the stuff that Sub::Exporter gives us, try # this is a silly idea use syntax gather => { gather => { -as => 'bake' }, take => { -as => 'cake' }, }; my @vals = bake { cake (1...10) }; DESCRIPTION
Perl 6 provides a new control structure -- "gather" -- that allows lists to be constructed procedurally, without the need for a temporary variable. Within the block/closure controlled by a "gather" any call to "take" pushes that call's argument list to an implicitly created array. "take" returns the number of elements it took. This module implements that control structure. At the end of the block's execution, the "gather" returns the list of values stored in the array (in a list context) or a reference to the array (in a scalar context). For example, instead of writing: print do { my @wanted; while (my $line = <>) { push @wanted, $line if $line =~ /D/; push @wanted, -$line if some_other_condition($line); } push @wanted, 'EOF'; join q{, }, @wanted; }; instead we can write: print join q{, }, gather { while (my $line = <>) { take $line if $line =~ /D/; take -$line if some_other_condition($line); } take 'EOF'; } and instead of: my $text = do { my $string; while (<>) { next if /^#|^s*$/; last if /^__[DATA|END]__ $/; $string .= $_; } $string; }; we could write: my $text = join q{}, gather { while (<>) { next if /^#|^s*$/; last if /^__[DATA|END]__ $/; take $_; } }; There is also a third function -- "gathered" -- which returns a reference to the implicit array being gathered. This is useful for handling defaults: my @odds = gather { for @data { take $_ if $_ % 2; take to_num($_) if /[one|three|five|nine]$/; } take (1,3,5,7,9) unless gathered; } Note that -- as the example above implies -- the "gathered" function returns a special Perl 5 array reference that acts like a Perl 6 array reference in boolean, numeric, and string contexts. It's also handy for creating the implicit array by some process more complex than by simple sequential pushing. For example, if we needed to prepend a count of non-numeric items: my @odds = gather { for @data { take $_ if $_ %2; take to_num($_) if /[one|three|five|seven|nine]$/; } unshift gathered, +grep(/[a-z]/i, @data); } Conceptually "gather"/"take" is the generalized form from which both "map" and "grep" derive. That is, we could implement those two functions as: sub map (&@) { my $coderef = shift; my @list = @{shift @_}; return gather { take $coderef->($_) for (@list) }; } sub grep (&@) { my $coderef = shift; my @list = @{shift @_}; return gather { take $_ if $coderef->($_) for @list }; } A "gather" is also a very handy way of short-circuiting the construction of a list. For example, suppose we wanted to generate a single sorted list of lines from two sorted files, but only up to the first line they have in common. We could gather the lines like this: my @merged_diff = gather { my $a = <$fh_a>; my $b = <$fh_b>; while(1) { if ( defined $a && defined $b ) { if ($a eq $b) { last } # Duplicate means end of list elsif ($a lt $b) { take $a; $a = <$fh_a>; } else { take $b; $b = <$fh_b>; } } elsif (defined $a) { take $a; $a = <$fh_a>; } elsif (defined $b) { take $b; $b = <$fh_b>; } else { last } } } NAME
Syntax::Keyword::Gather - Implements the Perl 6 'gather/take' control structure in Perl 5 HISTORY
This module was forked from Damian Conway's Perl6::Gather for a few reasons. to avoid the slightly incendiary name =item to avoid the use of the Perl6::Exporter =item ~ doesn't overload to mean string context =item to no longer takes the current topic ($_) The last item is actually due to an unintended side-effect of the fact that if "take" has an array of zero length it takes $_, which is suprising at the very least. I'll fix that issue if I can. BUGS AND IRRITATIONS
It would be nice to be able to code the default case as: my @odds = gather { for (@data) { take if $_ % 2; take to_num($_) if /(?:one|three|five|nine)z/; } } or (1,3,5,7,9); but Perl 5's "or" imposes a scalar context on its left argument. This is arguably a bug and definitely an irritation. AUTHORS
o Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com> o Damian Conway COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Arthur Axel "fREW" Schmidt. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.10.1 2011-02-25 Syntax::Keyword::Gather(3pm)
All times are GMT -4. The time now is 07:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy