Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the date string format

Tags:

bash

sed

awk

Hello Sed/Bash/Awk experts,

I have a file full of dates in the following format:

Feb 5 2015

Nov 25 2014

Apr 16 2015

What I would like is to convert them to this format:

YYYY-MM-DD

So they should look like this:

2015-02-05

2014-11-25

2015-04-16

Thanks for your help.

like image 949
Paul Dawson Avatar asked Feb 05 '26 12:02

Paul Dawson


2 Answers

You can simply use:

date -f dates.txt +%Y-%m-%d

In the -f option you can provide your input file with one date per line.

like image 68
Arton Dorneles Avatar answered Feb 07 '26 18:02

Arton Dorneles


Using awk

awk 'BEGIN{x="  JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"}
     {printf "%04d-%02d-%02d\n",$3,index(x,toupper($1))/3,$2}' file
like image 27
123 Avatar answered Feb 07 '26 16:02

123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!