awk - operation over time staps


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - operation over time staps
# 1  
Old 02-03-2009
Error awk - operation over time staps

Hi @ all,
I'm trying to accomplish a simple excel-job with awk, without any result...

What i have is a file with a time stamp for each line like :

21:02:07
21:04:11
21:06:28
21:08:44
21:10:45
21:12:48
21:14:52
21:16:53
21:18:55


What i would, is to calculate the time elapsed since the last time value. In other terms i need a difference between the [line+1] value and the [line] for line=0 to line=EOF.

The output should be :

00:02:04
00:02:17
....and so no


i've done a script that loads these value in a string....but then i can't operate on them by simple numeric aperator probably because i'm trying to subtract two character-strings.

This is what i've done

Code:
awk ' { RS = "" ; FS = "\n" } ; 
{ 
     for (i = 0; i <= NF; i++) 
    string[i]=$i 
} 

END {
    
    for (i = 0; i <= NF; i++) 
    diff[i]=string[i+1] - string[i]
    printf "%s\n", diff[i]


}'

The first part of code (upper the END statement) seems to works fine (do you agree?)...the for cycle correctly populate the string array.
But when i try to assign the [I]string[i+1] - stringvalues to the diff array...doesn't work Smilie

Maybe there is some function for time operations?
Maybe i have to firstly convert the time value to UTC?

Hoping in your help!
# 2  
Old 02-03-2009
who knows?

here's a ksh solution:

Code:
typeset -Z2 hour min sec

prev_time_t=0

sed -e 's/:/ /g' << EOF |
21:02:07
21:04:11
21:06:28
21:08:44
21:10:45
21:12:48
21:14:52
21:16:53
21:18:55
EOF
while read hour min sec ; do

  (( time_t = ( hour * 3600 ) + ( min * 60 ) + sec ))

  (( diff = time_t - prev_time_t ))

  prev_time_t=$time_t

  if [ $diff -eq $time_t ]; then
    continue
  fi

  (( hour = diff / 3600 ))
  (( min  = ( diff % 3600 ) / 60 ))
  (( sec  = diff % 60 ))

  print $hour:$min:$sec

done

Smilie
# 3  
Old 02-03-2009
Quote:
Originally Posted by quirkasaurus
who knows?

here's a ksh solution:

Why write a ksh solution when one that will work in any POSIX shell (which includes ksh) is just as easy?

Code:
prev_time_t=0

while IFS=": " read hour min sec x ; do
  time_t=$(( $hour * 3600 + $min * 60 + sec ))
  diff=$(( time_t - prev_time_t ))
  prev_time_t=$time_t

  if [ $diff -eq $time_t ]; then
    continue
  fi

  hour=$(( diff / 3600 ))
  min=$(( ( diff % 3600 ) / 60 ))
  sec=$(( diff % 60 ))

  printf "%02d:%02d:%02d\n" "$hour" "$min" "$sec"

done < "$FILE"

If the file is large, an awk solution will be faster.

Last edited by cfajohnson; 02-03-2009 at 07:06 PM.. Reason: s/$/%/
# 4  
Old 02-03-2009
Both scripts run perfectly, and this could be sufficient, so thank you all so much!

Therefore the file that i'm being to parse tends to grow up every 30 min, so the fastest solution is still welcome (and i think that awk is the fastest way)

@cfajohnson

there is a little error :
- printf "$02d:%02d:%02d\n" "$hour" "$min" "$sec"
+ printf "%02d:%02d:%02d\n" "$hour" "$min" "$sec"

Smilie
# 5  
Old 02-03-2009
Code:
awk  -F:  'NR>1 { printf "%.02d:%.02d:%.02d\n", $1-s1, $2-s2, $3-s3 } 
                          { s1=$1; s2=$2; s3=$3 }'  file

# 6  
Old 02-03-2009
We forgot to manage these inputs:

23:55:26
23:57:32
00:00:13
00:02:16

@to posix scripters
this is your output

00:02:06
00:-57:-19
00:02:03

@robin
this is yours
00:02:06
-23:-57:-19
00:02:03

thanks @ all!!!
# 7  
Old 02-03-2009
Try...
Code:
awk -F: '{t=$1*60*60+$2*60+$3;d=t-p;if(d<0)d+=24*60*60;if(p)printf "%02d:%02d:%02d\n",d/60/60,d/60%60,d%60;p=t}' file

