0

I have a csv file. I need to change the date format in the data inside the file.

ABCD,1234,01022019

I tried the following:

awk -F ',' '{printf("%s,%s,",$1,$2);system("date -d "$3" +%Y-%m-%d");}' new

I need to change the date format in that file, not to write to another file.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Mahi
  • 1
  • 2

2 Answers2

0

Assuming you mean you want to change the source file so that those dates are of the yyyy-mm-dd format, you can do that with awk on its own:

awk -F, '{$3 = substr($3,5,4)"-"substr($3,3,2)"-"substr($3,1,2) ; print}' inFile >outFile

You can then check to make sure the line count is the same before copying back over the original, something like:

[[ $(wc -l <<inFile) -eq $(wc -l <<outFile) ]] && mv outFile inFile
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Simply arrange the substrings from the date in a different order.

If you have Linux, chances are you have GNU Awk version 4.1 or newer, which has the option inplace so you can write the result back to the original file. (See also Save modifications in place with awk)

awk -F, -i inplace '{ $3 = substr($3, 5) "-" substr($3, 3, 2) "-" substr($3, 1, 2) }1' datadata.csv 
tripleee
  • 175,061
  • 34
  • 275
  • 318