1753872 Members
7389 Online
108809 Solutions
New Discussion юеВ

awk program help

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

awk program help

Hello,

I have a file with multiple entries of start and finish times:

R1 Stock Started Thu 28 Jul 08:52:00 2005
R1 Stock Finished Thu 28 Jul 08:52:15 2005

I would like to use awk to calculate the amount of time it took for R1 to complete.

A million Thank U's
hello
6 REPLIES 6
Ninad_1
Honored Contributor

Re: awk program help

Hi,

Are these entries consecutive entries or can be anywhere.
e.g. are these entries of type
R1 Stock Started time1
R1 Stock Finished time2
R1 Stock Started time3
R1 Stock Finished time4
R1 Stock Started time5
R1 Stock Finished time6

OR can be

R1 Stock Started time1
R1 Stock Started time3
R1 Stock Finished time2
R1 Stock Finished time4
R1 Stock Started time5
R1 Stock Finished time6


Regards,
Ninad
lawrenzo_1
Super Advisor

Re: awk program help

244 entries of stop and start al stop and start on the same date

Thanks man
hello
Ninad_1
Honored Contributor
Solution

Re: awk program help

What I wanted to know is that will the start entry be followed by the corresponding stop entry immediately on the next line ?
OR
There can be start entries mixed with stop entries as mentioned in my examples above. So the stop entry for instance 1 does not necessarily follow on the next immediate line of the start for instance 1 , and it may follow some where down the line after say start entries for instance 2 , 3 etc.

Please can you clarify this .

If it is the 1st case then following should do (replace 1.dat by your filename)

awk '/Started/,/Finished/ {
{if(match($3,"Started") == 1) starttime=$7};getline;
{if(match($3,"Finished") == 1) stoptime=$7}}
{print starttime,stoptime}' 1.dat | while read starttime stoptime
do
time1=$(echo $starttime | tr -s ":" " " | awk '{print (($1*3600)+($2*60)+$3)}')
time2=$(echo $stoptime | tr -s ":" " " | awk '{print (($1*3600)+($2*60)+$3)}')
timeelapsed=$(echo "($time2 - $time1)" | bc)
echo "Time elapsed = $timeelapsed seconds"
done

Regards,
Ninad
lawrenzo_1
Super Advisor

Re: awk program help

ok sorry didnt understand your question.

Thanks for a solution i am pretty sure I will be able to work something out.

I have created the file with the start and stop entries and it goes

R1 Stock Started Thu 28 Jul 08:52:00 2005
R1 Stock Finished Thu 28 Jul 08:52:15 2005
R1 Stock Started Thu 29 Jul 08:52:00 2005
R1 Stock Finished Thu 29 Jul 08:52:27 2005
R1 Stock Started Thu 30 Jul 08:52:00 2005
R1 Stock Finished Thu 30 Jul 08:52:01 2005

I'll let you know my results.

TY
hello
spex
Honored Contributor

Re: awk program help

Lawrenzo,

As long as the 'start-finished-start-finished' pattern is upheld, the starttime is always 08:52, and the duration is always <8 minutes, you can use this simple one-liner:

awk '/Finished/{print substr($7,5,1)-2 substr($7,6,3)}'


PCS
lawrenzo_1
Super Advisor

Re: awk program help

awk '/Started/,/Finished/ {
{if(match($3,"Started") == 1) starttime=$7};getline;
{if(match($3,"Finished") == 1) stoptime=$7}}
{print starttime,stoptime}' 1.dat | while read starttime stoptime
do
time1=$(echo $starttime | tr -s ":" " " | awk '{print (($1*3600)+($2*60)+$3)}')
time2=$(echo $stoptime | tr -s ":" " " | awk '{print (($1*3600)+($2*60)+$3)}')
timeelapsed=$(echo "($time2 - $time1)" | bc)
echo "Time elapsed = $timeelapsed seconds"
done

worked fine for me

Thanks chaps
hello