Read Date in a file and add one day - bash

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

Related

How to create a date generator in shell?

I want to pull some information from a website from past 4 years and each file is date based, like http://ransompull.com/pullme/2013-04-06/example.2013-04-06.txt
and it is the starting file and it ends today, so i want to pull all the txt files from last 4 years.
What I tried:
DATE=`date +%Y`
MONTH='01'
DAY='1'
for i in range(1,31);
for j in range(01,12):
do wget http://ransompull.com/pullme/$DATE$i/example.$DATE$i.txt;
done
done
But this seems to wrong as iterating over month and date is not feasible as it is not giving desired output.Any suggestions on how to pull all data from
http://ransompull.com/pullme/2013-04-06/example.2013-04-06.txt
to
http://ransompull.com/pullme/2017-08-10/example.2017-08-10.txt
Instead of counting years, months and days,
you could just count days relative to the start date.
If you have the GNU implementation of the date command,
you can use it to compute the relative date, for example:
date +%F -d '2013-04-06 + 1000 days'
This outputs 2016-01-01.
You can create a loop, generating dates by incrementing the number of days from start, until you reach the end:
start=2013-04-06
end=2017-08-10
date=$start
days=0
while [ "$date" != "$end" ]; do
date=$(date +%F -d "$start + $days days")
wget http://ransompull.com/pullme/$date/example.$date.txt
((days++))
done
try this:
$startdate=get-date 2017-08-11
$enddate=$startdate.AddYears(-4)
0..($startdate - $enddate).Days | %{wget ("http://ransompull.com/pullme/{0:yyyy-MM-dd}/example.{0:yyyy-MM-dd}.txt" -f $startdate.AddDays(-$_))}

How to get formatted date twice, ensuring it's the same time?

I have a Bash script which uses:
THE_DATE=`date +"%Y-%m-%d-%H%M%S"`
To get the current date and time in a format which can be used to mkdir new directory.
But, for report generation, I use something like:
REPORT_DATE=`date +"%Y-%m-%d %H:%M:%S"`
Which is the same, but different, if you see what I mean.
This means running date twice, and I understand that it's possible (although unlikely) for the seconds component to different between the two.
How, do I get the date in two different formats at the same time?
Get the date in a well-known format, and then pass it to date (using -d):
date="$(date +%s)"
the_date="$(date +'%Y-%m-%d-%H%M%S' -d "#${date}")"
report_date="$(date +'%Y-%m-%d %H:%M:%S' -d "#${date}")"
%s gets us the time in Unix time, which we can later use to make date print a specific time, instead of the current. The # prefix gives date a hint of the format.
It's possible that some other format could be used, but Unix time seemed the most universal and least error-prone.
(Off-topic: Don't use uppercase variable names, and use $() instead of backticks.)
You can call date only once, separating both dates by a newline (or other character) and read them:
IFS=$'\n' read -d '' -r the_date report_date < <(date +"%Y-%m-%d-%H%M%S%n%Y-%m-%d %H:%M:%S"; printf '\0')
or, similarly but with two calls to read:
{ read -r the_date; read -r report_date; } < <(date +"%Y-%m-%d-%H%M%S%n%Y-%m-%d %H:%M:%S")
So, basically, my answer is about calling date only once, and parsing its output…
As a side-note, since Bash 4.2, you can use the builtin printf instead of the external date:
{ read -r the_date; read -r report_date; } < <(printf "%(%Y-%m-%d-%H%M%S%n%Y-%m-%d %H:%M:%S)T\n" -1)
REPORT_DATE="$(date +"%Y-%m-%d %H:%M:%S")"
# Edit: Incorrect and slow: THE_DATE="$(echo "${REPORT_DATE}" | tr -d ":")
THE_DATE="$(echo "${REPORT_DATE//:}" | tr -d " " "-")"

UNIX Shell scripting (digit based subtraction)?

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.

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'

Use info from file name to rename file

I've a huge amount of file that are named like A2012178.1220.051.2012178233531.hdf There, from the 2nd character to the 8th is the date, year plus julian day. From the 13rd character to the extention of the file the name is rubbish to me... so I would like to rename the files and convert the date (for convenience).
So far I'm able to convert the date in bash
CONVERTED=$(date -d "${Year}-01-01 +${JulianDay} days -1 day" "+%Y%m%d")
But I have no idea of how to read the year and julian day from the file name and replace the name within the bash script....
Any idea?
Use parameter substitution to get the substrings:
name="A2012178.1220.051.2012178233531.hdf"
Year=${name:1:4}
JulianDay=${name:5:3}
The tricky part is that the julian day has a variable length, so you don't know ahead of time how to slice it out. So, you might do something like this:
read begin date end < <(echo $filename | sed -e 's/\(.\)\([[:digit:]]\+\)\(.*\)\
/\1 \2 \3/')
Year=${date::4}
JulianDay=${date:4}
newdate=$(date -d "${Year}-01-01 +${JulianDay} days -1 day" "+%Y%m%d")
mv $filename "${begin}${newdate}${end}"
This basically splits up the file name pulling out the date field and then grabbing the first 4 digits as the year with the rest being the julian day.
(This assumes the julian day isn't 0 padded, which isn't clear from your question. If it's zero padded, it's much easier).

Resources