Search code examples
linuxshellcsvunixunix-timestamp

How to change date format in .csv file in unix


I want to change the date format to 2013-08-01 and the current format in my file is 8/10/13 and the file contains thousands of records and i have bulk number of files as well.

Please suggest on this, how to convert.

+abcd 2013^8/1/13^Bing^Milwaukee_abcd High Volume^Abcd^izvd4PZo^^Wisconsin^Chicago^http://ad.abcd.net/clk;256581924;80198650;s;u=ms&sv1=izvd4PZo&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyacar.com/specials?srchid=|Milwaukee_cdhdf+High+Volume|_%2Berhefn_2013^Broad^5^1^1.47^
 +sjhrejk models^8/1/13^Bing^Milwaukee_shdh High Volume^fhekj^9NT23JfC^^Wisconsin^Chicago^http://ad.fhd.net/clk;256581924;80198650;s;u=ms&sv1=9NT23JfC&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyhfrkeu.com/specials?srchid=|Milwaukee_djkjfhkd+High+Volume|_%2Bdhk_models^Broad^14^4^5.58^
buy a +jkdsh^8/1/13^Bing^Milwaukee_kjfheu High Volume^jkdfhdj^T0ncFpv4^^Wisconsin^Chicago^http://ad.jfd.net/clk;256581924;80198650;s;u=ms&sv1=T0ncFpv4&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyfjj.com/specials?srchid=|Milwaukee_jefrhjd+High+Volume|buy_a_%2Bfhd^Broad^15^1^2.44^

Thanks !!


Solution

  • You can replace your date using -d to pass the current format and "+..." to indicate the new one:

    $ date -d"8/10/13" "+%Y-%m-%d"
    2013-08-10
    

    To update all contents on your file with this format, keep reading lines, fetching these kind of fields and formatting them:

    while IFS= read -r line
    do
      mydate=$(grep -Po '[0-9]+/[0-9]+/[0-9]+' <<< "$line") # gets 8/1/13
      # perform the replacement in case there is date to process
      if [[ ! -z "$mydate" ]]; then
         newdate=$(date -d"$mydate" "+%Y-%m-%d") # converts to 2013-08-01
         sed -i "s#$mydate#$newdate#" file       # replaces in the text (-i option)
      fi
    done < file
    

    Output based on your input:

    +abcd 2013^2013-08-01^Bing^Milwaukee_abcd High Volume^Abcd^izvd4PZo^^Wisconsin^Chicago^http://ad.abcd.net/clk;256581924;80198650;s;u=ms&sv1=izvd4PZo&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyacar.com/specials?srchid=|Milwaukee_cdhdf+High+Volume|_%2Berhefn_2013^Broad^5^1^1.47^
     +sjhrejk models^2013-08-01^Bing^Milwaukee_shdh High Volume^fhekj^9NT23JfC^^Wisconsin^Chicago^http://ad.fhd.net/clk;256581924;80198650;s;u=ms&sv1=9NT23JfC&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyhfrkeu.com/specials?srchid=|Milwaukee_djkjfhkd+High+Volume|_%2Bdhk_models^Broad^14^4^5.58^
    buy a +jkdsh^2013-08-01^Bing^Milwaukee_kjfheu High Volume^jkdfhdj^T0ncFpv4^^Wisconsin^Chicago^http://ad.jfd.net/clk;256581924;80198650;s;u=ms&sv1=T0ncFpv4&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyfjj.com/specials?srchid=|Milwaukee_jefrhjd+High+Volume|buy_a_%2Bfhd^Broad^15^1^2.44^