To get the time exactly 24hrs from the current time | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

To get the time exactly 24hrs from the current time

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-06-2013
mohanalakshmi mohanalakshmi is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 2 April 2014, 9:17 AM EDT
Posts: 43
Thanks: 9
Thanked 0 Times in 0 Posts
To get the time exactly 24hrs from the current time

Hi guys,

I am having file which contains below data.

Code:
2012-04-24 08:40:13     10739022730     1027699274PersonInfoShipTO details missing      FirstName,LastName,
2012-04-24 08:40:13     10739022730     1027699274PersonInfoShipTO details missing      FirstName,LastName,
2012-04-24 08:40:13     10739022730     1027699274PersonInfoShipTO details missing      FirstName,LastName,
2012-04-24 08:40:13     10739022730     1027699274PersonInfoShipTO details missing      FirstName,LastName,
2012-04-24 08:40:13     10739022730     1027699274PersonInfoShipTO details missing      FirstName,LastName,
2012-07-05 07:40:13     10739022730     1027699274PersonInfoShipTO details missing      FirstName,LastName,
2012-07-05 11:40:13     10739022730     1027699274PersonInfoShipTO detailsmissing      FirstName,LastName,
2013-07-05 08:40:13     10739022730     1027699274PersonInfoShipTO detailsmissing      FirstName,LastName,

First column represent the time stamp .My requirement is whenever i run my script it needs to go through the above file and should grep the line in which the time stamp should be between current time and 24hrs.
i.e
If i run the script by July 6 ,2013 10.00 pm ct then output should contains the date from July 5 ,2013 10.00 pm ct to July 6,2013 10.00 pm CT.
My OS version is

Code:
SunOS

Thank u guys.

Last edited by mohanalakshmi; 07-16-2013 at 09:00 AM.. Reason: wrong information
Sponsored Links
    #2  
Old 07-06-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 19 April 2014, 8:15 AM EDT
Location: NM
Posts: 10,058
Thanks: 250
Thanked 756 Times in 711 Posts
You will need to use C or perl or or ruby -- some language. bash, ksh, nawk and other standard utilities that come with Solaris do not "do dates" like the GNU coretuils do.
So if someone posts an example using "date" or "awk" it will not work unless you have the GNU coreutils installed.

Here is a C example, someone else may post a perl example.

Code:
// tsrch.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
//compile [g]cc -o tsrch tsrch.c
// usage ./tsrch < filename_to_search  NOTE the "<"

time_t then(const char *tmp)
{
    char *fmt="%Y-%m-%d %H:%M:%S ";
    struct tm tm;
    if(strptime(tmp, fmt, &tm)==NULL) // bad text in line
       return (time_t)0;
    
    return (time_t) mktime(&tm);
}

int main()
{
   time_t now=time(NULL);
   char tmp[1024]={0x0};
   while( fgets(tmp, sizeof(tmp), stdin)!=NULL )
   {    
      if(strlen(tmp)> 24 && 
         now - then(tmp) <=86400)  // seconds in 24 hours
      printf("%s", tmp);   
   }
   
   return 0;
}

/usr/sfw/bin/gcc (C compiler) is part of the standard Solaris 10 && 11 install
Sponsored Links
    #3  
Old 07-07-2013
Chubler_XL's Avatar
Chubler_XL Chubler_XL is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 17 April 2014, 12:50 PM EDT
Posts: 2,601
Thanks: 94
Thanked 804 Times in 757 Posts
Since your date format is sortable you could calculate the 24hour time using perl and then use a simple nawk script like this:

24hr.pl

Code:
#!/bin/perl
use POSIX; print strftime('%Y-%m-%d %H:%M:%S', localtime(time() - 24*60*60));

Then use this (24hr.pl executable and in current directory):

Code:
FROM=`./24hr.pl`
awk "\$0 > \"$FROM\"" infile

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Convert UTC time into current UNIX sever time zone mohanalakshmi Shell Programming and Scripting 5 12-11-2013 07:19 PM
Displaying current date time of EDT in IST time Showdown Shell Programming and Scripting 6 03-16-2013 10:17 AM
How far is given date from current time? SkySmart Shell Programming and Scripting 2 06-14-2012 12:06 AM
Getting current time from wolfram.com using python frozensmilz Shell Programming and Scripting 5 01-11-2010 04:08 AM
compare file modification date/time to current date/time Bill Ma Shell Programming and Scripting 5 08-27-2009 12:02 PM



All times are GMT -4. The time now is 08:40 PM.