I would like to modify the following statement to extract the Month, as well as day:
testdate=$(date -d "2 days" +%d| sed 's/0([1-9])/ \1/')
right now it only extracts the day...
I'm currently googling how sed works. looks like a regular expression of sorts that's being used but... I thought I'd check with the forum for certain.
Thanks.
EDIT 1
now I have:
testdate=$(date -d "2 days" "+%b %_d %Y")
Which is great... except when the leading zeros in the day are stripped out. then i end up with two spaces between the Month and day for example, for April 29, 2014, I get:
Apr 29 2014
which is great. But for May 01, 2014 I get:
May 1, 2014
where there's a double space between "May" and "1". I guess i can do it in two steps... by doing the date command, followed by sed to find and replace double spaces. Just wondering if there's a way to do it all in one command. Thanks.
date
accepts a FORMAT
arg which should be used here. sed
is not necessary:
date +%d.%m.
Outputs:
03.04.
in European time format. If you want the month's name printed use
date +'%d. %B'
Output:
03. April
To read the month and day into shell variables (assuming bash/ksh/zsh)
read month day < <(date -d "2 days" "+%m %d")
If you're planning to do arithmetic on these values, be of numbers that would be treated as octal but are invalid octal numbers 08
and 09
. If you want to strip off the leading zero, use "+%_m %_d"
Using read
, the shell takes care of the excess whitespace for you:
read mon day year < <(date -d "2 days" "+%b %_d %Y")
testdate="$mon $day $year"
If you don't want to use the temp vars:
testdate="Apr 5 2014" # $(date -d "2 days" "+%b %_d %Y")
testdate=${testdate// / } # globally replace 2 spaces with 1 space
echo "$testdate"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With