UNIX Shell scripting (digit based subtraction)? - shell

I have an input like 2013_07_02 (It may be any date). I want to retrieve date which is 2 days back from the input date. How to do so?

In case you want the output in the same format as the input, i.e. 2013-07-02, substitute _ with - before passing to date:
$ inputdate='2013_07_02';
$ date --date=${mydate//_/-}'-2 day' +'%Y_%m_%d'
2013_06_30

date is your friend:
date -d "2013-01-01 -2 days" +"%Y-%m-%d"
This prints out 2012-12-30.

use date with -d option. It understands relative dates, even with common language like
date -d "two days ago".
In your case something like
date -d "2013-07-01 -2 days"
would be enough.

Related

awk text replacement script

I'm trying to write a script that takes today's date and replaces it with tomorrow's date. My problem relies on the awk command. Here's what I've done so far:
date_today=`date`
day_today=`echo $date_today | awk '{print $3}'`
day_tomorrow=$(( ++day_today ))
date_tomorrow=`echo $date_today | awk '{print $3=$day_tomorrow}'`
But it doesn't print the expected date. I'm just trying to replace today's date number by tomorrow's date number.
Since you haven't shown in which format you need to the complete date so I am assuming it could be year-month-date format.
Solution 1st: With GNU date in case you have GNU date in your system then following may help you on same.
date --date="+1 day" +"%Y-%m-%d"
Solution 2nd: With a non-GNU date following may help you in same.(Since I have GNU date in my system so I couldn't test it)
date -d #$(( $(date +"%s") + 86400)) +"%Y-%m-%d"
Also in case you don't need it in above format you could change it by changing %Y-%m-%d in above codes.

Construct a new date by adding X number of hours (Unix Shell)

Given a UNIX date variable, I need to construct a new date adding a certain number of hours (must be variable) to the initial date. I've looked at this post, which explains how to do this with the current date. For example with 1 hour, date -d '+1 hour' '+%F %T' returns the time it will be in exactly one hour. However, I need to do this with a date variable, not with the current date.
I've tried messing around with the -d flag, but if I set the date to another date variable, I can't figure out how to change it again (such as adding another X number of hours).
Is there a good way to do this? Am I on the right track with the -d flag or is there a better way?
Thanks!
You can use:
# your date variable
dt=$(date -d '2016-08-15 11:10:15')
# add 1 hour to $dt now
date -d "$dt +1 hour"
Mon Aug 15 12:10:15 EDT 2016

Read Date in a file and add one day

I want to read a file which contain a date and rewrite this file with plus one day
I have this command line :
date --date=$(cat /parm/control.date) "+%Y-%m-%d" -d "tomorrow" > control.date
When I run many times this command it return tomorrow of the current date ..
Thank you :)
date --date="$(< /dz-ceibo/applis/db5/parm/control.date) + 1 day" "+%Y-%m-%d"
Using bash's $(< file) instead of cat

How to date as a user input in shell scripting?

I am new to shell scripting..
I want a script to get any date as a input from user and print date of 3 days back?
example:
If user enters date as 2013-01-01
then output should be
2012-12-29.
If you have GNU date, then this will work:
user_date=2013-01-01
date +%Y-%m-%d -d "$user_date - 3 days"
With BSD date, you'd have to do like this:
user_date=2013-01-01
date -j -v -3d +%Y-%m-%d -d "${user_date//-}0000"
because BSD date needs date to be in the format YYYYmmddHHMM.
I don't have a Solaris now to test there. If you're in Solaris then hopefully there is gdate, and you can use the first option, just replace the date command with gdate.
Whichever OS you are in, there are two important points:
In what format can you pass dates to the date command. I tested that GNU date can accept YYYY-mm-dd format (and probably many others), while BSD needs YYYYmmddHHMM.
In what format can you ask for a difference. With GNU date simply DATE - 3 days works, with BSD date it's trickier with -j -v -3d flags.
man date of your system should help you get through these hurdles. In the worst case, you could do all the date operations you need in perl or similar.
You can just do:
date --date="3 days ago"
to get get date of 3 days back.

getting a previous date in bash/unix

I am looking to get previous date in unix / shell script .
I am using the following code
date -d ’1 day ago’ +’%Y/%m/%d’
But I am getting the following error.
date: illegal option -- d
As far as I've read on the inetrnet , it basically means I am using a older version of GNU. Can anyone please help with this.
Further Info
unix> uname -a
SunOS Server 5.10 Generic_147440-19 sun4v sparc SUNW,Sun-Fire-T200
Also The below command gives an error.
unix> date --version
date: illegal option -- version
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
try this:
date --date="yesterday" +%Y/%m/%d
dtd="2015-06-19"
yesterday=$( date -d "${dtd} -1 days" +'%Y_%m_%d' )
echo $yesterday;
you can use
date -d "30 days ago" +"%d/%m/%Y"
to get the date from 30 days ago, similarly you can replace 30 with x amount of days
In order to get 1 day back date using date command:
date -v -1d It will give (current date -1) means 1 day before .
date -v +1d
This will give (current date +1) means 1 day after.
Similarly below written code can be used in place of d to find out year,month etc
y-Year,
m-Month
w-Week
d-Day
H-Hour
M-Minute
S-Second
Several solutions suggested here assume GNU coreutils being present on the system. The following should work on Solaris:
TZ=GMT+24 date +’%Y/%m/%d’
SunOS ships with legacy BSD userland tools which often lack the expected modern options. See if you can get the XPG add-on (it's something like /usr/xpg4/bin/date) or install the GNU coreutils package if you can.
In the meantime, you might need to write your own simple date handling script. There are many examples on the net e.g. in Perl. E.g. this one:
vnix$ perl -MPOSIX=strftime -le 'print strftime("%Y%m", localtime(time-86400))'
201304
(Slightly adapted, if you compare to the one behind the link.)
the following script prints the previous date to the targetDate (specified Date or given date)
targetDate=2014-06-30
count=1
startDate=$( echo `date -d "${targetDate} -${count} days" +"%Y-%m-%d"`)
echo $startDate
I have used the following workaround to get to the required solution .
timeA=$(date +%Y%m)
sysD=$(date +%d)
print "Initial sysD $sysD">>LogPrint.log
sysD=$((sysD-1))
print "Final sysD $sysD">>LogPrint.log
finalTime=$timeA$sysD
I hope this is useful for people who are facing the same issue as me.
$ date '+%m/%d/%Y' --- current date
$ TZ=Etc/GMT+24 date '+%m/%d/%Y' -- one dayprevious date
Use time zone appropriately
Problem
You are using backticks, rather than single quotes, for your arguments. You may also not be using GNU date, or a version of date that supports the flag you are using.
Solution
Quote your arguments properly. For example:
$ date -d '1 day ago' +'%Y/%m/%d'
2013/04/14
Try This:
gdate -d "1 day ago" +"%Y/%m/%d"
date -d "yesterday" '+%Y-%m-%d'

Resources