match regex split

M

MisterX

OK, I've tried googling, reading many perl books, but some things I
just dont get.

The problem: Parsing a simple backup.log. Below are the lines that I
need to extract. Note the leading space on the 1st line:

Server date/time: 01/03/05 20:05:57 Last access: 01/02/05
20:06:25
Total number of bytes transferred: 27.42 GB
Elapsed processing time: 01:04:07


The script thus far:

while (<LOGFILE>)
{
if ($_=~/Server date/ || /Total number of bytes/ || /Elapsed
processing time/ || /Elapsed processing time/)
{
($_=~ s/^\s+//);
print $_;
}
}
====================
Preferably I'd like it formatted (printf I suppose) nicely.
I know it's not pretty, what can i say I'm a noob. Any help is VERY
appreciated.
 
G

Gunnar Hjalmarsson

MisterX said:
OK, I've tried googling, reading many perl books,

Which books did you read?
but some things I just dont get.

The problem: Parsing a simple backup.log.

I think there is another problem: Your description of what the problem
is is insufficient. A problem description should (at least) include the
program output and how that differs from the expected and/or desired output.
Below are the lines that I
need to extract. Note the leading space on the 1st line:

There is no leading space below (but that may be Google's fault).
Server date/time: 01/03/05 20:05:57 Last access: 01/02/05 20:06:25
Total number of bytes transferred: 27.42 GB
Elapsed processing time: 01:04:07

The script thus far:

while (<LOGFILE>)
{
if ($_=~/Server date/ || /Total number of bytes/ || /Elapsed
processing time/ || /Elapsed processing time/)
{
($_=~ s/^\s+//);
print $_;
}
}

That script seems to do what you reasonably can expect it to do, i.e. it
prints those three lines.

What would you want it to do?
 
M

Michele Dondi

The script thus far:

while (<LOGFILE>)
{
if ($_=~/Server date/ || /Total number of bytes/ || /Elapsed
processing time/ || /Elapsed processing time/)
{
($_=~ s/^\s+//);
print $_;
}
}

As a side note: (i) it's preferrable to adopt an effective indenting
style, (ii) the whole point of $_ is to be implicit when possible.
Thus your snippet may look like:

while (<LOGFILE>) {
next unless /Server date/ ||
/Total number of bytes/ ||
/Elapsed processing time/ ||
/Elapsed processing time/;
s/^\s+//;
print;
}


Michele
 
J

Joe Smith

MisterX said:
Server date/time: 01/03/05 20:05:57 Last access: 01/02/05
20:06:25
Total number of bytes transferred: 27.42 GB
Elapsed processing time: 01:04:07

I prefer to put the parsed pieces into variables, then output
the whole thing when the end-of-log-entry is seen.
-Joe

while (<>) {
$server_time = $1 if /Server date\/time:\s*(\S+)\s+(\S+)/;
$last_access = $1 if /Last access:\s*(\S+)\s+(\S+)/;
$gigabytes = $1 if /Total number of bytes transferred:\s*(\S+)/;
print "ST:$server_time LA=$last_access GB=$gigabytes ET=$1\n"
if /Elapsed processing time:\s*(\S+)/;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top