/bin/sh: How to parse a date string to variables?

I have this string:
Code:
2012-04-10_08-00
How can I parse this to year, month and day?

Why wouldn't something like this work?
Code:
read Y M D h m s <<< ${timestamp//[-_]/ }
 
<<< is a bashism.

you could translate the hyphen to a newline and loop it in to variables if you need them or just send them to their respective output:

Code:
% echo "2012-04-10_08-00" | tr '_-' '\n' 
2012
04
10
08
00
 
Code:
> cat test.sh 

#!/bin/sh
test=`echo '2012-04-10_08-00' | tr '_-' ' '`
set $(echo $test);
echo $1

Code:
> ./test.sh
2012
 
Code:
# date -f "%Y-%m-%d_%H-%M" 2012-04-10_08-00 "+%Y"
2012
# date -f "%Y-%m-%d_%H-%M" 2012-04-10_08-00 "+%B"
April

DATE="2012-04-10_08-00"; MONTH=$(date -f "%Y-%m-%d_%H-%M" $DATE "+%B")
 
Back
Top