python/perl log file script

Status
Not open for further replies.

sparso

Member level 3
Joined
Dec 30, 2006
Messages
64
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Activity points
1,580
I am in need of a script that reads a log file, takes some data from the file and saves it in a .csv (coma separated value) format. The example log is below:


log.txt:

Date: 3/14/09

Device #: 1
Test: Cont: pass
Test: Leakage: pass
Test: INL: 0.88
Test: DNL: 0.56

Device #: 2
Test: Cont: pass
Test: Leakage: pass
Test: INL: 0.76
Test: DNL: 0.44

Device #: 3
Test: Cont: pass
Test: Leakage: pass
Test: INL: 1.2
Test: DNL: 0.84


format for the .csv file:
log.csv:

device, INL, DNL
1,0.88,0.56
2,0.76,0.44
3,1.2,0.84

Is someone has a script either in perl or python it would be greatly appreciated.
 

Simple perl script (your 1st line might be #/usr/local/bin/perl or something else.
If this file is called simple then you would execute it like

./simple input.in output.out


#!perl
#
#
#
$header = "device,INL,DNL\n";

open(IF,$ARGV[0]);
open(OF,">$ARGV[1]");


print OF $header;

while ($line=<IF>)
{
$csv="";


if($line =~m/Device #:/)
{
$dev = 0;
$INL = 0;
$DNL = 0;

if ($line =~m/Device #: (\d+)/) {
$dev=$1;
$line=<IF>;
$line=<IF>;
$line=<IF>;
if ($line =~m/Test: INL: (\d.\d+)/) {
$INL = $1
}
$line=<IF>;
if ($line =~m/Test: DNL: (\d.\d++)/) {
$DNL = $1
}
print OF $dev,",",$INL,",",$DNL,"\n"
}
}
}
close(IF);
close(OF);
 

    sparso

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…