Date Arithimetic in shell scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date Arithimetic in shell scripts
# 1  
Old 05-29-2014
Date Arithimetic in shell scripts

Hi,

I have a file which has a date extension to it.
Like <filename>_01012014.

I need to rename the file as 12152013 , that is i need to be able to subtract 15 days from the date of the file name.

Please advise how this can be handled using shell scripting.

Regards,
VN
# 2  
Old 05-29-2014
Code:
$ cat tester
#!/bin/bash

file="filename_01012014"
touch $file
cp $file "${file%%_*}_"$(date -d"$(sed -r 's/(.{2})(.{2})(.{4})/\3\2\1/' <<<"${file##*_}") -15 days " +"%d%m%Y")

Code:
$ bash tester
$ ls filename_* -1
filename_01012014
filename_17122013

if happy replace cp with mv

for multiple files try this

Code:
#!/bin/bash

for file in filename_*; do
cp $file "${file%%_*}_"$(date -d"$(sed -r 's/(.{2})(.{2})(.{4})/\3\2\1/' <<<"${file##*_}") -15 days " +"%d%m%Y")
done

Tested with
Code:
GNU sed version 4.2.1
date (GNU coreutils) 8.13
GNU bash, version 4.2.25(1)-release (i686-pc-linux-gnu)

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 05-29-2014
I'm using korn shell.

sh tester
tester: syntax error at line 4: `(' unexpected

Please advise
# 4  
Old 05-29-2014
use ksh <scrptname>
# 5  
Old 05-29-2014
Thanks for your inputs.

ksh tester
tester[4]: syntax error at line 1 : `<' unexpected

I still get an error
# 6  
Old 05-29-2014
The problem appears to be GNU date. That is generally available on Linux servers, not other kinds of UNIX. What UNIX are you running the script on?
# 7  
Old 05-29-2014
Oracle Solaris and am using korn shell
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Running Multiple Scripts for different business date

Hi Team, I have the below 4 scripts which I will be running in sequential order. This run will start for today's business date. If all the 4 scripts are success for today that means script has ran succesfully. Howver if any one of these 4 scripts failed then it has to take the next... (1 Reply)
Discussion started by: Deena1984
1 Replies

2. UNIX for Dummies Questions & Answers

Shell Scripts - Append a filename with date and time....

Hello, I need to create a shell script that appends a filename to create a name with the date and time appended that is guaranteed to not exist. That is, the script insures you will not overwrite a file with the same name. I am lost with this one. I know I need to use date but after that I am... (3 Replies)
Discussion started by: citizencro
3 Replies

3. UNIX for Dummies Questions & Answers

shell scripts - create a filename with the date appended

I am looking to do something where if I created a file named backup,or whatever it would print a name like “backup_Apr_11_2011”. Thanks citizencro (1 Reply)
Discussion started by: citizencro
1 Replies

4. UNIX for Dummies Questions & Answers

Shell Scripts - shows today’s date and time in a better format than ‘date’ (Uses positional paramete

Hello, I am trying to show today's date and time in a better format than ‘date' (Using positional parameters). I found a command mktime and am wondering if this is the best command to use or will this also show me the time elapse since 1/30/70? Any help would be greatly appreciated, Thanks... (3 Replies)
Discussion started by: citizencro
3 Replies

5. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

6. UNIX for Advanced & Expert Users

date calculation scripts(hp-ux)

Hi all, i need a script that can check if users did an operation within 3 days,if not delete all the logs of users who did not perform any activity after 3 days.therefore script should be able to use current date and verify last date user performed activity and see if it is greater or less than 3... (3 Replies)
Discussion started by: tomjones
3 Replies

7. Shell Programming and Scripting

global date for many scripts in shell

Hi, I am using shell (#!/bin/bash), i am trying to set my date in a script date.sh, i want this dates to be used by another scripts. I have tried to make: #!/bin/bash begin_date =`date +%Y_%m_%d_%H_%M_%S`, i also tried: #!/bin/bash begin_date =20080709 12:14:11, then i write in... (11 Replies)
Discussion started by: rosalinda
11 Replies

8. Shell Programming and Scripting

replace old date in scripts with new date

Hiee .... i want an immediate solution for this.... Let replace.cfg file contains a list of files to be replaced the old dates with new dates...... The script must take 3 parameters.... 1) old date 2) new date 3) .config file Can u get me the solution...... I tried with sed but i... (2 Replies)
Discussion started by: prasanna1983
2 Replies

9. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies

10. Shell Programming and Scripting

Determine date and time the file was created through shell scripts

Can I determine when the particular file was created, in korn-shell. Can please someone help me. If possible please mail the solution to me. my mail id: bharat.surana@gmail.com (1 Reply)
Discussion started by: BharatSurana
1 Replies
Login or Register to Ask a Question