Link to home
Start Free TrialLog in
Avatar of stevecamp
stevecamp

asked on

struggling to conquer a split/reg. expression issue with records in a flat file

Hi

I have a flat file database that contains records in the following format:

==================================================================
UNSUCCESSFUL POST!
Date/Time of post attempt: Tue Mar 11 11:34:41 2003
IP Address: 0.0.0.0
Browser: Mozilla/4.0 (compatible; MSIE 5.01;)
Error Message: Missing data. Please ensure you have entered a message.
==================================================================

The above is ONE single record, on mutiple lines of the file. Each record is written exactly as above on a new line each time. What I would like to do in my display script, is "massage" the display so that each record is displayed as fields on a single line, not multiple lines. Somewhat like below:

Message             Date/Time                  IP        Browser        Error
UNSUCCESSFUL POST!  Tue Mar 11 11:34:41 2003   0.0.0.0   Mozilla/4.0 (compatible; MSIE 5.01)

I have tried multiple ways to split on the newline character and cannot fathom it. For example:

                $row = $_; # this reads in the whole file
                chop $row;
                @fields = split (/\n/, $row); # trying to split on newline
                $fields[$i] =~ s/\n/ /g; # also tried substituting newline with whitespace.

Each time, it just displays the record on mutiple lines. I can change the file writing routines to write the records differently so that it is continuous records, delimited by a special character for instance, and I will do that if needs must but i'd like to be able to look at the flat file in a relatively pleasing format as well as display it for all my "users" like I stated above. If anyone could help out, that would be much apreciated.

Regards,
Avatar of chapatti
chapatti
Flag of Canada image

Hi Steve,

"chop" removes the newline character on line2 so split cannot produce the result you expect.

I haven't tried your example but if this doesn't fix it please let me know.

Cheers,

chapatti
Avatar of stevecamp
stevecamp

ASKER

Hi Chapatti

I gave that a try (cursing myself for forgetting that fact about chop!) but it made no difference. Any further help would be great.

Thanks again.
Sorry Steve,

I was not thinking clearly earlier...

The chop takes just one newline off at the end - and that should not be a problem.

Does this file have only one record in it?

Maybe try splitting the file into records along the multiple =s then remove the =s and try the splitting along the newlines again?

L8r,

chapatti
Sorry Steve,

I was not thinking clearly earlier...

The chop takes just one newline off at the end - and that should not be a problem.

Does this file have only one record in it?

Maybe try splitting the file into records along the multiple =s then remove the =s and try the splitting along the newlines again?

L8r,

chapatti
Avatar of ozo
$_ = join'',<>;
while( /(\w.*)\s+Date\/Time of post attempt:(.*)\s+IP Address:(.*)\s+Browser:(.*)\s+Error Message:(.*)/g ){
    print $1,$2,$3,$4,$5,"\n";
}
IF you could give me just a few sample entries exactly how they occur in your DB files and outline more clearly your overall objective I can help you.
 
Have you actually parsed out the field names?

Date/Time of post attempt:
IP Address:
Browser:
Error Message:

I'd like to write some code to process your files, if you just want. Yes, that's right, I do it for fun!!!

No guarantees...
Thanks for all your replies. Re: perlyhead - Each entry looks exaclty like the one above on a new line each time i.e

==================================================================
UNSUCCESSFUL POST!
Date/Time of post attempt: Tue Mar 11 11:34:41 2003
IP Address: 0.0.0.0
Browser: Mozilla/4.0 (compatible; MSIE 5.01;)
Error Message: Missing data. Please ensure you have entered a message.
==================================================================

==================================================================
UNSUCCESSFUL POST!
Date/Time of post attempt: Tue Mar 11 11:34:41 2003
IP Address: 0.0.0.0
Browser: Mozilla/4.0 (compatible; MSIE 5.01;)
Error Message: Missing data. Please ensure you have entered a message.
==================================================================

The only thing that would change is the "Error Message" portion, and then it's only the text that changes, basically it's a log file. So, I want to ignore the "=" character (already taken care of) and then I basically want to be able to split on newline so that when this file is displayed, they are all on one line like above. So in essence, several lines make up the record, and I want to display those lines on just one line. Here is a portion of my display code (I can post the entire script if it will help):

