mpatharkar has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I have one input file "pick no.txt" from which i have to find perticuler pattern and print it in to output file "Numbers.txt".

The input file is "pick no.txt"


Number:
Number-1999-1011
----------------------------------------
Number:
Number-1999-0696
----------------------------------------
Number:
Number-1999-0833,Number-2004-0786,Number-2004-0747,Number-2004-075,Number-2004-0748,Number-2004-0809
----------------------------------------
Number:
Number-2000-1209
----------------------------------------

The perl script i made for this is..
#!/usr/bin/perl open (RD, "pick no.txt") or die"Could not open file"; @Read = <RD>; close (RD); open (WR1, ">>Numbers.txt") or die"Could not open file"; for ($i=0; $i<= $#Read; $i++) { if($Read[$i] =~/^Number:/and $Read[$i+1]=~/(\w\w\w\w\w\w-\d\d\d\d-\d\ +d\d\d)/) { $Number = $1; $Number1 = substr ($Number, 7, 9); print "$Number1\n"; print WR1"$Number1\n"; } }


The output of the script is Numbers.txt..

1999-1011
1999-0696
1999-0833
2000-1209
The problem with this is it is not useful to search pattern occurance more than once in the same line. The numers after 1999-0833 is not shown in thw output.
Any suggestions for this?


--Mahesh

Replies are listed 'Best First'.
Re: how to search a pattern occured more than once in line
by moritz (Cardinal) on Jan 29, 2008 at 07:45 UTC
    You can use the /g modifier, and replace the if by a while (untested):
    for (@Read){ while (m/Number-(\d{4}-\d{4})/g){ print "$1\n"; print WR1 "$1\n"; } }

    By putting the parenthesis only around these parts of the regex that you want to capture, you avoid the substr alltogether.

    \d{4} is short for \d\d\d\d

Re: how to search a pattern occured more than once in line
by svenXY (Deacon) on Jan 29, 2008 at 07:58 UTC
    Hi,
    I'd go with split:
    #!/usr/bin/perl use strict; use warnings; my @Read = <DATA>; for (@Read){ next unless /^Number-/; chomp; $_ =~ s/Number-//g; my @numbers = split(/,/); print join("\n", @numbers),"\n"; } __DATA__ Number: Number-1999-1011 ---------------------------------------- Number: Number-1999-0696 ---------------------------------------- Number: Number-1999-0833,Number-2004-0786,Number-2004-0747,Number-2004-075,Num +ber-2004-0748,Number-2004-0809 ---------------------------------------- Number: Number-2000-1209 ----------------------------------------
    output:
    1999-1011 1999-0696 1999-0833 2004-0786 2004-0747 2004-075 2004-0748 2004-0809 2000-1209

    Regards,
    svenXY

    update: I personally prefer the solution moritz++ offered above. It's also more regex-like - as was asked for in the OP.

Re: how to search a pattern occured more than once in line
by parv (Parson) on Jan 29, 2008 at 07:49 UTC