Although the date
command is very flexible regarding the format of what it admits in --date
, however it does not admit that you specify your own formats, and the one you use has another meaning for it.
In particular, when you use a pattern of type XXYYYZZ
where XX
, ZZ
are numbers, but YYY
are letters, assume that XX
is the day, ZZ
the year, e YYY
the abbreviation of the name of the month, which does not fit in your case.
If you put only YYYZZ
, then assume that YYY
is the name of the month, and ZZ
the day, giving the year the value of the current year. In your case this would work, at least in the example you have put, since the year (18) coincides with the current year. Therefore, one option would be to eliminate the first two characters of your string. Example (in a shell script):
FECHA=18APR01
date --date=${FECHA:2} +%Y-%m-%d
Result:
2018-04-01
Naturally, if not all of your dates start with 18, this trick does not work. In that case you will not be able to use the date
command, but you can pull out other scripting languages that have a good library for handling dates, like python.
The following is an example of a one-liner that you could include in a shell script, which uses python (ok, maybe it's killing flies with cannon fire) to do the conversion of that date format :
FECHA=15APR01
python -c "from datetime import datetime;print(datetime.strptime(\"$FECHA\", \"%y%b%d\").strftime(\"%Y-%m-%d\"))"
Result:
2015-04-01
To be completed, although I do not think it is useful, if you are in OSX or in BSD, the date
command is different from the one in Linux (which is GNU date), and it does support an option to specify the format of the entry date. In this case you could put:
$ LC_ALL=POSIX date -jf %y%b%d +%Y-%m-%d 18APR01
2018-04-01
(the LC_ALL
is in case your locale were Spanish, in which case you would not understand the name of the month)