Quote:
Originally Posted by vanand420
Hi All
I have a long file having different fields like :-
hh:mm:ss seconds
14:15:56 120
14:18:36 12
15:12:36 1500
I want to subtract the hh:mm:ss in line(2) from hh:mm:ss in line(1) & compare the output of substraction (obtained in seconds) with the seconds in line(1) and echo whether it is greater than seconds(120). I want the process to repeat for the whole file as in excel we copy the formula down the column. I tried the command
nawk '{printf (substr($0,1,2)...........}' to seperate the hh:mm:ss as
14 15 56 120
14 18 36 12
15 12 36 1500
then using command
nawk '{print ($1*3600+$2*60+$3)-prev;prev=($1*3600+$2*60+$3) }' <file>
I got the result the result as:
51356
160
3240, all value in seconds.
The problem is that I am unable to compare the 160sec with 120 sec and echo the result & loop the process ie compare 3240 sec with 12 sec & so on.
Can u help me out.
Thanks in Advance.
Hi Friends
Thanks for the reply
I tried the your script & made changes according to my need as under :
line=`head -1 test.tmp`
sec1=`echo $line | cut -d' ' -f5`
hms1=`echo $line | cut -d' ' -f4|awk -F':' '{print $1*3600+$2*60+$3}'`
tail +2 test.tmp |while read line;do
sec2=`echo $line | cut -d' ' -f5`
hms2=`echo $line | cut -d' ' -f4|awk -F':' '{print $1*3600+$2*60+$3}'`
secint=`expr $hms2 - $hms1`
if [ $secint -gt $sec1 ]; then
awk '{print $0,"pk"}'
else
awk '{print $0,"W"}'
fi
sec1=$sec2; hms1=$hms2
done
but it gives some problems
1) It does not output the first two lines. Rest of lines are printed with out any comparision made (might loop not working properly)
2) Rest of line printed reflect the comparision made for the first two lines.
e.g. all lines either contains the character pk or W.
Please rectify the errors in script and help me out.