How to compare the time in different format from a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare the time in different format from a file?
# 1  
Old 12-21-2012
How to compare the time in different format from a file?

Code:
15:09:50.350038  Reading A Data
15:09:50.371645  Reading B Data
15:10:55.655724  Initializing models
15:11:31.320920  Preparing Simulation
15:11:32.763217  Running Calculation
15:15:29.668882  Aggregating Results
15:15:29.950897  Persisting Results

How could I make a matrix in H.M.S.MS format as below which is always time differnce between start time of 2nd activity and start time of it's own activity.

Code:
Reading A Data 	(15:09:50.371645 - 15:09:50.350038)
Reading B Data	(15:10:55.655724 - 15:09:50.371645)
Initializing models 	(15:11:31.320920 - 15:10:55.655724)
Preparing Simulation	(15:11:32.763217 - 15:11:31.320920)
Running Calculation	(15:15:29.668882 - 15:11:32.763217)
Aggregating Results	(15:15:29.950897 - 15:15:29.668882)
Persisting Results	(15:15:29.950897)

SO here Reading A data took time A.XMs which is difference from start time of 2nd activity which is 15:09:50.371645 minus it's own start time which is 15:09:50.350038.
Similarly Reading B Data took time B.Yms which is difference of start time of next activity which is 15:10:55.655724(for Initializing models ) minus it's own start time which is 15:09:50.371645.
the process goes on till the last line; as Last activity won't have any time to compare.
# 2  
Old 12-21-2012
If you want difference in milliseconds..

try sth like this..

Code:
awk -F "  +" 'a{print $1,x,y;x=$1;y=$2} !a{x=$1;y=$2;a++}END{print $1,x,y}' OFS="\t" file | while read a b c
do
echo -e "$c\t$(expr $(date -d "$a" +%s%N) - $(date -d "$b" +%s%N))"
done

Reading A Data  21607000
Reading B Data  65284079000
Initializing models     35665196000
Preparing Simulation    1442297000
Running Calculation     236905665000
Aggregating Results     282015000
Persisting Results      0

# 3  
Old 12-21-2012
turn the first column into a floating point value - this assumes you WILL NOT have run this so that times overlap midnight.
tmpfile:
Code:
54590.350038 15:09:50.350038  Reading A Data
54590.371645 15:09:50.371645  Reading B Data
54655.655724 15:10:55.655724  Initializing models
54691.320920 15:11:31.320920  Preparing Simulation
54692.763217 15:11:32.763217  Running Calculation
54929.668882 15:15:29.668882  Aggregating Results
54929.950897 15:15:29.950897  Persisting Results

Code:
awk -F '[: ]'  '{dbl=($1*3600) + ($2*60) + $3
                 printf("%.6f ",dbl)
                 print $0}' infile>tmpfile

Does that give you enough to go on?
# 4  
Old 12-21-2012
Hi Pamu,

you are a real life savor, thanks but any way it's throwing out error for last line in this case for Persisting Results

