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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl coding needed..thanks in advance 1

Status
Not open for further replies.

abcxyz778

Programmer
Joined
Jun 18, 2006
Messages
1
Location
US
I have a file with about a 1000 or more records in the following format(this is one record,imagine it all in one line):

5/31/2006 0:00:00|"EMS"|"mBlox"|"25255"|"Alltel"|"0014172290558"|38.00|43.00|"Sexy"|"How bout tellin me yah name now =)"|"Hi there sexy thing.. wanna talk?"|4/30/2006 23:40:48|4/30/2006 0:00:00|5/1/2006 17:51:32|5/1/2006 0:00:00|4/30/2006 23:43:22|4/30/2006 0:00:00|"out"||37.62|42.57|"31 - 60 Days"|"21 - 40"|"Less than $50.99"|31.00

But some of the records are cut off for some reason like this(it's cut off after "Sexy and the rest of it shows as a new record):

5/31/2006 0:00:00|"EMS"|"mBlox"|"27772"|"Alltel"|"0014802043951"|1.00|1.00|"Sexy
''CLOWNIN AROUND''"|"ohh ok here from New Mexico how old are you whar is your name?"|"ohh ok here from New Mexico how old are you whar is your name?"|5/1/2006 7:36:49|5/1/2006 0:00:00|5/1/2006 9:58:25|5/1/2006 0:00:00|5/1/2006 9:58:25|5/1/2006 0:00:00|"out"||0.99|0.99|"Less than 30 Days"|"Less than 20"|"Less than $50.99"|30.00

What i want to do now is write a perl script which processes this file so that when each line in the file does not start with the date,then merge it with the previous line i.e., the ENTER key character needs to be gone...hope you understand my question.
I'm new to PERL and have no idea on how to implement this...can someone help.Thank you very much!
 
Try this:
Code:
#!/usr/bin/perl -w
use strict;

# This script processes records that are damaged by joining lines that are not
# prefixed with a date to the one above that is.

my $record = "";
while(<>) {
    chomp;
    if(m!^\d+/\d+/\d+\s+\d+:\d+:\d+!) {
        # Found a record that is prefixed so eject any old one and start anew.
        print $record, "\n" if($record);
        $record = $_;
    } else {
        # Found a partial record so append to the last one.
        $record .= $_;
    }
}
# There may be a trailing record buffered so eject that.
print $record, "\n" if($record);


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top