jumdesumit:

Well, while you have several lines that match the pattern ([N01]{39}), none of the lines stop after the last [N01] match, they all have a semicolon and more text after them.

Try changing the line to not require that the line end immediately after that, like this:

if ( $line =~ /([N01]{39})/ )

or alternatively, specify that the string should be followed with a semicolon:

if ( $line =~ /([N01]{39});/ )

It would be helpful if you'd re-read perldoc perlre and verify that your expressions are saying what you really mean.

When I have a problem with a pattern, I usually do something akin to putting this at the start of the file:

use strict; use warnings; while (my $line = <DATA>) { if ($line =~ /([N01]{39})$/) { print "line $.: <$1>\n"; } } __DATA__

And then run the file as a perl script. Then you can tweak your regex until you get what you're looking for. For example, when I ran it with the above bit of code on the front, I got this:

Roboticus@Waubli ~ $ perl pm1208422.pl Roboticus@Waubli ~ $

I then removed the '$' from your condition, re-ran it and got:

Roboticus@Waubli ~ $ perl pm1208422.pl line 194: <NN0NNNNNNNNNNN0NNNNNNNNNNNNNNNNNNNNNNNN> line 196: <NN0NNNNNNNNNNN1NNNNNNNNNNNNNNNNNNNNNNNN> line 199: <NN0NNNNNNNNNNN1NNNNNNNNNNNNNNNNNNNNNNNN> line 201: <NN0NNNNNNNNNNN0NNNNNNNNNNNNNNNNNNNNNNNN> line 204: <NN0NNNNNNNNNNNNNNNNN0NNNNNNNNNNNNNNNNNN> line 206: <NN0NNNNNNNNNNNNNNNNN1NNNNNNNNNNNNNNNNNN> line 209: <NN0NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0NN> line 211: <NN00NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN1NN> line 214: <NN0NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0NNN> line 216: <NN0NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN1NNN> . . . et cetera . . .

Update: Fixed wording and formatting (code tags so square brackets don't linkify) in first sentence.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re^3: How should I Extract data from text file. Pattern I have to extract should not repeated one. by roboticus
in thread How should I Extract data from text file. Pattern I have to extract should not repeated one. by jumdesumit

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.