Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-10-2012
Registered User
 
Join Date: Jun 2012
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
IF statement dealing with current time

Hi Guys,

I am trying to put together a script that will search a txt file for a timestamp and select all text after the initial one up to EOF but the timestamp searched for will depend on the current date

So if it the current time is between 0600 and 0700 the script will search a text file (currently open) for a timestamp in the form of YY/MM/DD(yesterdays date)HH:MM that is AFTER 1900 and up to the current date and time. If it finds a stamp within that window it should select all text from that stamp to EOF.
The other case is, if the current time is between 1800 and 2000 then the script should search for a timestamp in the same format but on today's date.
Then if it finds one it should select all text from that timestamp up and to EOF

Something like:
If ( 06:00>Now()<=07:00
then (search for YY/MM/DD HH:MM > YY/MM/DD(-1) 19:00)
if (search finds match)
select all up to EOF
else exit
else ( search for YY/MM/DD HH:MM > YY?MM/DD 07:00)
if (search finds match)
select all to EOF

Sorry for the above but it is (for me ) the easiest way to explain my needs!

After the selection is made I think I know how to print it to a new file but not 100% sure.

For now if I can make the required selection based on the terms I would be more than happy.
Any help would be appreciated greatly!

TIA
Sponsored Links
    #2  
Old 08-10-2012
Registered User
 
Join Date: Mar 2012
Location: Pune, India
Posts: 1,485
Thanks: 56
Thanked 425 Times in 421 Posts
From what i understand that u want to run script in between 06 - 07..
so u can run through crontab...

and to get yesterday's date


Code:
date --date="yesterday"

to add your required time format just append to to above code..


Code:
date --date="yesterday" +%Y/%m/%d_%H:%M

Sponsored Links
    #3  
Old 08-10-2012
Registered User
 
Join Date: Jun 2012
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
Thanks very much for the quick response pamu!

That has some very helpful info but one thing,
How to I search so that it will look for the date and time AFTER a given DATE + TIME?

So the timestamp could be anywhere from yesterday @ 1900 up to now (between 0600 and 0700) so when it finds it it should select all text in file after.

Thanks!
    #4  
Old 08-10-2012
Registered User
 
Join Date: Mar 2012
Location: Pune, India
Posts: 1,485
Thanks: 56
Thanked 425 Times in 421 Posts
Quote:
Originally Posted by martin0852 View Post
Thanks very much for the quick response pamu!

That has some very helpful info but one thing,
How to I search so that it will look for the date and time AFTER a given DATE + TIME?

So the timestamp could be anywhere from yesterday @ 1900 up to now (between 0600 and 0700) so when it finds it it should select all text in file after.

Thanks!
Could you please provide me the input file format...... or some input....
Sponsored Links
    #5  
Old 08-10-2012
Registered User
 
Join Date: Jun 2012
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
I have attached the file I want to pull the text from...
So this would be run between 06:00 and 07:00 and from the file I want anything done after 19:00 the previous day.
So the first set of action up to the action started at 01:33 "this morning" should not be pulled, just the actions timestamped after 19:00 yesterday,
Thanks!
Attached Files
File Type: txt log1361.txt (4.4 KB, 2 views)
Sponsored Links
    #6  
Old 08-10-2012
Registered User
 
Join Date: Mar 2012
Location: Pune, India
Posts: 1,485
Thanks: 56
Thanked 425 Times in 421 Posts
To search from yesterdays date after 19.00 ... use this..
It will print all the lines after the match found...

Code:
search_var=$(echo "`date --date="yesterday" +%Y-%m-%d`  19:")

sed '1,/'"$search_var"'/d'

The Following User Says Thank You to pamu For This Useful Post:
martin0852 (08-10-2012)
Sponsored Links
    #7  
Old 08-10-2012
Registered User
 
Join Date: Jun 2012
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
Hi Pamu,

I put this in as a shell command but get a warning saying Variable syntax search_var:undefined variable

If this is something to do with defining the file to search then I'm not sure how to fix it.

I am running the script as a shell_command in NEDIT so the script will be running on the currently opened file.

Also, will this just search for everything between 19:00 and 20:00??

Thanks
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
How far is given date from current time? SkySmart Shell Programming and Scripting 2 06-14-2012 12:06 AM
unziping to current time stamp posix Shell Programming and Scripting 4 06-14-2010 01:37 AM
compare file modification date/time to current date/time Bill Ma Shell Programming and Scripting 5 08-27-2009 12:02 PM
Need help for AWK output with current time roxy2k Shell Programming and Scripting 2 09-26-2008 01:01 PM
add 2hours in current time anup13 Shell Programming and Scripting 8 09-15-2008 04:11 AM



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