$total = 0;
$rowcount = 0;
$startVal = $in{'start'} - 1;
$endVal = $startVal + $numOfRecordsPerPage;
$ranking = $in{'start'};

####### Open the DB and format the data #######
open (DATABASE, "../../htdocs/wb_log_test");

while (<DATABASE>)
{
        if (($rowcount >= $startVal) && ($rowcount < $endVal)) {
                $row = $_;
                ##chop $row;
                @fields = split (/\n/, $row);
                ##$fields[$i] =~ s/  +/ /g;
                print"<tr>\n";
                for ($i = 0; $i < $numOfFields; $i++) {
                $fields[$i] =~ s/.*-/ /g;
                $fields[$i] =~ s/.*=/ /g;
                $fields[1] =~ s/.*t:/ /g;
                $fields[2] =~ s/.*:/ /g;
                $fields[3] =~ s/.*:/ /g;
                $fields[4] =~ s/.*:/ /g;
                print"<td align=middle><font size=$fontsize>$fields[$i]</font></
td>\n";
                }
                print"</tr>\n";
                $ranking++;
        }
        $rowcount++;
        $total++;
}
close(DATABASE);
print "</table>\n";

@fields is the array holding the file contents and I want to split it up from mutiple lines into one single line. Is that clearer?

Let me know if you need anything else.

Thanks again to all who responded.

Thanks for all your replies. Re: perlyhead - Each entry looks exaclty like the one above on a new line each time i.e

==================================================================
UNSUCCESSFUL POST!
Date/Time of post attempt: Tue Mar 11 11:34:41 2003
IP Address: 0.0.0.0
Browser: Mozilla/4.0 (compatible; MSIE 5.01;)
Error Message: Missing data. Please ensure you have entered a message.
==================================================================

==================================================================
UNSUCCESSFUL POST!
Date/Time of post attempt: Tue Mar 11 11:34:41 2003
IP Address: 0.0.0.0
Browser: Mozilla/4.0 (compatible; MSIE 5.01;)
Error Message: Missing data. Please ensure you have entered a message.
==================================================================

The only thing that would change is the "Error Message" portion, and then it's only the text that changes, basically it's a log file. So, I want to ignore the "=" character (already taken care of) and then I basically want to be able to split on newline so that when this file is displayed, they are all on one line like above. So in essence, several lines make up the record, and I want to display those lines on just one line. Here is a portion of my display code (I can post the entire script if it will help):

$total = 0;
$rowcount = 0;
$startVal = $in{'start'} - 1;
$endVal = $startVal + $numOfRecordsPerPage;
$ranking = $in{'start'};

####### Open the DB and format the data #######
open (DATABASE, "../../htdocs/wb_log_test");

while (<DATABASE>)
{
        if (($rowcount >= $startVal) && ($rowcount < $endVal)) {
                $row = $_;
                ##chop $row;
                @fields = split (/\n/, $row);
                ##$fields[$i] =~ s/  +/ /g;
                print"<tr>\n";
                for ($i = 0; $i < $numOfFields; $i++) {
                $fields[$i] =~ s/.*-/ /g;
                $fields[$i] =~ s/.*=/ /g;
                $fields[1] =~ s/.*t:/ /g;
                $fields[2] =~ s/.*:/ /g;
                $fields[3] =~ s/.*:/ /g;
                $fields[4] =~ s/.*:/ /g;
                print"<td align=middle><font size=$fontsize>$fields[$i]</font></
td>\n";
                }
                print"</tr>\n";
                $ranking++;
        }
        $rowcount++;
        $total++;
}
close(DATABASE);
print "</table>\n";

@fields is the array holding the file contents and I want to split it up from mutiple lines into one single line. Is that clearer?

Let me know if you need anything else.

Thanks again to all who responded.

ASKER CERTIFIED SOLUTION
Avatar of chapatti
chapatti
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Tanja, that worked really well thank you. Thanks to all of you who responded.

Regards,

Stephen
anytime, thanks for the points!  :-)