ksh to get longest timediff report?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh to get longest timediff report?
# 1  
Old 01-03-2012
ksh to get longest timediff report?

Hi all,

I have a report.log
Code:
1,BEAR,2011-11-21 08:49:16.000,2011-11-21 08:53:13.910,San Francisco,,,
2,BEAR,2011-11-21 08:49:16.000,2011-11-21 12:50:31.550,San Francisco,,,
3,BEAR,2011-11-21 08:49:16.000,2011-11-21 08:49:19.987,San Francisco,,,
4,HAWK,2011-11-21 10:36:26.000,2011-11-21 10:45:11.823,Los Angeles,,,
5,HAWK,2011-11-21 10:36:26.000,2011-11-21 12:41:17.763,Los Angeles,,,
6,HAWK,2011-11-21 10:41:12.000,2011-11-21 10:55:08.393,San Francisco,,,
7,HAWK,2011-11-21 10:41:12.000,2011-11-21 15:46:24.707,San Francisco,,,
8,HAWK,2011-11-21 10:41:12.000,2011-11-22 11:09:13.907,San Francisco,,,

How do you write a shell script to get from report.log and write to result.log? Thanks
Which team , location and how long? (longest time end time - start time)
Example:
Code:
BEAR,San Francisco, timediff (2011-11-21 12:50:31.550 - 2011-11-21 08:49:16.000) in hours
HAWK,Los Anegles, timediff (2011-11-21 12:41:17.763 - 2011-11-21 10:36:26.000) in hours
HAWK,San Francisco, timediff (2011-11-22 11:09:13.907 - 2011-11-21 10:41:12.000) in hours


Last edited by Franklin52; 01-04-2012 at 03:39 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-03-2012
For the benefit of others, please post your Operating System and version and make it 100% clear whether you have the GNU version of the "date" command on your system.
# 3  
Old 01-04-2012
Sorry, it is Linux 2.6.9-67.ELsmp and date --version
date (coreutils) 5.2.1
Thanks
# 4  
Old 01-04-2012
Assuming no ordering of the time and not caring about the fractions of the second:
Code:
gawk -F, '{
  k = $2 "," $5
  if (!(k in a) || a[k] > $3)
    a[k] = $3
  if (b[k] < $4)
    b[k] = $4
}
END {
  for (i in a) {
    gsub(/[^0-9]/, " ", a[i])
    gsub(/[^0-9]/, " ", b[i])
    printf("%s, %.2f\n", i, (mktime(b[i]) - mktime(a[i]))/3600)
  }
}'

# 5  
Old 01-04-2012
How do you read from report.log? may i ask?
Code:
gawk -F, '{
  k = $2 "," $5
  if (!(k in a) || a[k] > $3)
    a[k] = $3
  if (b[k] < $4)
    b[k] = $4
}
END {
  for (i in a) {
    gsub(/[^0-9]/, " ", a[i])
    gsub(/[^0-9]/, " ", b[i])
    printf("%s, %.2f\n", i, (mktime(b[i]) - mktime(a[i]))/3600)
  }
}' report.log

I got error
gawk: testit:2: gawk -F, '{
gawk: testit:2: ^ invalid char ''' in expression

Last edited by sabercats; 01-04-2012 at 08:07 PM.. Reason: adding code
# 6  
Old 01-04-2012
Appart from the miliseconds time causing issues for mktime it worked fine for me.

Try putting this updated code in a testit.awk file:
Code:
{
  k = $2 "," $5
  if (!(k in a) || a[k] > $3)
    a[k] = $3
  if (b[k] < $4)
    b[k] = $4
}
END {
  for (i in a) {
   gsub(/\.[0-9][0-9][0-9]$/ , "", a[i])
   gsub(/\.[0-9][0-9][0-9]$/ , "", b[i])
    gsub(/[^0-9]/, " ", a[i])
    gsub(/[^0-9]/, " ", b[i])
        printf("%s, %.2f\n", i, (mktime(b[i]) - mktime(a[i]))/3600)
  }
}

And the use:
Code:
$ awk -F, -f testit.awk report.log
HAWK,San Francisco, 24.47
HAWK,Los Angeles, 2.08
BEAR,San Francisco, 4.02

# 7  
Old 01-05-2012
Thanks, it worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing the longest match substring

Hello gurus, I have a database of possible primary signal strings pp22 pt22dx pp22dx jty2234 Also I have a list of scrambled signals which has a shorter string and a longer string separated by // (double slash ). Always the shorter string of a scrambled signal will have the primary... (6 Replies)
Discussion started by: senhia83
6 Replies

2. Shell Programming and Scripting

HTML Report from mail in ksh 88

Hi Team, I'm trying to send HTML Report from email using the below script (Using ksh 88 version) #!/bin/ksh set -x SUB="Test Email" Mail_Body="This is for testing" Send_Mail_HTML() { ( echo "FROM: abcd@test.com" echo "To: abcd@test.com" echo "Subject:... (2 Replies)
Discussion started by: smile689
2 Replies

3. Shell Programming and Scripting

Longest word in a file

I am trying to write a command on just one line, i.e seperated by ';' and '|' etc, that finds the number of characters in the longest word of a file, preferably using the 'tr' and 'wc' commands. i no that wc shows the number of lines words and characters in a file but im not sure how to use it... (5 Replies)
Discussion started by: scotty85
5 Replies

4. Shell Programming and Scripting

Longest length of string in array

I would be grateful if someone could help me. I am trying to write a .sh script in UNIX. I have the following code; User=john User=james User=ian User=martin for x in ${User} do print ${#x} done This produces the following output; 4 5 3 6 (12 Replies)
Discussion started by: mmab
12 Replies

5. Shell Programming and Scripting

Finding longest line in a Record

Good Morning/Afternoon All, I am using the nawk utility in korn shell to find the longest field and display that result. My Data is as follows: The cat ran The elephant ran Milly ran too We all ran I have tried nawk '{ if (length($1) > len) len=length($1); print $1}' filename The... (5 Replies)
Discussion started by: SEinT
5 Replies

6. Shell Programming and Scripting

longest, shortest, map wrong

Hi Everyone, a.txt cola,colb1111 601030,CEL 6031,CEL 60103,CEL Would like to find the longest field colb1111, and shortest, so the output is 6, 4 my code: #!/usr/bin/perl use strict; use warnings; my %prefix_to_contry = (); my $key; open(FH,'/root/a.txt') or die "$!"; (0 Replies)
Discussion started by: jimmy_y
0 Replies

7. Shell Programming and Scripting

shell script: longest match from right?

Return the position of matched string from right, awk match can do from left only. e.g return pos 7 for search string "service" from "AA-service" or return the matched string "service", then caculate the string length. Thanks!. (3 Replies)
Discussion started by: honglus
3 Replies

8. Shell Programming and Scripting

Find the length of the longest line

Dear All, To find the length of the longest line from a file i have used wc -L which is giving the proper output... But the problem is AIX os does not support wc -L command. so is there any other way 2 to find out the length of the longest line using awk or sed ? Regards, Pankaj (1 Reply)
Discussion started by: panknil
1 Replies

9. UNIX for Advanced & Expert Users

in sed ,to get longest word

i want the longest word from the file using sed. can any one help me in this case? (6 Replies)
Discussion started by: lakshmananindia
6 Replies

10. Shell Programming and Scripting

TimeDiff function?

Hi, Is there any function in Unix that could get the Time Differrence of two values? this would be used as statistic for how long a job completes, from start to finish.. Please help. thanks, Jet (12 Replies)
Discussion started by: kakashi_jet
12 Replies
Login or Register to Ask a Question