Trouble getting the next to last record with awk

Hello all,

I'm a beginner to shell/ awk script writing, and I'm trying to do something that looks like it shouldn't be (too) hard at all to do, but unfortunately, I can't seem to be able to find the right way to do it with awk.

I need to look for the time several processes start & end in a couple log files. Getting the beginning of each process was fairly simple, I just need awk to get the right patterns but i'm at a loss as far as the ending time of those processes are concerned:

Those log files look like that:

26/01/2010 00:56:51 FIN DES TRAITEMENTS DE FIN DE JOURNEE
----- Traitement termine -----

I need to get the line before the one with : " ----- Traitement termine ----- " which states the time each process ended.

The first trick is no two processes will display the same ending message before the common " ----- Traitement termine ----- " message -> so I obviously can't look for any pattern there.

I thought "piece of cake", I just need to find all lines matching that " ----- Traitement termine ----- " pattern and ask awk to print the previous record...

So here is the script I wrote to do so:

awk ' BEGIN {split($0,ligne,"\t")}
/[A-Z]tape/ {print $1" " $2" " $3" " $4" "$5 >> "tmp"}
/Lancement/ {print "Lancement le : "$2 " a " $3 >> "tmp"}
/Traitement termine/ {print ligne[NR-1] >> "tmp"} 'log

I don't get any error message, but I don't get any result with the last search either.

The second trick is I also tried defining RS as " ----- Traitement termine ----- " to be able to print the last few fieds of each record to get what I need, but I can't either since some of those records would then be bigger than the 3kb awk is limited to...

I can't seem to find the right syntax to do so. Or may be this is not the way it should (can) be done ?

Any help will be most welcome. Thanks in advance

Muadib

It would be better if you show more input data but try this...

awk '{if($0=="----- Traitement termine -----"){
print prv}}{prv=$0}' infile

Here are a couple exemples of the type of paragraph ending I might run into in those log files:

26/01/2010 00:56:51 FIN DES TRAITEMENTS DE FIN DE JOURNEE
----- Traitement termine -----

26/01/2010 00:57:22 VSRECYQS : DM-ibut de l'M-idition des opM-iration recyclM-ies
26/01/2010 00:57:22 Fin de traitement deVSRECYQS terminM-i avec succM-hs
----- Traitement termine -----

26/01/2010 00:59:16 Nombre d'enregistrements en anomalie: 000000
26/01/2010 00:59:16 Fin - Traitement operations ayant atteint la date limite
----- Traitement termine -----

26/01/2010 01:29:10 MC28B5TB : Mise M-` jour des services et des interventions terminM-i
----- Traitement termine -----

Thanks for your help,

Regards,

The code of malcomex999 gives one line before every line with " ----- Traitement termine ----- ".

Isn't that what you want?

Hello again,

First things first, thank you for your help.

If i'm not mistaken, what you do here is stock every line in a variable, print it if it matches a pattern and then replace its value with the next line after that test ?

Cheers

Right, that's what the code does.

Regards