2

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.

0

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This is the only sensible option.
Didn't know about -f - that's an eye-opener!
simple and genius. Thank you!
I'm amazed there's an option for date to handle the case where you have a file of just dates, nothing else. I guess it happens given this question though!
4

Using awk

awk 'BEGIN{x=" JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"} {printf "%04d-%02d-%02d\n",$3,index(x,toupper($1))/3,$2}' file 

1 Comment

Very nice. I was thinking to make a big array but indexing is perfect for this.
2

the date command is your friend here:

date --date="Feb 5 2015" +"%Y-%m-%d" 2015-02-05 

so, you can say:

$ cat my_file | while read -r dt > do > date --date="${dt}" +"%Y-%m-%d" > done 2015-02-05 2014-11-25 2015-04-16 

1 Comment

UUOC and missing IFS= to prevent word splitting and see unix.stackexchange.com/q/169716/133219 for why using a shell loop just to manipulate text is to be avoided.
1

paste the following:

{ month="00"; mon=toupper($1); if(mon=="JAN") month="01"; else if(mon=="FEB") month="02"; else if(mon=="MAR") month="03"; else if(mon=="APR") month="04"; else if(mon=="MAY") month="05"; else if(mon=="JUN") month="06"; else if(mon=="JUL") month="07"; else if(mon=="AUG") month="08"; else if(mon=="SEP") month="09"; else if(mon=="OCT") month="10"; else if(mon=="NOV") month="11"; else if(mon=="DEC") month="12"; printf("%s-%s-%02d\n", $3, month, $2) } 

into a file (We'll refer to the filename as [script_filename] execute the following: awk -F' ' -E [script_filename] [date_filename] Where [date_filename] refers to the file which contains the dates you wish to convert.

5 Comments

fixed month numbers :P
Thanks for the printf gotcha! The -E option is infact the posix short option I tested this on Ubuntu trusty. About the -F' ': I try to be as specific as possible to prevent other unexpected behaviour. The op's example indicates he expects all instance formatted with a single space.
All your months after january are off by one.
Good to know about -E, thanks! Working on OS X as well. Personally, I still wouldn't set FS, although I certainly see your point.
I'd never heard of -E so I just googled it and apparently it's like -f but it disallows other normal awk functionality like assigning variables in the file list. While useful in CGI scripts apparently, -f is the normal way to introduce a script file in all awks. You need to make the 2nd %d in your printf either %s or %2d so the singe digit month numbers are printed as 2 digits.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.