Unix/Linux Go Back    


Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

Helpm with time function

Homework & Coursework Questions


Tags
awk

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-23-2016
alula alula is offline
Registered User
 
Join Date: Dec 2016
Last Activity: 15 January 2017, 5:43 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Helpm with time function

I want to print the difference (in days) between ($7) and the oldest record date ($6) based on unique ID ($5) on a new field. In addition, I want to subtract oldest date from recent dates(in days) ($6) for each unique ID ($5).

Here is the data looks like

Code:
 7  81  1    47  32070  2010-12-14    20101009  
 7  82  2    10  41920  2010-12-14    20100724  
 7  83  1    67  29446  2010-12-14    20101118   
 7  81  1    47  32070  2011-5-11     20101009     
 7  83  1    67  29446  2011-6-22     20101118     
 7  82  2    10  41920  2011-5-14     20100724

I would like to see as follows

Code:
 7  81  1    47  32070   2010-12-14   20101009     65      147    
 7  82  2    10  41920   2010-12-14   20100724     170     150  
 7  83  1    67  29446   2010-12-14   20101118     26      188  
 7  81  1    47  32070   2011-5-11    20101009      65      147  
 7  83  1    67  29446   2011-6-22    20101118      26      188  
 7  82  2    10  41920   2011-5-14    20100724      170     150​

I have used the following code but doesnt work well. could you help me please.



Code:
BEGIN{
  lastid=0
  lastmilk=0
}
{
   milkyear=substr($6,1,4)
  milkmonth=substr($6,5,2)
  milkday=substr($6,7,2)
  
  startmilksec = mktime(""milkyear" "milkmonth" "milkday"  00")
  endmilk = mktime(""milkyear" "milkmonth" "milkday" 00")
   if(lastid!=$5) 
  {
    printf("%s 0 0 0\n", $0) 
  }
  else
  {
    startmilkdiff=startmilk-lastmilk
    printf("%s %0.f %f %f\n", $0, startmilkdiff) # 
  }
   lastid=$5
  lastmilk=startmilk
}



Moderator's Comments:
Helpm with time function Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-23-2016 at 11:04 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 12-23-2016
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 26 June 2017, 8:08 AM EDT
Location: Lancashire, UK
Posts: 3,141
Thanks: 1,316
Thanked 600 Times in 542 Posts
Quote:
...code but doesnt work well.
Sadly, the above doesn't help us diagnose it very well. What output/errors do you get?
Can you get any trace output from/after the mktime function? I think that the date input to mktime is actually a string in the format YYYY MM DD HH MM SS and you don't have all of it, hence I'm wondering if that's where it's going wrong. There is also confusion about all the double quotes.

I also don't see where you are trying to output the days as days. You are displaying the output as the raw difference between two timestamps, which we don't know if they are formatted correctly.

Can you add some printf statements into your code and show us the output from a single input record?



Kind regards,
Robin
Sponsored Links
    #3  
Old Unix and Linux 12-23-2016
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 June 2017, 5:34 PM EDT
Location: Aachen, Germany
Posts: 10,956
Thanks: 280
Thanked 3,368 Times in 3,101 Posts
Your specification is not too clear, and the code doesn't help understanding nor interpreting .
It is always beneficial to show WHAT exactly "doesn't work well". A few comments that jump to mind looking at your code :
- where is $7 used for computations?
- milkmonth and milkday will not have correct values as the substr parameters are wrong and could be moving.
- lastmilk is undefined for the first line, and not reset if $5 changes. This may be intended behaviour, though.
- endmilk is never used; why not drop it entirely?
- as startmilkdiff is the only variable in the printf stament, three %f format specifiers are redundant.
- as correspondent $5 values are NOT in sequence, you'd better work with arrays, or do a sort beforehand.

EDIT:
- plus, the mktime function requires 6 or 7 parameters; you supply 4 only.
- you want to output the difference in days, but calculate and print second values only.

Last edited by RudiC; 12-23-2016 at 12:15 PM..
    #4  
Old Unix and Linux 12-23-2016
alula alula is offline
Registered User
 
Join Date: Dec 2016
Last Activity: 15 January 2017, 5:43 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
the data has a repeated record on different days for the same Id [$5] and i want to print the difference between the oldest and recent record date (in days) [$6] for each unique ID on separate field. In addition, i want to print separately also the difference between field [$7] (which is a date) and the oldest record date of field [$6] for each unique ID. I am struggling to write the command. could you help me in writting the script please.
Sponsored Links
    #5  
