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!
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!
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