473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Parsing a Log File

12
hello im new with perl
i must do parse log file from networker

with format look like this :

Start time: 2-16-07 11:27a
Recovering files of client 'blablabla' from server 'bliblibli'.
Recover: Total estimated disk space needed for recover is 3 MB.
Recovering 5 files within D:\X\Y\ into E:\restore
Requesting 5 file(s), this may take a while...
E:\restore\blabla\blablabla.bla
E:\restore\blabla\blablabla.bli
E:\restore\blabla\blablabla.blu
E:\restore\blabla\blablabla.ble
E:\restore\blabla\blablabla.blo
Received 5 file(s) from NSR server 'blablabla'
Recover completion time: Fri Feb 16 11:30:52 2007

and the data,i need:
start time,completion time,client name,server name,total file,and disk space

i have problem with regex,grep,split..is anyone can help me?
thx before =)
Jun 18 '07 #1
21 2587
KevinADC
4,059 Expert 2GB
What have you tried so far?
Jun 18 '07 #2
drushof
12
What have you tried so far?
Start time: 2-16-07 11:27a
Recovering files of client 'blablabla' from server 'bliblibli'.
Recover: Total estimated disk space needed for recover is 3 MB.
Recovering 5 files within D:\X\Y\ into E:\restore
----> this is oke..


and then how can i go to the last line to get the completion date,
because every file has different amount of files to recover.(in this example 5 files)
i dont have idea to make the loop
Jun 18 '07 #3
KevinADC
4,059 Expert 2GB
Lets see the perl code you have written so far.
Jun 18 '07 #4
drushof
12
Lets see the perl code you have written so far.
o yea i forgot to tell that i just make the prosedur for each line, not a whole file..
Jun 18 '07 #5
KevinADC
4,059 Expert 2GB
Please post your code here in this thread.
Jun 18 '07 #6
drushof
12
i've done this :

use strict;
use warnings;

open (LOGFILE, "Restoremarble.log") || die "couldn't open the file!";
my @line = <LOGFILE>;

#line 1
my $list = $line[0];
my @list_t = split(/ /,$list);

my @grepNames = grep(/^\d/, @list_t);
my $el1 = $grepNames[0];
my $el2 = $grepNames[1];

my @list_date = split('-',$el1);
my @list_time = split(':',$el2);

print "-----------------\n";
print "day :$list_date[0]\n";
print "month :$list_date[1]\n";
print "year : $list_date[2]\n";
print "time : $list_time[0]-$list_time[1]\n";

#line 2
my $list2 = $line[1];
my @list_t2 = split(/ /,$list2);