Old Unix and Linux 12-24-2016
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 26 June 2017, 11:41 AM EDT
Location: NM
Posts: 11,095
Thanks: 536
Thanked 1,067 Times in 989 Posts
Want help? Show us your output as your code now produces please.
Sponsored Links
    #6  
Old Unix and Linux 12-24-2016
alula alula is offline
Registered User
 
Join Date: Dec 2016
Last Activity: 15 January 2017, 5:43 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
I need to print the difference (in days) in ($6) between the starting and end date of records for each unique ID ($5) on a new field.

[
Code:
 7  65  2    5   32070  2010-12-14    13:25:30  
 7  82  2    10  41920  2010-12-14    11:30:45
 7  83  1    67  29446  2010-12-14    04:15:25  
 7  81  1    47  32070  2011-5-11      08:14:20
 7  83  1    67  29446  2011-6-22      07:13:24    
 7  82  2    10  41920  2011-5-14      06:15:25

]

I want to see like this:
code:
[
Code:
 7  65  2    5   32070   2010-12-14      13:25:30      65     
 7  82  2    10  41920   2010-12-14      11:30:45     170  
 7  83  1    67  29446   2010-12-14       04:15:25    26   
 7  81  1    47  32070   2011-5-11         08:14:20    65    
 7  83  1    67  29446   2011-6-22         07:13:24    26   
 7  82  2    10  41920   2011-5-14          06:15:25   70

]

Last edited by Scrutinizer; 12-24-2016 at 01:50 PM.. Reason: code tags
Sponsored Links
    #7  
Old Unix and Linux 12-25-2016
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 26 June 2017, 11:41 AM EDT
Location: NM
Posts: 11,095
Thanks: 536
Thanked 1,067 Times in 989 Posts
This is some awk code. Because you wanted to add days to lines where it could not be calculated, this code is two awk scripts in one bash script. It could be cleaned up.
It adds -1 to lines in error, error being a singleton line.

Code:
$ cat filename && ./t.awk
7  65  2    5   32070  2010-12-14    13:25:30
7  82  2    10  41920  2010-12-14    11:30:45
7  83  1    67  29446  2010-12-14    04:15:25
7  81  1    47  32070  2011-5-11      08:14:20
7  83  1    67  29446  2011-6-22      07:13:24
7  82  2    10  41920  2011-5-14      06:15:25
7  82  2    10  41921  2011-5-14      06:15:25

7  65  2    5   32070  2010-12-14    13:25:30   147
7  82  2    10  41920  2010-12-14    11:30:45 150
7  83  1    67  29446  2010-12-14    04:15:25   189
7  81  1    47  32070  2011-5-11      08:14:20 147
7  83  1    67  29446  2011-6-22      07:13:24     189
7  82  2    10  41920  2011-5-14      06:15:25 150
7  82  2    10  41921  2011-5-14      06:15:25 -1


Code:
# 7  82  2    10  41920  2011-5-14      06:15:25


awk '{
       split($6,arr,"-")
       a=sprintf("%s %s %s 0 0 0",arr[1], arr[2], arr[3])
       d=mktime(a)
       # this is to handle the fact that you want the same data early and late 
       # in the output
       delta[$5]=delta[$5] " " d
     } 
     END {for(i in delta) {print i, delta[i]}  }'  filename > tmp.dat

#    
# display output, print error "-1" if each $5 does not occur twice in the input file
awk '{
      if (FILENAME=="tmp.dat" )
      { 
        delta[$1]=$0; 
        next
      }
      if (FILENAME=="filename")
      { 
        a="-1"  # default is error
        if($5 in delta)
        {
           cnt=split(delta[$5],arr)
           if(cnt==3) # correct number, 1 for $5 two & three are epoch seconds
           {
             a=arr[3] - arr[2]
             a/=86400
             a=int(a)
           }
        }
        print $0, a      
        next
      }
    }' tmp.dat filename   
# note the order of files here is important

The Following User Says Thank You to jim mcnamara For This Useful Post:
RavinderSingh13 (03-30-2017)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
time function nicos UNIX for Dummies Questions & Answers 2 12-13-2008 11:41 PM
Nawk Time Function Raynon Shell Programming and Scripting 4 11-01-2007 06:15 AM
function time scotty_123 Programming 2 05-04-2007 09:51 AM
time function hsekol Shell Programming and Scripting 10 03-20-2007 12:11 AM
C time in milliseconds function. kamil Programming 3 07-09-2003 12:20 PM



All times are GMT -4. The time now is 06:29 PM.