Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Matching, Desperate Newbie. 3

Status
Not open for further replies.

hughed00

Technical User
Jan 15, 2001
43
US
I have a very LARGE text file which has interspersed 2000 unique codes throughout alot of junk I don't care about. The task of identifying these codes was dumped on my desk.

My first thought was "Why didn't you learn Perl when you had some free time?". My second was, this should be an easy task, grab the "Learning Perl on Win32 Systems" off the shelf and you can find an answer.

Well, apparantly I am to dense and have too little experience with programming to achieve what I need to do before my boss comes looking for the list of codes. I am sure this is an real newbie/idiot question, but here goes;

source.txt contains strings that are always 18 characters in length. They also always begin with the characters "EANCM" followed by any 13 numbers. They also always have at least one whitespace after them. I need to parse the file and copy them out to another file.

Any help will be appreciated and I intend to continue to pursue my adventures in Perl, I am just jammed up for time on this one and deperately need help.

 
what exactly is it you need out of the file?

if you need the 18 character string you can use something like this:

#!/usr/bin/perl

open(FILE, "source.txt");
@file = <FILE>;
close(FILE);

open(NEW, &quot;>fileyouwanttocreate.txt&quot;);
foreach $file (@file)
{
if($file =~ /(EANCM\d{13}\s+?)/g)
{
print NEW &quot;$1\n&quot;;
}
}
close(NEW);

adam@aauser.com
 
Yes, I need all the strings EANCM... . Your bit of code works fine, until the file gets big and then the program bombs out at some point with an &quot;Out of Memory!&quot;

The source file is ~24MB.

Thanks for your help!! Any ideas on the the memory problem (he goes to the well again)?

Again, thanks.

--Dave
 
try:

#!/usr/bin/perl

open(FILE, &quot;source.txt&quot;);
open(NEW, &quot;>fileyouwanttocreate.txt&quot;);
while(<FILE>)
{
if($_ =~ /(EANCM\d{13}\s+?)/g)
{
print NEW &quot;$1\n&quot;;
}
}
close(FILE);
close(NEW); adam@aauser.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top