Reputation: 15
I have a date field from a file with 50 dates in mm/dd/yy format. How can I convert it to yymmdd? I have seen questions similar, but going the opposite direction. I cant seem to apply it the way I need it.
Dates are saved in file as 01/20/72 and I need to convert them to 720120
Currently I have $bDate +%y%m%d as the command, but it is wrong. Thanks in advance!!
Upvotes: 0
Views: 1017
Reputation: 955
you can use sed
and its internal regexp storing vars like
echo "01/20/72" | sed -r 's#(..)/(..)/(..)#\3\1\2#g'
Upvotes: 0
Reputation: 247042
string manipulation: bash regular expressions suffice here:
date="04/13/06"
d='[[:digit:]]'
if [[ $date =~ ($d$d)/($d$d)/($d$d) ]]; then
newdate=${BASH_REMATCH[3]}${BASH_REMATCH[1]}${BASH_REMATCH[2]}
fi
echo $newdate
060413
If you're repeatedly doing this, make it a function:
mdy2ymd() {
local d='[[:digit:]]' newdate
if [[ $1 =~ ($d$d)/($d$d)/($d$d) ]]; then
newdate=${BASH_REMATCH[3]}${BASH_REMATCH[1]}${BASH_REMATCH[2]}
fi
echo $newdate
}
Upvotes: 1