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

Hello all,

I am working with a tab separated text file and I need to do some s/r in it, followed by writing the file into another text file if there is tag <EMP>. I used the following code.

while(<F1>) { #some conversions here if($_ =~ /<EMP>/g) { print F2 $_; } }

When I did this in the file F2, there was only the <EMP> and the word following it. I need to print the whole line of text if <EMP> is encountered. Below are few lines from the files which I am working with.

PT 23 0.05 PK 27 0.21 <EMP>P7 22 0.12 PP 21 0.22 P3 21 0.20 P1 21 0.92 <EMP>P1 23 0.82 P2 22 0.12

In my output file, I should have only the two lines which begins with <EMP>.

Replies are listed 'Best First'.
Re: Working with a text file
by citromatik (Curate) on Jun 05, 2007 at 11:26 UTC

    If your files are not very big you can solve the problem with:

    print F2 grep {/<EMP>/} <F1> ;

    This will upload F1, and "grep" all the lines that contain "<EMP>"

    A more gentle solution (that doesn't upload F1 in memory):

    while (<F1>){ #Some conversions here print F2 if /<EMP>/; }

    Hope this helps!

    citromatik

    PD: Your code seems to be valid. check if you are opening F2 correctly and try removing it before running the script again

Re: Working with a text file
by GrandFather (Saint) on Jun 05, 2007 at 11:43 UTC
    use warnings; use strict; while(<DATA>) { #some conversions here if($_ =~ /<EMP>/g) { print; } } __DATA__ PT 23 0.05 PK 27 0.21 <EMP>P7 22 0.12 PP 21 0.22 P3 21 0.20 P1 21 0.92 <EMP>P1 23 0.82 P2 22 0.12

    Prints:

    <EMP>P7 22 0.12 <EMP>P1 23 0.82

    as expected. What's the problem?

    Added:

    Oh, and the /g (global) switch is not required - the first match in the line should be sufficient.


    DWIM is Perl's answer to Gödel
Re: Working with a text file
by ww (Archbishop) on Jun 05, 2007 at 12:13 UTC
    update: s/point is/points are/;

    GrandFather's points are well taken; It's good practice to avoid complicating your regexen unnecessarily.

    ... and unless citromatik's suggestion about handling F2 hits the nail on the head, it looks as though your problem is somewhere inside

    #some conversions here