Frage

I have some date other than current date in unix and I want to convert into a specific format

Original Format
D="Mon Dec 30 06:35:02 EST 2013"

New Format
E=20131230063502
War es hilfreich?

Lösung

E=`date +%Y%m%d%H%M%S`

this is the way to format the output of the date command and save it in the variable E

Andere Tipps

Using python:

def data(dstr):
    m = {'Jan': '01', 'Feb':'02', 'Mar':'03', 'Apr':'04', 'May':'05', 'Jun':'06', 'Jul':'07', 'Aug':'08', 'Sep':'09', 'Oct':'10', 'Nov':'11', 'Dec':'12'}
    val = dstr.split(' ')
    month = m[val[1]]
    time = val[3].split(':')
    return '{}{}{}{}{}{}'.format(val[-1],month,val[2],time[0],time[1],time[2])

if __name__ == '__main__':
    print data("Mon Dec 30 06:35:02 EST 2013")

In: Mon Dec 30 06:35:02 EST 2013

Out: 20131230063502

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top