I have to parse a 9GB textfile, I only want to keep lines containing certain strings (UniProt IDs, like P40303 or Q99436).

I would think that the first thing is to decide whether you even need to write any kind of program or not (Perl or otherwise)! I figure you are on a Unix type machine. There is a standard program that does what you want called "grep".

Type "man grep", "man egrep" at the command line to get some hints. "grep P40303 *.datafile" will output all lines containing P40303 in all files ending in ".datafile".

But if you must, here is some Perl code...

#!/usr/bin/perl -w use strict; my @items = qw (P40303 Q99436 X1234 W9765543); my $regex = join ("|",@items); print $regex; # to see what this does # put something like this in your "grep" # P40303|Q99436|X1234|W976554 while (<>) { print if m/$regex/; } __END__ Perl 5.10 is pretty smart. I think that the /o option is not necessary here. I don't think more complex syntax's are either.

In reply to Re: Large File Parsing by Marshall
in thread Large File Parsing by RobertCraven

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.