I have to do stuff like this fairly often with a bunch of different format files so I just keep this around as a generic framework. I modify the regex statements, delimiter and number of fields as necessary but it's pretty much the same program every time.

Here's what I use - lifted almost completely from various posts here on PM...

#!/usr/bin/perl use warnings; use strict; open INFILE, "< $ARGV[0]" or die "Could not open file $ARGV[0]: $!"; until(eof INFILE) { my $fn = $ARGV[0]; open OUTFILE, "> $fn.filtered" or die "Could not open file $fn.filtered $!"; while(<INFILE>) { my @line = split /\t/; $line[0] =~ s/(.{1,6}).*/$1/; $line[3] =~ s/(.{1,6}).*/$1/; $line[6] = $line[6]."\n"; $_ = join("\t", $line[0], $line[1], $line[2], $line[3], $line[ +4], $line[5], $line[6]); print OUTFILE; } }
BTW, this code processed an 8 million line/385 mb file in about 33 minutes the other day on my old, anemic Sun Ultra 5 workstation. No brownouts, no sweat. System load never got above 1.05.

Jack


In reply to Re: Pimp My RegEx by jcoxen
in thread Pimp My RegEx by heathen

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.