Code:
date: invalid date `Persisting'
expr: syntax error
Results
        0

so is there a way to make it h:m:s.ms format the o/p?
# 5  
Old 12-21-2012
I tried using floating point (as Jim suggested), but with some additional testing I found the results were occasionally off by a microsecond. The following seems to work even when time stamps roll over to the next day. (It still assumes that there is always less than 24 hours between adjacent time stamps.) If you're running on a Solaris system, use nawk or /usr/xpg4/bin/awk instead of awk:
Code:
awk -F '  ' '{  
        if(split($1, nf, /[:.]/) != 4) {
                printf "Time stamp split for \"%s\" on line %d failed\n", $1, NR
                exit 1 
        }
        if(NR > 1) {
                usec = nf[4] - lf[4]
                sec = nf[3] - lf[3]
                min = nf[2] - lf[2]
                hr = nf[1] - lf[1]
                if(usec < 0) {usec += 1000000; sec--}
                if(sec < 0) {sec += 60; min--}
                if(min < 0) {min += 60; hr--}
                if(hr < 0) hr += 24
                printf "%2d:%02d:%02d.%06d\n", hr, min, sec, usec
        }
        for(i = 1; i <= 4; i++) lf[i] = nf[i]
        printf "%20s: ", $2
}
END {   printf "unknown (no end time stamp)\n"
}' in

with the file in containing:
Code:
15:09:50.350038  Reading A Data
15:09:50.371645  Reading B Data
15:10:55.655724  Initializing models
15:11:31.320920  Preparing Simulation
15:11:32.763217  Running Calculation
15:15:29.668882  Aggregating Results
15:15:29.950897  Persisting Results
23:59:59.000000  1 minute to midnight
00:00:00.000000  midnight
23:59:59.999999  eod
00:00:00.000001  early
00:00:00.000000  next midnight

the ouput produced is:
Code:
      Reading A Data:  0:00:00.021607
      Reading B Data:  0:01:05.284079
 Initializing models:  0:00:35.665196
Preparing Simulation:  0:00:01.442297
 Running Calculation:  0:03:56.905665
 Aggregating Results:  0:00:00.282015
  Persisting Results:  8:44:29.049103
1 minute to midnight:  0:00:01.000000
            midnight: 23:59:59.999999
                 eod:  0:00:00.000002
               early: 23:59:59.999999
       next midnight: unknown (no end time stamp)

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 12-26-2012
Quote:
Originally Posted by Don Cragun
I tried using floating point (as Jim suggested), but with some additional testing I found the results were occasionally off by a microsecond. The following seems to work even when time stamps roll over to the next day. (It still assumes that there is always less than 24 hours between adjacent time stamps.) If you're running on a Solaris system, use nawk or /usr/xpg4/bin/awk instead of awk:
Code:
awk -F '  ' '{  
        if(split($1, nf, /[:.]/) != 4) {
                printf "Time stamp split for \"%s\" on line %d failed\n", $1, NR
                exit 1 
        }
        if(NR > 1) {
                usec = nf[4] - lf[4]
                sec = nf[3] - lf[3]
                min = nf[2] - lf[2]
                hr = nf[1] - lf[1]
                if(usec < 0) {usec += 1000000; sec--}
                if(sec < 0) {sec += 60; min--}
                if(min < 0) {min += 60; hr--}
                if(hr < 0) hr += 24
                printf "%2d:%02d:%02d.%06d\n", hr, min, sec, usec
        }
        for(i = 1; i <= 4; i++) lf[i] = nf[i]
        printf "%20s: ", $2
}
END {   printf "(no end time stamp)\n"
}' in

with the file in containing:
Code:
15:09:50.350038  Reading A Data
15:09:50.371645  Reading B Data
15:10:55.655724  Initializing models
15:11:31.320920  Preparing Simulation
15:11:32.763217  Running Calculation
15:15:29.668882  Aggregating Results
15:15:29.950897  Persisting Results
23:59:59.000000  1 minute to midnight
00:00:00.000000  midnight
23:59:59.999999  eod
00:00:00.000001  early
00:00:00.000000  next midnight

the ouput produced is:
Code:
      Reading A Data:  0:00:00.021607
      Reading B Data:  0:01:05.284079
 Initializing models:  0:00:35.665196
Preparing Simulation:  0:00:01.442297
 Running Calculation:  0:03:56.905665
 Aggregating Results:  0:00:00.282015
  Persisting Results:  8:44:29.049103
1 minute to midnight:  0:00:01.000000
            midnight: 23:59:59.999999
                 eod:  0:00:00.000002
               early: 23:59:59.999999
       next midnight: unknown (no end time stamp)

Hey thanks, this is exactly what I was looking for......but I have an issue if I write this same piece of awk to a script it's throwing input file read error. but if I do this same from command line no issue at all. in script and command line I'm using same input file but not sure why this awk throws input file read error in script while not thru command line.

Last edited by manas_ranjan; 12-27-2012 at 09:27 AM..
# 7  
Old 12-26-2012
Quote:
Originally Posted by manas_ranjan
Hey thanks, this is exactly what I was looking for......but I have an issue if I write this same piece of awk to a script it's throwing input file read error. but if I do this same from command line no issue at all. in script and command line I'm using same input file but not sure why this awk throws input file read error in script while not thru command line.
I'm not sure what you mean by
Quote:
write this same piece of awk to a script
or by
Quote:
it's throwing input file read error
.

If you save the code I provided in a file named compare_time,
change /bin/ksh in #!/bin/ksh to be the absolute pathname of ksh on your system, run the command:
Code:
chmod +x compare_time

and then run the command:
Code:
./compare_time

it should work exactly like it works if you paste the script into an interactive Korn shell.

If you mean that you want to put the script into a file that can be used with awk's -f option, then create a file named compare_time.awk containing the following:
Code:
BEGIN { FS = "  "
}
{
        if(split($1, nf, /[:.]/) != 4) {
                printf "Time stamp split for \"%s\" on line %d failed\n", $1, NR
                exit 1
        }
        if(NR > 1) {
                usec = nf[4] - lf[4]
                sec = nf[3] - lf[3]
                min = nf[2] - lf[2]
                hr = nf[1] - lf[1]
                if(usec < 0) {usec += 1000000; sec--}
                if(sec < 0) {sec += 60; min--}
                if(min < 0) {min += 60; hr--}
                if(hr < 0) hr += 24
                printf "%2d:%02d:%02d.%06d\n", hr, min, sec, usec
        }
        for(i = 1; i <= 4; i++) lf[i] = nf[i]
        printf "%20s: ", $2
}
END {   printf "unknown (no end time stamp)\n"
}

and then use it as follows:
Code:
awk -f compare_time.awk input_filename

If these suggestions don't take care of the issue, please provide the exact message or messages being written by awk that tell you that awk is throwing an input file read error, and provide the exact command line that you're using to invoke awk. (As mentioned in an earlier message, if you're running this on a Solaris system, use /usr/xpg4/bin/awk or nawk instead of awk.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate Time diff in milli milliseconds(Time format : HH:MM:SS,NNN)

Hi All, I have one file which contains time for request and response. I want to calculate time difference in milliseconds for each line. This file can contain 10K lines. Sample file with 4 lines. for first line. Request Time: 15:23:45,255 Response Time: 15:23:45,258 Time diff... (6 Replies)
Discussion started by: Raza Ali
6 Replies

2. Shell Programming and Scripting

Compare actual files with format

Hi, I have database table let say t_filenames which stores filenames and date_format in two columns. e.g. ABCD_TV_YYYYMMDD.txt YYYYMMDD ABCD_MOUSE_YYYYMMDDHHMISS.txt YYYYMMDDHHMISS Actual files are available in a directory (say /tmp), actual files are with... (2 Replies)
Discussion started by: pointers1234
2 Replies

3. Shell Programming and Scripting

Long list file display different time format.

Hi Gurus, I have some weird issue. when using ls -l the result shows different time format: -rw-r--r-- 1 abc gourp1 3032605576 Jun 14 2013 abc -rw-rw-r-- 1 abc gourp1 1689948832 Aug 10 06:22 abc one display 2013 which is year; another one displays 06:22 which is time. ... (4 Replies)
Discussion started by: ken6503
4 Replies

4. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

5. Shell Programming and Scripting

Compare current time to timestamp on a file

I'm trying to compare 2 dates between current time and the timestamp on a file. The date format is mmdd Both return Apr 1 but when using if statement line 11: Apr 1: command not found error is returned #!/bin/sh log="DateLog" Current_Date=`date +%b%e` Filepmdate=`ls -l /file.txt |... (1 Reply)
Discussion started by: cillmor
1 Replies

