Logfile - extracting certain lines to concatenate into 1 line

I've got a log file from automatic diagnostic runs. The log file is appended to each time an automatic log is run.
I'd like to just pull certain lines from each run in the log file, and concatenate them into 1 comma delimited line (for export into excel or an html table).

Each diagnostic run is bracketed by a begin and end comment so I'm able to group it that way but I'm having trouble getting everything into 1 line.

Typical data:

Diags begin.
Tues March 17 18:07:34 EDT 2009

PASS: (123) Power Check
.
.
[more data]
Diags end.
Diags begin.
Tues March 17 19:09:22 EDT 2009

FAIL: (123) Power Check
.
description: Voltage clamp
.
.[more failing data details]
.
Diags end.

I want the timestamp, test result (for the 123 test)

I was able to use awk to get close but I can't quite get it.

awk '/Diags begin/{getline;print};{if ($2=="(123)") print $1,$2,$3,$4}'

This gives me:

Mon Mar 16 11:37:07 EDT 2009
PASS: (123) Power Check
Mon Mar 16 12:31:10 EDT 2009
PASS: (123) Power Check
Tue Mar 17 01:30:54 EDT 2009
**FAIL: (123) Power Check
Tue Mar 17 03:08:16 EDT 2009
PASS: (123) Power Check

What I'm trying to get is:
Mon Mar 16 11:37:07 EDT 2009,PASS: (123) Power Check
Mon Mar 16 12:31:10 EDT 2009,PASS: (123) Power Check
Tue Mar 17 01:30:54 EDT 2009,**FAIL: (123) Power Check
Tue Mar 17 03:08:16 EDT 2009,PASS: (123) Power Check

Also, I'm looking for a way to just pull the information for a certain time frame (for instance the current date - 7 days) but I'll worry about that later.. baby steps... :slight_smile:

The system is Unix (HP-UX) so awk, perl, or sed are options.

Thanks for any help..

Paul

#!/usr/bin/perl -w

use strict;

open (FH,'txt');
my $fb=0;
my @str ;
while (my $line = <FH>){
chomp ($line);
if ($line =~ m/Diags begin/){
$fb =1;
}
if ($fb && !($line =~ m/Diags begin/ || $line =~ m/Diags end/ )){
push @str , $line;
}
if ($line =~ m/Diags end/){
my $x = join " ", @str;
print "$x\n";
@str=();
$fb=0;
}
}

Try this

Cheers

Following on your awk script - use printf:

awk '/Diags begin/{getline;printf};{if ($2=="(123)") print ","$1,$2,$3,$4}' 

ok, thanks for the good ideas.. after some hacking and testing I finally got the output in a workable format using awk.

I've outputted the results to an ascii file in a comma delimited format..
my output file is called tstres.txt
and typical lines in the file look like this:

TSTR01 , Mar 29 21:29:17 EDT 2009 , PASS: , Power Check
TSTR01 , Mar 30 00:54:55 EDT 2009 , PASS: , Power Check
TSTR01 , Mar 30 08:31:31 EDT 2009 , **FAIL: , Power Check
TSTR02 , Mar 07 14:41:08 EST 2009 , PASS: , Power Check
TSTR02 , Mar 07 21:46:33 EST 2009 , PASS: , Power Check

What is the easiest way to take this data file and output it to an html table ?

My plan is to have the script run in a cron job and all I'll need to do is view the html page.