-2

My file looks like this:

ID,DATE,NAME
1,10-17-18,John
2,10-12-18,Mary
3,10-19-18,David

And I want it to look like this:

1,2018-10-17,John
2,2018-10-12,Mary
3,2018-10-19,David

How can I do that in bash?

Thanks!

  • 1
    https://stackoverflow.com/a/6508849/9799449 –  Oct 25 '18 at 15:24
  • 2
    Did you make any attempt? – anubhava Oct 25 '18 at 15:54
  • I need a solution that doesn't modify line by line because I have over 90k lines. I need something to modify the entire column. – Robert Roland Oct 25 '18 at 15:57
  • Sounds like a homework question... [How do I ask homework questions on Stack Overflow](https://www.google.com/search?q=how+do+I+ask+homework+questions+on+Stack+Overflow). You are expected to make an effort. – jww Oct 25 '18 at 19:07

1 Answers1

-1

Delimit by ',' -F','.
Skip the first line (NR>1).
Replace - in date string with /, gsub(/-/,"/",$2) so date will recognize the format as month/day/year.
Convert date format, save as variable d ("date +%Y-%m-%d -d"$2 | getline d). Then print each field separated by comma.

awk -F',' '(NR>1){gsub(/-/,"/",$2); ("date +%Y-%m-%d -d"$2 | getline d); print $1","d","$3}' file

To skip lines with blank dates, add if($2=="") next; at the start, like this:

awk -F',' '(NR>1){if($2=="") next; gsub(/-/,"/",$2); ("date +%Y-%m-%d -d"$2 | getline d); print $1","d","$3}' file
flu
  • 546
  • 4
  • 11