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

Hi, I have a file with 1823194 lines of text which I need to search through. Each line is formatted like this:

ID</td><td>10103</td><td>TITLE</td><td>NAME</td><td></td><td>LAST NAME</td><td></td><td>POSTALCODE</td><td>EMAIL</td><td>PRODUCT</td><td>3

Every line has a different ID, so no line is the same. I tried using native perl code to loop through the whole file. This, takes a very long time, and kept running out of memory. Linux' grep was way faster.

I'm succesfully using grep, but grep doesnt stop searching through the file until it reaches the last line.

How can I have perl stop a system() function when it has found the line I need?

TIA,
art0rz

Replies are listed 'Best First'.
Re: Stopping system()
by Corion (Patriarch) on Oct 30, 2007 at 11:33 UTC

    How are you scanning through your file? It sounds as if you are reading the whole file into memory and then scanning through the lines, which will be slow. The following is a reimplementation of grep that quits after the first line matching the regular expression has been found:

    while (<>) { /...whatnot.../ and print, exit };

    But the whole thing sounds to me as if you'd be better off with a database or at least a different data structure, like a tied hash - but all those suggestions are moot as you don't show any code to work with.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Stopping system()
by derby (Abbot) on Oct 30, 2007 at 13:02 UTC

    Well ... I'm sure there are ways to make your native perl code loop without running out of memory ... but to directly answer your question, look at the man page for grep.

    -m NUM, --max-count=NUM Stop reading a file after NUM matching lines. ....
    -derby
      I was going to mention that. Also you can pipe your results to head.
      grep BLAH INFILE.txt | head -1
      Either way, it'll stop processing as soon as a match has been found. Also awk could be used. It's *nix, so there are lots of ways of doing it. If grep does what he wants, I say use it.
      I used to drive a Heisenbergmobile, but everyone I looked at the speedometer, I got lost.