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



## olav (Apr 10, 2012)

I have this string:

```
2012-04-10_08-00
```
How can I parse this to year, month and day?

Why wouldn't something like this work?

```
read Y M D h m s <<< ${timestamp//[-_]/ }
```


----------



## UNIXgod (Apr 10, 2012)

<<< 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:


```
% echo "2012-04-10_08-00" | tr '_-' '\n' 
2012
04
10
08
00
```


----------



## Shamrock (Apr 10, 2012)

```
> cat test.sh 

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


```
> ./test.sh
2012
```


----------



## varda (Apr 10, 2012)

```
# 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")
```


----------



## olav (Apr 10, 2012)

*T*hanks, I finally got this to work


----------

