Add month to a variable date in shell script
You seem to be looking for:
date -d "20170601+1 month" +%Y-%m-%d
When using multiple -d
flags in the same command, date
seems to only use the last one.
And of course, feel free to replace 20170601
by $VAR
containing any date.
Adding months using shell script
Well,
date -d "1-$(echo "ABC,XYZ,123,Sep-2018" | awk -F "," '{ print $4 }')+3 months" "+%b-%Y"
(Careful, that code continues past the edge of the box.)
Shows you how to get it working. Just replace the echo
with a shell variable as you loop through the dates.
Basically, you use awk
to grab just the date portion, add a 1-
to the front to turn it into a real date then use the date
command to do the math and then tell it to give you just the month abbreviation and year.
The line above gives just the date portion. The first part can be found using:
stub=`echo "ABC,XYZ,123,Dec-2018" | awk -F "," '{ printf("%s,%s,%s,",$1,$2,$3) }'`
How to increment a date in a Bash script
Use the date
command's ability to add days to existing dates.
The following:
DATE=2013-05-25
for i in {0..8}
do
NEXT_DATE=$(date +%m-%d-%Y -d "$DATE + $i day")
echo "$NEXT_DATE"
done
produces:
05-25-2013
05-26-2013
05-27-2013
05-28-2013
05-29-2013
05-30-2013
05-31-2013
06-01-2013
06-02-2013
Note, this works well in your case but other date formats such as yyyymmdd may need to include "UTC" in the date string (e.g., date -ud "20130515 UTC + 1 day"
).
How to set a variable to current date and date-1 in linux?
You can try:
#!/bin/bash
d=$(date +%Y-%m-%d)
echo "$d"
EDIT: Changed y to Y for 4 digit date as per QuantumFool's comment.
How to subtract or add date from a variable?
With GNU date
it can be done quite easily with its -d
switch.
x=20170402
date -d "$x -1 days" "+%Y%m%d"
20170401
and for 2 days
date -d "$x - 2 days" "+%Y%m%d"
20170331
Using awk to add one month to a date
Awk has limited support for date calculation, so here is a bash only solution relying on the date
command:
IFS='$';
while read n t; do
printf '%s$"%s"\n' "$n" "$(date -d "${t//\"/} +1 month" '+%F %T')"
done <file
The input field separator is set to $
to get the time into $t
variable.
The double quote of the date field are removed using bash parameter expansion ${t//\"/}
.
This allows to pass the +1 month
key word to date
.
Then the printf
prints back to the original format of the input file.
Related Topics
Calling Printf from Assembly Language on 64Bit and 32Bit Architecture Using Nasm
How to Set the Socket Option So_Reuseport in Rust
Shell Script to Write and Read Data from Serial Communication
How Does Gdb Start an Assembly Compiled Program and Step One Line at a Time
Interacting with Files from Multiple Directories via Bash Script
Gitlab Ssh Requests Password and Ignoring Ssh Keys
Reading Gnu-Screen Logs with Vim
Shell Script to Create Empty Files with All Possible Permissions
When Installing Rust Toolchain in Docker, Bash 'Source' Command Doesn't Work
Script Produces Different Result When Executed by Bash Than by Cron
Curl Command Doesn't Work in Bash Script
Linux Split a Column into Two Different Columns in a Same CSV File
Qserialport Cannot Open Tty After Application Has Previously Been Run by 'Root'