my @grepNames2 = grep(//,@list_t2);
my $el1a = $grepNames2[4]; #client
my $el2a = $grepNames2[7]; #server


print "-----------------\n";
print "client : $el1a\n";
print "server : $el2a\n";

#line 3
my $list3 = $line[2];
my @list_t3 = split(/ /,$list3);

my @grepNames3 = grep(/^\d/,@list_t3);
print "--------------\n";
print "Total disk : $grepNames3[0]\n";

#2 last line
my $list4 = $line[-2];
my @list_t4 = split(/ /,$list4);

my @grepNames4 = grep(/^\d/,@list_t4);
print "-----------------\n";
print "Total files : $grepNames4[0]\n";

#last line
my $list5 = $line[-1];
my @list_t5 = split(/ /,$list5);

print "---------------------\n";
print "day : $list_t5[5]\n"; #day
print "month : $list_t5[4]\n"; #month
print "year : $list_t5[7]"; #year
print "time : $list_t5[6]\n"; #time

close(LOGFILE);
exit 0;

is it correct?
Jun 19 '07 #7
KevinADC
4,059 Expert 2GB
It's not the way I would do it and the results look incomplete based on the data you posted. When I run your script with the sample data this is the output:

-----------------
day :2
month :16
year : 07
time : 11-27a

-----------------
client : 'blablabla'
server : 'bliblibli'.

--------------
Total disk : 3
-----------------
Total files :
---------------------
day :
month :
year : time :

so while your code could be improved and does not return all the data you wanted, completion time and total file are missing, at least it runs. It uses "strict" and "warnings" (very good), and does produce most of the output you want. Is this school work or some sort of learning excersize?
Jun 19 '07 #8
drushof
12
so what i supossed to do?
this is school work =(
Jun 19 '07 #9
drushof
12
i've improved it :

use strict;
use warnings;

open (LOGFILE, "bla.log") || die "couldn't open the file!";
my @line = <LOGFILE>;

#line 1
my $list = $line[0];
my @list_t = split(/ /,$list);

my @grepNames = grep(/^\d/, @list_t);
my $el1 = $grepNames[0];
my $el2 = $grepNames[1];

my @list_date = split('-',$el1);
my @list_time = split(':',$el2);

print "-----------------\n";
print "day :$list_date[1]\n";
print "month :$list_date[0]\n";
print "year : $list_date[2]\n";
print "time : $list_time[0]-$list_time[1]\n";

#line 2
my $list2 = $line[1];
my @list_t2 = split(/ /,$list2);

my @grepNames2 = grep(//,@list_t2);
my $el1a = $grepNames2[4]; #client
my $el2a = $grepNames2[7]; #server


print "-----------------\n";
print "client : $el1a\n";
print "server : $el2a\n";

#line 3
my $list3 = $line[2];
my @list_t3 = split(/ /,$list3);

print "-------------------\n";
print "Total size : $list_t3[9] $list_t3[10]\n";

# my @grepNames3 = grep(/^\d/,@list_t3);
# print "--------------\n";
# print "Total disk : $grepNames3[1]\n";

#2 last line
my $list4 = $line[-2];
my @list_t4 = split(/ /,$list4);

my @grepNames4 = grep(/^\d/,@list_t4);
print "-----------------\n";
print "Total files : $grepNames4[0]\n";

#last line
my $list5 = $line[-1];
my @list_t5 = split(/ /,$list5);

print "---------------------\n";
print "day : $list_t5[5]\n"; #day
print "month : $list_t5[4]\n"; #month
print "year : $list_t5[7]"; #year
print "time : $list_t5[6]\n"; #time

close(LOGFILE);
exit 0;

content of bla.log is same with the first post
Jun 19 '07 #10
KevinADC
4,059 Expert 2GB
You need a way to skip the lines that don't interest you and find the ones that do. You can use a regexp to do that. Since this is school work I leave that up to you to figure out.
Jun 19 '07 #11
drushof
12
You need a way to skip the lines that don't interest you and find the ones that do. You can use a regexp to do that. Since this is school work I leave that up to you to figure out.
skip the line??
sorry i dont get it

yea im gonna figure it out..do i have the right step?
Jun 19 '07 #12
KevinADC
4,059 Expert 2GB
Your entire approach is really quite awkward but I have no idea how far along you are in your perl studies. If you have already covered regular expressions you should be using them to find the lines you want to parse data out of. But maybe the assignment dictates what functions you have to use to get the output.

As far as skipping goes, these lines do not seem to interest you:

E:\restore\blabla\blablabla.bla
E:\restore\blabla\blablabla.bli
E:\restore\blabla\blablabla.blu
E:\restore\blabla\blablabla.ble
E:\restore\blabla\blablabla.blo

so you would skip them (move on to the next line) until you find a line that does interest you.
Jun 19 '07 #13
drushof
12
im sorry..but im new in perl..
any advise for my code?
thx
Jun 19 '07 #14
KevinADC
4,059 Expert 2GB
any advise for my code?
Yes, use regular expressions to parse the data to get the results you want.
Jun 20 '07 #15
drushof
12
use regular expressions to parse the data to get the results you want.
mmh oke..but is my code wrong?
or regular expression make it easier?
Jun 20 '07 #16
KevinADC
4,059 Expert 2GB
mmh oke..but is my code wrong?
or regular expression make it easier?

There is no way for me to know if your code is wrong. That depends on how much perl you have learned already and how much you are expected to know at this point in your studies.

Technically, if the code runs and poduces the correct results, it's good code. It may not be efficient code, but it's good. Your code is close to producing the results you want.

Using regular expressions should make the job easier.
Jun 20 '07 #17
drushof
12
thx so much
another question,can you help me

Recovering 10 files within C:\ABC\DEF\ into F:\XYZ\PQR\

data i need C:\ABC\DEF\ and F:\XYZ\PQR\
how regular expression solve this thing?
Jun 20 '07 #18
KevinADC
4,059 Expert 2GB
OK you beat some code out of me, but since this is your school work don't expect anymore code. This is one possible way to match the pattern you are interested in:

Expand|Select|Wrap|Line Numbers
  1. $line = 'Recovering 5 files within D:\X\Y\ into E:\restore';
  2. if ($line =~ /^Recovering \d+ files within (\S+) into (\S+)/ ) {
  3.    print "$1 $2";
  4. }
here are a couple of regexp tutorials:

# perlrequick - Perl regular expressions quick start
# perlretut - Perl regular expressions tutorial

you can find them here:

http://perldoc.perl.org/index-tutorials.html
Jun 20 '07 #19
drushof
12
thx so much..
im gonna try it =)
Jun 20 '07 #20
drushof
12

Expand|Select|Wrap|Line Numbers
  1. $line = 'Recovering 5 files within D:\X\Y\ into E:\restore';
  2. if ($line =~ /^Recovering \d+ files within (\S+) into (\S+)/ ) {
  3.    print "$1 $2";
  4. }
sorry but in other case like 'Recovering 10 files within D:\X Y's\Z into E:\restore'
the code didnt work
what should i supposed to do to avoid the space and '

thx
Jun 20 '07 #21
KevinADC
4,059 Expert 2GB
You're going to have to read the tutorials I linked you to (or read whatever perl resource material you have). You will find the answers to your questions in those tutorials.
Jun 20 '07 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Willem Ligtenberg | last post by:
I decided to use SAX to parse my xml file. But the parser crashes on: File "/usr/lib/python2.3/site-packages/_xmlplus/sax/handler.py", line 38, in fatalError raise exception...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
3
by: Pir8 | last post by:
I have a complex xml file, which contains stories within a magazine. The structure of the xml file is as follows: <?xml version="1.0" encoding="ISO-8859-1" ?> <magazine> <story>...
1
by: Christoph Bisping | last post by:
Hello! Maybe someone is able to give me a little hint on this: I've written a vb.net app which is mainly an interpreter for specialized CAD/CAM files. These files mainly contain simple movement...
4
by: Rick Walsh | last post by:
I have an HTML table in the following format: <table> <tr><td>Header 1</td><td>Header 2</td></tr> <tr><td>1</td><td>2</td></tr> <tr><td>3</td><td>4</td></tr> <tr><td>5</td><td>6</td></tr>...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
9
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
13
by: charliefortune | last post by:
I am fetching some product feeds with PHP like this $merch = substr($key,1); $feed = file_get_contents($_POST); $fp = fopen("./feeds/feed".$merch.".txt","w+"); fwrite ($fp,$feed); fclose...
2
by: Felipe De Bene | last post by:
I'm having problems parsing an HTML file with the following syntax : <TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'> <TH BGCOLOR='#c0c0c0' Width='3%'>User ID</TH> <TH...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.