Change date format

I know the command date +"%Y%m%d" can change today's date to digit format as below .

$date +"%Y%m%d"
20071217

it works fine .

now I want to do it back . If I have a file like below, (in the file , there are three lines, and each line have ; sign , after the ; sign is the date ) , I want to change the date of the first line to the format Dec 17 , can advise what can i do ? thx

$vi myfile
aaa;20071217
bbb;20071217
ccc;20071254

Here is an example awk script which should help you achieve what you want to do

BEGIN {
    months = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
    split(months, month, ",")
    FS = ";"
}

$2 != "" {
    y = substr($2,1,4)
    m = substr($2,5,2)
    m+= 0
    d = substr($2,7,2)
    d+= 0
    printf "%s %d, %s\n", month[m], d, y
}

If you are looking for something that works like the date command but on arbitrary dates: there is no such thing, at least not to my knowledge.

You can emulate dates behavior, though, fpmurphy has already shown you how. There are equivalent ways to achieve the same in shell script languages.

bakunin

best way is to parse it. if each field after the ; is exactly the same width - ie; first 4 digits are the year, next 2 digits are the month, and last 2 digits are the day, then it will be quite easy for you to convert with awk, or with a cut + date + case.

the last field may pose you issues though: 20071254

Man - December is going to be a long month this year!