Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: File search using Grep

by poolpi (Hermit)
on Jun 26, 2009 at 10:08 UTC ( #775000=note: print w/replies, xml ) Need Help??

in reply to File search using Grep

Better read the file line by line:

perl -e ' open(FILE, '<','/tmp/Config1'); while(<FILE>){ print if /9344151299/i }; close FILE; ' # Or perl -ne 'print if /9344151299/i' /tmp/Config1


'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
Update: -options and second solution

Replies are listed 'Best First'.
Re^2: File search using Grep
by johngg (Canon) on Jun 26, 2009 at 10:20 UTC

    I'm not sure whether the OP's use of grep implies that there might be more than one record with that ID but, if not, it might be an idea to avoid reading the rest of the file once you've found the record. Something like

    $ perl -ne ' > next unless /9344151299/; > print; > last;' /tmp/Config1

    I hope this is of interest.



      Hi, Thanks to all your suggestions; My problem is just to get the information particular to an account using his unique id; some of them will not be in the list also. We are expecting that this file will have millions of records as the accounts grow. So thinking of whether to store these records in hash each time the script is called or to just grep it? Thanks Priya

        Is there any reason not to use database?

        Using grep or perl -ne 'print if /<expr>/' is linear search, which is an operation that will scale with the size of the file. If the file is double the size, you can expect it to take twice as long time.

        If you use a different storage format, such as dbm or the like, you can improve this performance significantly, but it has nothing with the way the file is being read - its all about the format of the file.

        If you still want to keep the flat file you have described, you can build an index in a separate and use that for look up. One tool that will allow you to index the data is Berkeley DB for which there is the DB_File module.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://775000]
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2023-02-02 15:48 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (19 votes). Check out past polls.