Tested...
Code:
$ cat file
21:02:07
21:04:11
21:06:28
21:08:44
21:10:45
21:12:48
21:14:52
21:16:53
21:18:55
23:55:26
23:57:32
00:00:13
00:02:16
$ awk -F: '{t=$1*60*60+$2*60+$3;d=t-p;if(d<0)d+=24*60*60;if(p)printf "%02d:%02d:%02d\n",d/60/60,d/60%60,d%60;p=t}' file
00:02:04
00:02:17
00:02:16
00:02:01
00:02:03
00:02:04
00:02:01
00:02:02
02:36:31
00:02:06
00:02:41
00:02:03


Last edited by Ygor; 02-03-2009 at 10:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to do arithmetic operation

Hi, I've this following text file FileVersion = 1.03 Filetype = meteo_on_curvilinear_grid TIME = 0 hours since 2016-10-03 12:00:00 +00:00 -6.855 -6.828 -6.801 -6.774 -6.747 -6.719 -6.691 -6.663 -6.634 -6.606 -6.577 -6.548 -6.519 -6.489 TIME = 0 hours since... (2 Replies)
Discussion started by: xisan
2 Replies

2. UNIX for Dummies Questions & Answers

Basic arithmetic operation with awk?

input: Name|Operation rec_10|1+2+2- Output: rec_10|1 Basically I am trying to calculate the result of "the path" in $3 where the operators follow the number and not preceding them like we normally do: rec_10: +1+2-2=1 But I realise (I am sure there is a good reason for that) that awk... (7 Replies)
Discussion started by: beca123456
7 Replies

3. AIX

AIX 6, operation on time in script

Hi I have a question, On linux (centos) I am executing a script: #!/bin/bash st1=`date "+%T"` SD=`date -u -d $st1 +"%s"` #some operations ... st2=`date "+%T"` FD=`date -u -d $st2 +"%s"` time_oper=`date -u -d "0 $FD sec - $SD sec" +"%H:%M:%S"` echo "Time script execution -... (1 Reply)
Discussion started by: primo102
1 Replies

4. Shell Programming and Scripting

awk --> math-operation in a array

Hi main object is categorize the difference of data-values (TLUFT02B - TLUFT12B). herefor i read out data-files which are named acording to the timeformat yyyymmddhhmm. WR030B 266.48 Grad 0 WR050B 271.46 Grad 0 WR120B 268.11 Grad 0 WV030B 2.51 m/s ... (6 Replies)
Discussion started by: IMPe
6 Replies

5. Shell Programming and Scripting

How To Perform Mathematical Operation Within If in awk?

Hi All, I am using an awk script as below: awk -F'|' 'BEGIN{OFS="|";} { if ($1==$3 && $3==$7 && $7==$13 && $2==$6 && $6==$11 && $15-$14+1==$11) print $0"|""TRUE"; else print $0"|""FALSE"; }' tempfile.txt In above script, all conditions are being checked except the one which is... (4 Replies)
Discussion started by: angshuman
4 Replies

6. Shell Programming and Scripting

awk math operation on two files

Hi, I need your help. I've got two files and i need to add 2nd line after occurrence of "Group No X" from data2.txt to 3rd line (after occurrence of "Group No X") from data1.txt. There is the same number of "Groups" in both files and the numbers of groups have the same pattern. data1.txt Group... (2 Replies)
Discussion started by: killerbee
2 Replies

7. Shell Programming and Scripting

Variable%pattern operation within awk

Hello; I have: ll | grep -v ^d | awk '{print $9}' rcx_access_report_fid.txt rcx_access_report_hsi.txt rcx_access_report_mmm.txt rcx_access_report_qsc.txt I want to get: rcx_access_report_fid.txt rcx_access_report_hsi rcx_access_report_mmm rcx_access_report_qsc But when I try: (9 Replies)
Discussion started by: delphys
9 Replies

8. Shell Programming and Scripting

column operation using awk

I have atxt file,i want to perform some operation on 3rd coulmn 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 67.780083 -3.0000000 6.9356270 0 4 ... (4 Replies)
Discussion started by: shashi792
4 Replies

9. Shell Programming and Scripting

Column operation : cosne and sine operation

I have a txt file with several columns and i want to peform an operation on two columns and output it to a new txt file . file.txt 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 ... (4 Replies)
Discussion started by: shashi792
4 Replies

10. Shell Programming and Scripting

Arithmetic operation with awk

I have output like following in a file usmtnz-dinfsi19 72 71 38 1199 1199 0.8 19:23:58 usmtnz-dinfsi19 72 71 38 1199 1199 0.8 19:24:04 (9 Replies)
Discussion started by: fugitive
9 Replies
Login or Register to Ask a Question