Help with Display Shell Script

hi Friends,
I am writing a shell script to extract data from sar logs for my daily analysis.
I have this in sar logs

HP-UX dhpcdbe1 B.11.31 U ia64    05/09/12

01:22:42   device   %busy   avque   r+w/s  blks/s  avwait  avserv
01:27:59   disk30    8.79   13.72      26     427   36.47   10.09
           disk50   10.48   12.87      29     449   36.78   10.74
          disk745    0.00    0.50       0       0    0.01    0.41
          disk746    0.05    0.50       0       0    0.00    8.33
          disk747    0.03    0.50       0       0    0.01    6.00
          disk748    0.07    0.50       0       0    0.00   11.09

What i want is something like below:

01:22:42   device   %busy   avque   r+w/s  blks/s  avwait  avserv
01:27:59   disk30    8.79   13.72      26     427   36.47   10.09
01:27:59   disk50   10.48   12.87      29     449   36.78   10.74
01:27:59   disk745    0.00    0.50       0       0    0.01    0.41
01:27:59   disk746    0.05    0.50       0       0    0.00    8.33
01:27:59   disk747    0.03    0.50       0       0    0.01    6.00
01:27:59   disk748    0.07    0.50       0       0    0.00   11.09

So what i am trying to do is fill those rows which dont have the timings with the immediate previous one which has, like here the second row.

What challenges i am facing:

  1. What logic to implement and how to get the timings in all the rows filled?
    Its been a long time since i have written a script .
  2. Something i tried was
#!/usr/bin/ksh
TIMESTAMP_SAR=`date +%d%m%Y_%H%M%S`
tt=$1
awk 1 $tt > $TIMESTAMP_SAR
perl -pi -e '$_ = "" if ($. <= 3);' $TIMESTAMP_SAR

cut -d"d" -f2- $TIMESTAMP_SAR |perl -pi -e 's/^/d/g' > ${TIMESTAMP_SAR}_3
awk -F"d" '{print $1}' $TIMESTAMP_SAR > ${TIMESTAMP_SAR}_4
perl -pi -e 's/ //g' ${TIMESTAMP_SAR}_4
awk -F\t '{if($1=="")$1=string}1' OFS="\t" string=0 ${TIMESTAMP_SAR}_4 > ${TIMESTAMP_SAR}_5
#below part fills the times for missing rows same as row before
######
c=1
while read line
do
a=${#line}
b=${line}
if [[ "$a" -eq "$c" ]]
then
b=${d}
echo $b
else
echo $b
d=${b}
fi
done < ${TIMESTAMP_SAR}_5 > ${TIMESTAMP_SAR}_6
######
paste ${TIMESTAMP_SAR}_6 ${TIMESTAMP_SAR}_3 > ${TIMESTAMP_SAR}_7

But the problem with this logic is if the device column has any name starting with any other alphabet other than 'd' then it doesn't work.

Why i am doing all this?
Basically i will convert all the sar data into csv format and put it in a db table and then make graphs using excel ,by connecting to the db , whenever i need to .

Please can you suggest the solutions to my challenges.

Thanks,
Kunwar

try this..

perl -nle 'if($.>2) { if (/(^\d{2}:\d{2}:\d{2})/) { $time=$1;print $_;}else{s/^\s+/$time   /;print $_;}}' filename

A try in awk :

awk '$1 ~ /[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/ { a=$1"\t" }  { print ( $1 != a ) ? a $0 : $0 }' input

thanks you guys...It helped..Below is o/p:

> perl -nle 'if($.>2) { if (/(^\d{2}:\d{2}:\d{2})/) { $time=$1;print $_;}else{s/^\s+/$time   /;print $_;}}' tt3

01:22:42   device   %busy   avque   r+w/s  blks/s  avwait  avserv
01:27:59   disk30    8.79   13.72      26     427   36.47   10.09
01:27:59   disk50   10.48   12.87      29     449   36.78   10.74
01:27:59   disk745    0.00    0.50       0       0    0.01    0.41
01:27:59   disk746    0.05    0.50       0       0    0.00    8.33
01:27:59   disk747    0.03    0.50       0       0    0.01    6.00
01:27:59   disk748    0.07    0.50       0       0    0.00   11.09


> awk '$1 ~ /[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/ { a=$1"\t" }  { print ( $1 != a ) ? a $0 : $0 }'  tt3

HP-UX dhpcdbe1 B.11.31 U ia64    05/09/12

01:22:42        01:22:42   device   %busy   avque   r+w/s  blks/s  avwait  avserv
01:27:59        01:27:59   disk30    8.79   13.72      26     427   36.47   10.09
01:27:59                   disk50   10.48   12.87      29     449   36.78   10.74
01:27:59                  disk745    0.00    0.50       0       0    0.01    0.41
01:27:59                  disk746    0.05    0.50       0       0    0.00    8.33
01:27:59                  disk747    0.03    0.50       0       0    0.01    6.00
01:27:59                  disk748    0.07    0.50       0       0    0.00   11.09