How to search file for a date variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search file for a date variable?
# 1  
Old 11-26-2012
How to search file for a date variable?

Hello,

I'm trying to write a ksh script which will allow me to to search for yesterday's date within a rtf file. I just need someway to know if the date is in the file, echo results to a text file, and then mail that out.

The format of the date is mm/dd/yyyy.

I had to make a variable for the date I'm searching for since "yesterday's date" can vary.

If this script is ran 5pm-midnight EST I need to look for the current date.

If ran after midnight but before 5pm, I will need to look for yesterday's date.



I have simplified the file names and paths for compliance reasons, but right now I have:

Code:
SOURCE_PATH="/path"
SOURCE_FILE="filename.rtf"
 
 
THE_TIME_HR=$(date "+%H")
 
#Use yesterday's date if ran after midnight EST, use current date if ran before midnight EST

 
if [ $THE_TIME_HR -ge 17 ]; then
 YEST_DATE=$(date "+%m/%d/%Y") #yesterday's date US
else
 YEST_DATE=$(TZ=EST+24 date "+%m/%d/%Y")
fi
 
#Look for min date for file validity, if nothing is returned then file should be correct
grep ${YEST_DATE} ${SOURCE_PATH}${SOURCE_FILE)

if [ $? -ne 0]; then

echo "Yesterday's date in file, please correct and reload" > $SOURCE_PATH/file.txt
 
else
 
echo "Yesterday's date not found in ClearTran file" > $SOURCE_PATH/file.txt
 
fi
 
#end of script

I'm getting the error "syntax error at line 25 : `>' unexpected" (the line I bolded) after the second if statement when I try to echo results to a text file.

Does anyone know what the issue is? Thanks in advance!

Also let me know if I need to clarify anything, I'm a new programmer.

Jay

Last edited by joeyg; 11-26-2012 at 02:33 PM.. Reason: Please wrap data and sripts with CodeTags
# 2  
Old 11-26-2012
Pls correct these errors and test again:
Code:
grep ${YEST_DATE} ${SOURCE_PATH}${SOURCE_FILE}
                                             ^--- } in lieu of )
if [ $? -ne 0 ]; then
             ^--  here be space(s)

These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 11-27-2012
It worked!

Thanks joeyg :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

2. Shell Programming and Scripting

UNIX: Search file between two Date stamps

Hi, I have a requirement to find a file based on date stamp provided by user. In one of the shared location I get data file say “datafile<dataformat>“. And User will provide the date based on which I need to select a file which is 7 days older, if a file not present within 7 days, then I need... (3 Replies)
Discussion started by: santhosh86467
3 Replies

3. Shell Programming and Scripting

How to search log file from a date

Hi All, I want to grep through all the log files created from 20th August. How do I do that? My script will run everyday. So it will take current date as To_Date. My log file name looks like - applog-2012-08-20-000001....applog-2012-08-20-000002...etc. Thanks in advance. (5 Replies)
Discussion started by: kmajumder
5 Replies

4. Shell Programming and Scripting

Search on date range of file based on user input

Hello I would like to ask for help with a script to search a directory that contains many log files and based on a users input after being prompted, they enter a date range down to the hour which searches the files that contain that range. I dont know how to go about this. I am hoping that the... (5 Replies)
Discussion started by: lostincashe
5 Replies

5. Shell Programming and Scripting

How to search for file and display by date

I like "ls -ltr". I would like to search for a file in a large directory recursively and and display all the candidates in reverse order. /usr/bin/find . -name \*.txt This works. How do I display the date and sort the file names by the date in reverse order for the entire directory... (1 Reply)
Discussion started by: siegfried
1 Replies

6. Shell Programming and Scripting

Search for a specific data in a file based on a date range

Hi, Currently I am working on a script to automate the process of converting the log file from binary into text format. To achieve this, partly I am depending on my application’s utility for this conversion and the rest I am relying on shell commands to search for directory, locate the file and... (5 Replies)
Discussion started by: svajhala
5 Replies

7. Solaris

Search date pattern in application log file

I am viewing a file in vi editor and would like to search for a date pattern. In the log, the timestamp is enclosed in parentheses ''. I am using the '/' option in vi to search for the pattern. log snippet: 000000f4 ServletWrappe I SRVE0242I: : Initialization successful. 000000f4... (3 Replies)
Discussion started by: Vangogh78
3 Replies

8. Shell Programming and Scripting

How to search a date format from a file an replace with a string in PERL

I am very new to Perl. I am struggling so hard to search a date (such as 10/09/2009, 10-09-2009) from a text file and replace with a string (say DATE) using Perl. Please help me out. Thanks in advance. Regds Doren (4 Replies)
Discussion started by: my_Perl
4 Replies

9. UNIX for Dummies Questions & Answers

Search for a file with Current Date

Hi, How can we search for a file with current date(sysdate). Actually i want the file name from a directory which was created today(current date). For example i have a dir /home/arch and i have files with name aglito03axyz.datetimestamp in this directory, but all these files were created... (7 Replies)
Discussion started by: sandeep_1105
7 Replies

10. UNIX for Advanced & Expert Users

find file with date and recursive search for a text

Hey Guyz I have a requirement something like this.. a part of file name, date of modification of that file and a text is entered as input. like Date : 080206 (MMDDYY format.) filename : hotel_rates text : Jim now the file hotel_rates.ZZZ.123 (creation date is Aug 02 2006) should be... (10 Replies)
Discussion started by: rosh0623
10 Replies
Login or Register to Ask a Question