6. Shell Programming and Scripting

Compare Last Modified Time across Time Zone

Hi, I'm new to shell script programming, I only have Java programming background. I'm writing a shell script to do file synchronization between 2 machines that located at different time zone area. Both machine were set its time zone according to its geographical location (Eg: server is at... (1 Reply)
Discussion started by: python
1 Replies

7. Shell Programming and Scripting

How to compare the mtime of a file with the current time?

Hi, I wondered if we could do this with shell script? How to compare the mtime of a file with the current time and check whether its less than 24 hours. Thanks.:b: (2 Replies)
Discussion started by: Krsh
2 Replies

8. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

9. UNIX for Dummies Questions & Answers

Need to get 4 Hrs back time and compare with successive time

Hi all, I am working on a script in which i need to get 4 hrs back time from the current time which i got from this perl function : `perl -e 'print localtime(time() - 14400) . "\n"'` now i need to get this in a loop and increment that time by 15 minutes i.e i=900(=15minutes) `perl... (2 Replies)
Discussion started by: maanik85
2 Replies

10. Shell Programming and Scripting

Compare file time

i need to write script where I need to keep monitoring a files timestamp, if it changes, I need to run another abc.sh script. I am thinking I can save file's current timestamp in another file or enviornment variable and after 10 min compare the files timestamp with the original timestamp. If... (1 Reply)
Discussion started by: pdr302
1 Replies
Login or Register to Ask a Question