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.
Related
In Solaris, I am trying to write a shell script that converts current date to the number of days after 1/1/1970 for Unix. This is because etc/shadow isn't using Epoch time but instead a 'days format'
i.e "root:G9yPfhFAqvlsI:15841::::::" where the 15841 is a date.
So in essence what command do I use to find out the epoch time for now and then convert that to days.
You probably don't have GNU tools, which might make things easier. This is simple enough though:
perl -le 'print int(time/86400)'
I found some pseudo code to calculate it from the basics:
if month > 2 then
month=month+1
else
month=month+13
year=year-1
fi
day=(year*365)+(year/4)-(year/100)+(year/400)+(month*306001/10000)+day
days_since_epoch=day-719591
Credit: http://www.unix.com/shell-programming-and-scripting/115449-how-convert-date-time-epoch-time-solaris.html). On the same forum thread, another poster said this would work in Solaris:
truss /usr/bin/date 2>&1 | grep ^time | awk -F"= " '{print $2}'
Solaris date utility doesn't support %s format. However, nawk utility has srand() function that returns date in seconds when there's no parameter passed to it.
nawk 'BEGIN {print srand()}'
Results in
1405529923
To get the days instead of seconds, you can divide the result by 86400.
nawk 'BEGIN {printf("%d", srand() / 86400)}'
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.
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.
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'
In awk, Is there a way to convert the time format from "2010-10-08 00:00:01" to 1286467201
as using the command "date"
$ date +%s -d '2010-10-08 00:00:01'
1286467201
GNU awk has a mktime function that can do the job. However, it's crucial to be aware of timezones. The string "2010-10-08 00:00:01" does not contain enough information to define a specific time. If you assume it is in UTC you can do:
$ echo 2010-10-08 00:00:01 | \
TZ=UTC gawk '{ tstr=$1" "$2; gsub(/[\-:]/, " ", tstr); print mktime(tstr); }'
1286496001
If you don't specify the TZ variable you end up with the server's time zone (which should be UTC anyway, but a lot of folks use local time on servers, so it's not a safe assumption).
You can get UTC output from your date command by altering it slightly:
$ date +%s -u -d '2010-10-08 00:00:01'