Shell script to work on dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to work on dates
# 1  
Old 12-02-2013
Code Shell script to work on dates

Hi Sir/Madam

I have a file data.txt like below
file_name date_of_creation
Code:
x                        2/10/2012
y                        8/11/2010
z                        11/3/2013
a                        2/10/2013
b                        3/10/2013
c                        2/10/2012
d                       3/10/2012
e                        9/11/2010

Now i need to prepare a script where i can pass the 2 dates (as in above format only) as arguments and i need to get the files created between those 2 dates.

eg: scriptname.sh 2/10/2012 11/3/2013

output should be
Code:
x                       2/10/2012
c                       2/10/2012
d                       3/10/2012
z                       11/3/2013

Could anyone please help me in this?

I stuck up with how to read dates as arguments and how to grep the date between those dates.

Last edited by radoulov; 12-02-2013 at 06:02 AM..
# 2  
Old 12-02-2013
is it DD/MM/YYYY or MM/DD/YYYY ?
# 3  
Old 12-02-2013
all the dates in the file are as dd/mm/yy format
# 4  
Old 12-02-2013
Code:
#!/bin/ksh

#2/10/2012 11/3/2013

FromDt=$1
ToDate=$2

fd=$(echo $FromDt | awk -F"/" '{print $1}')
fm=$(echo $FromDt | awk -F"/" '{print $2}')
fY=$(echo $FromDt | awk -F"/" '{print $3}')

td=$(echo $ToDate | awk -F"/" '{print $1}')
tm=$(echo $ToDate | awk -F"/" '{print $2}')
tY=$(echo $ToDate | awk -F"/" '{print $3}')

FS=$(date -d "$fm/$fd/$fY" '+%s')
TS=$(date -d "$tm/$td/$tY" '+%s')

while read line
do
      d=$(echo $line | awk -F"[/ ]" '{print $2}')
      m=$(echo $line | awk -F"[/ ]" '{print $3}')
      Y=$(echo $line | awk -F"[/ ]" '{print $4}')
      S=$(date -d "$m/$d/$Y" '+%s')
      [ $S -ge $FS -a $S -le $TS ] && print $line

done < filename

# 5  
Old 12-02-2013
Thank you very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script to Loop through Quarter dates

Hi, Trying to automate a Postgres query using Shell script Every month 1st week has to run a Monthly Queries to generate delimited files. July 1st start of fiscal yr which has 4 Quarters until next June 30th Example If I'm running on Sept 5th it has to generate one file(Becuase it... (12 Replies)
Discussion started by: krux_rap
12 Replies

2. Shell Programming and Scripting

Comparing dates in shell script

Hi All, I have a date variable say dt="2014-01-06 07:18:38" Now i need to use this variable to search a log and get the entries which occured after that time. (1 Reply)
Discussion started by: Girish19
1 Replies

3. Shell Programming and Scripting

Need Help:Shell Script for Solaris to change the dates in a file by one week

I have to increase the date by one week in an input when script is executed in solaris. I was able to acheive this using ksh script that is working in Linux enivironment, when i execute the same script in Solaris i am getting below error: /var/tmp\n\r-> ./script.ksh date: illegal option -- d... (3 Replies)
Discussion started by: sriramanaramoju
3 Replies

4. Shell Programming and Scripting

Shell script to calculate difference between 2 dates

shell script to calculate difference between 2 dates (3 Replies)
Discussion started by: gredpurushottam
3 Replies

5. Shell Programming and Scripting

append dates going forward from today to certain line in shell script

Hi there, I have a requirement to append dates going forward to a certain line in a file. I'm not sure of how to go about this. Any help will be greatly appreciated. Thanks Slyesco:wall: (2 Replies)
Discussion started by: Slyesco
2 Replies

6. UNIX for Dummies Questions & Answers

How to work command 'cd' in shell script?

I have simple script. like that, I am working on /usr/local/src and also under src folder there is a ft folder #!/bin/ksh #!/bin/bash dirpath="/usr/local/src/ft" echo $dirpath cd $dirpath echo displays ok "/usr/local/src/ft" but that doesn't enter "ft" folder. stays in current... (4 Replies)
Discussion started by: F@NTOM
4 Replies

7. Shell Programming and Scripting

Difference of 2 dates in shell script

Hi., After retrieving values from DB I have two datestamps in format: 12/01/2010:05:40:00 AM and 12/01/2010:06:00:00 PM. general time format: MM/DD/YYYY:HH:MM:SS AM or PM Any quick solution to get the difference of two in the format : 1 day(s) 12:20:00 Thanks., (6 Replies)
Discussion started by: IND123
6 Replies

8. Shell Programming and Scripting

Shell script to generate daily crontab entries between to specific dates

Hi, I am currently writing a shell script to enter daily reoccurring jobs into the crontab. I want to be able to specify any two dates for which I want these daily jobs to run between. Below is an example of what I need to be written to crontab. # Give a start day of 21/10/2009 09:00... (2 Replies)
Discussion started by: JM_86
2 Replies

9. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. Shell Programming and Scripting

How to compare the dates in shell script

Hi How to compare created or modified date of two files help needed thanks Vajiramani :) (9 Replies)
Discussion started by: vaji
9 Replies
Login or Register to Ask a Question