I have the date written in csv file as 19/10/2014.I want it in the format 19-Oct-2014

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have the date in a CSV file as 19/10/2014. I want it in the format 19-Oct-2014. I am new to Unix so I don't know any command.







share|improve this question


















  • 1




    Please post at least one line of your CSV file so that I can adjust the awk script below!
    – Ned64
    Feb 16 at 10:56














up vote
1
down vote

favorite












I have the date in a CSV file as 19/10/2014. I want it in the format 19-Oct-2014. I am new to Unix so I don't know any command.







share|improve this question


















  • 1




    Please post at least one line of your CSV file so that I can adjust the awk script below!
    – Ned64
    Feb 16 at 10:56












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have the date in a CSV file as 19/10/2014. I want it in the format 19-Oct-2014. I am new to Unix so I don't know any command.







share|improve this question














I have the date in a CSV file as 19/10/2014. I want it in the format 19-Oct-2014. I am new to Unix so I don't know any command.









share|improve this question













share|improve this question




share|improve this question








edited Feb 16 at 10:56









yeti

2,36611223




2,36611223










asked Feb 16 at 10:32









jpyngas

91




91







  • 1




    Please post at least one line of your CSV file so that I can adjust the awk script below!
    – Ned64
    Feb 16 at 10:56












  • 1




    Please post at least one line of your CSV file so that I can adjust the awk script below!
    – Ned64
    Feb 16 at 10:56







1




1




Please post at least one line of your CSV file so that I can adjust the awk script below!
– Ned64
Feb 16 at 10:56




Please post at least one line of your CSV file so that I can adjust the awk script below!
– Ned64
Feb 16 at 10:56










5 Answers
5






active

oldest

votes

















up vote
2
down vote













A solution completely in GNU awk is as follows:



awk ' split($0,slt,"/");datspec=mktime(slt[3]" "slt[2]" "slt[1]" 00 00 00");print strftime("%d-%b-%Y",datspec) ' <<< "19/10/2014"


Taking the date as the input, we first split the data into an array (slt) to attain the date,month and year. We then use awk's mktime function to get a date spec that is then used by awk's sttftime function to get the date in the correct format.



With a csv, $0 would be the number corresponding to the comma delimited field that has the date.






share|improve this answer





























    up vote
    2
    down vote













    The date command can format this for you:



    date -d "2014-10-19" +%d-%b-%Y


    which yields



    19-Oct-2014


    If you give sample lines of your file we can script it for you.



    One way is using awk:



    echo zzz,19/10/2014,aaa,bbb | awk -F, ' getline date; OFS=","; $2=date; print; close(cmd)'


    which gives



    zzz,19-Oct-2014,aaa,bbb


    Assuming here that you wish to modifiy the 2nd field. Adjust "split($2,dateelem,"/");" in the awk script to change the field (column) number your date appears in






    share|improve this answer






















    • When processing a large file, you'll need to close the pipe after you read from it: close cmd
      – glenn jackman
      Feb 16 at 14:36










    • @glennjackman Thanks, you are right, editing...
      – Ned64
      Feb 16 at 21:51

















    up vote
    0
    down vote













    perl can do it, but it's not really for beginners. However, parsing dates is best handled by proper date handling libraries. perl has one in the box, Time::Piece



    $ cat file
    a,b,19/10/2014,d

    $ perl -MTime::Piece -F, -lane '
    $F[2] = Time::Piece->strptime($F[2], "%d/%m/%Y")->strftime("%d-%b-%Y");
    print join ",", @F
    ' file
    a,b,19-Oct-2014,d





    share|improve this answer



























      up vote
      0
      down vote













      I have done by below method



      command



       j=`date -d "2014/10/19" +%b`;echo "19/10/2014" | awk -v j="$j" -F "/" 'print $1"-"j"-"$3'


      Output



      19-Oct-2014





      share|improve this answer



























        up vote
        0
        down vote













        Another awk approach. Uses basic awk functionality, so likely to work with non-GNU implementations.



        $ cat infile
        foo,19/10/2014,bar
        foo2,19/01/2015,bar2
        $ awk -F, 'print $1","substr($2,0,2)"-"substr("JanFebMarAprMayJunJulAugSepOctNovDec",substr($2,4,2)*3-2,3)"-"substr($2,7,4)","$3' infile
        foo,19-Oct-2014,bar
        foo2,19-Jan-2015,bar2
        $





        share|improve this answer




















          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424577%2fi-have-the-date-written-in-csv-file-as-19-10-2014-i-want-it-in-the-format-19-oct%23new-answer', 'question_page');

          );

          Post as a guest






























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote













          A solution completely in GNU awk is as follows:



          awk ' split($0,slt,"/");datspec=mktime(slt[3]" "slt[2]" "slt[1]" 00 00 00");print strftime("%d-%b-%Y",datspec) ' <<< "19/10/2014"


          Taking the date as the input, we first split the data into an array (slt) to attain the date,month and year. We then use awk's mktime function to get a date spec that is then used by awk's sttftime function to get the date in the correct format.



          With a csv, $0 would be the number corresponding to the comma delimited field that has the date.






          share|improve this answer


























            up vote
            2
            down vote













            A solution completely in GNU awk is as follows:



            awk ' split($0,slt,"/");datspec=mktime(slt[3]" "slt[2]" "slt[1]" 00 00 00");print strftime("%d-%b-%Y",datspec) ' <<< "19/10/2014"


            Taking the date as the input, we first split the data into an array (slt) to attain the date,month and year. We then use awk's mktime function to get a date spec that is then used by awk's sttftime function to get the date in the correct format.



            With a csv, $0 would be the number corresponding to the comma delimited field that has the date.






            share|improve this answer
























              up vote
              2
              down vote










              up vote
              2
              down vote









              A solution completely in GNU awk is as follows:



              awk ' split($0,slt,"/");datspec=mktime(slt[3]" "slt[2]" "slt[1]" 00 00 00");print strftime("%d-%b-%Y",datspec) ' <<< "19/10/2014"


              Taking the date as the input, we first split the data into an array (slt) to attain the date,month and year. We then use awk's mktime function to get a date spec that is then used by awk's sttftime function to get the date in the correct format.



              With a csv, $0 would be the number corresponding to the comma delimited field that has the date.






              share|improve this answer














              A solution completely in GNU awk is as follows:



              awk ' split($0,slt,"/");datspec=mktime(slt[3]" "slt[2]" "slt[1]" 00 00 00");print strftime("%d-%b-%Y",datspec) ' <<< "19/10/2014"


              Taking the date as the input, we first split the data into an array (slt) to attain the date,month and year. We then use awk's mktime function to get a date spec that is then used by awk's sttftime function to get the date in the correct format.



              With a csv, $0 would be the number corresponding to the comma delimited field that has the date.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 16 at 11:19









              Kusalananda

              103k13202318




              103k13202318










              answered Feb 16 at 11:03









              Raman Sailopal

              1,18317




              1,18317






















                  up vote
                  2
                  down vote













                  The date command can format this for you:



                  date -d "2014-10-19" +%d-%b-%Y


                  which yields



                  19-Oct-2014


                  If you give sample lines of your file we can script it for you.



                  One way is using awk:



                  echo zzz,19/10/2014,aaa,bbb | awk -F, ' getline date; OFS=","; $2=date; print; close(cmd)'


                  which gives



                  zzz,19-Oct-2014,aaa,bbb


                  Assuming here that you wish to modifiy the 2nd field. Adjust "split($2,dateelem,"/");" in the awk script to change the field (column) number your date appears in






                  share|improve this answer






















                  • When processing a large file, you'll need to close the pipe after you read from it: close cmd
                    – glenn jackman
                    Feb 16 at 14:36










                  • @glennjackman Thanks, you are right, editing...
                    – Ned64
                    Feb 16 at 21:51














                  up vote
                  2
                  down vote













                  The date command can format this for you:



                  date -d "2014-10-19" +%d-%b-%Y


                  which yields



                  19-Oct-2014


                  If you give sample lines of your file we can script it for you.



                  One way is using awk:



                  echo zzz,19/10/2014,aaa,bbb | awk -F, ' getline date; OFS=","; $2=date; print; close(cmd)'


                  which gives



                  zzz,19-Oct-2014,aaa,bbb


                  Assuming here that you wish to modifiy the 2nd field. Adjust "split($2,dateelem,"/");" in the awk script to change the field (column) number your date appears in






                  share|improve this answer






















                  • When processing a large file, you'll need to close the pipe after you read from it: close cmd
                    – glenn jackman
                    Feb 16 at 14:36










                  • @glennjackman Thanks, you are right, editing...
                    – Ned64
                    Feb 16 at 21:51












                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  The date command can format this for you:



                  date -d "2014-10-19" +%d-%b-%Y


                  which yields



                  19-Oct-2014


                  If you give sample lines of your file we can script it for you.



                  One way is using awk:



                  echo zzz,19/10/2014,aaa,bbb | awk -F, ' getline date; OFS=","; $2=date; print; close(cmd)'


                  which gives



                  zzz,19-Oct-2014,aaa,bbb


                  Assuming here that you wish to modifiy the 2nd field. Adjust "split($2,dateelem,"/");" in the awk script to change the field (column) number your date appears in






                  share|improve this answer














                  The date command can format this for you:



                  date -d "2014-10-19" +%d-%b-%Y


                  which yields



                  19-Oct-2014


                  If you give sample lines of your file we can script it for you.



                  One way is using awk:



                  echo zzz,19/10/2014,aaa,bbb | awk -F, ' getline date; OFS=","; $2=date; print; close(cmd)'


                  which gives



                  zzz,19-Oct-2014,aaa,bbb


                  Assuming here that you wish to modifiy the 2nd field. Adjust "split($2,dateelem,"/");" in the awk script to change the field (column) number your date appears in







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 16 at 21:55

























                  answered Feb 16 at 10:44









                  Ned64

                  2,44411035




                  2,44411035











                  • When processing a large file, you'll need to close the pipe after you read from it: close cmd
                    – glenn jackman
                    Feb 16 at 14:36










                  • @glennjackman Thanks, you are right, editing...
                    – Ned64
                    Feb 16 at 21:51
















                  • When processing a large file, you'll need to close the pipe after you read from it: close cmd
                    – glenn jackman
                    Feb 16 at 14:36










                  • @glennjackman Thanks, you are right, editing...
                    – Ned64
                    Feb 16 at 21:51















                  When processing a large file, you'll need to close the pipe after you read from it: close cmd
                  – glenn jackman
                  Feb 16 at 14:36




                  When processing a large file, you'll need to close the pipe after you read from it: close cmd
                  – glenn jackman
                  Feb 16 at 14:36












                  @glennjackman Thanks, you are right, editing...
                  – Ned64
                  Feb 16 at 21:51




                  @glennjackman Thanks, you are right, editing...
                  – Ned64
                  Feb 16 at 21:51










                  up vote
                  0
                  down vote













                  perl can do it, but it's not really for beginners. However, parsing dates is best handled by proper date handling libraries. perl has one in the box, Time::Piece



                  $ cat file
                  a,b,19/10/2014,d

                  $ perl -MTime::Piece -F, -lane '
                  $F[2] = Time::Piece->strptime($F[2], "%d/%m/%Y")->strftime("%d-%b-%Y");
                  print join ",", @F
                  ' file
                  a,b,19-Oct-2014,d





                  share|improve this answer
























                    up vote
                    0
                    down vote













                    perl can do it, but it's not really for beginners. However, parsing dates is best handled by proper date handling libraries. perl has one in the box, Time::Piece



                    $ cat file
                    a,b,19/10/2014,d

                    $ perl -MTime::Piece -F, -lane '
                    $F[2] = Time::Piece->strptime($F[2], "%d/%m/%Y")->strftime("%d-%b-%Y");
                    print join ",", @F
                    ' file
                    a,b,19-Oct-2014,d





                    share|improve this answer






















                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      perl can do it, but it's not really for beginners. However, parsing dates is best handled by proper date handling libraries. perl has one in the box, Time::Piece



                      $ cat file
                      a,b,19/10/2014,d

                      $ perl -MTime::Piece -F, -lane '
                      $F[2] = Time::Piece->strptime($F[2], "%d/%m/%Y")->strftime("%d-%b-%Y");
                      print join ",", @F
                      ' file
                      a,b,19-Oct-2014,d





                      share|improve this answer












                      perl can do it, but it's not really for beginners. However, parsing dates is best handled by proper date handling libraries. perl has one in the box, Time::Piece



                      $ cat file
                      a,b,19/10/2014,d

                      $ perl -MTime::Piece -F, -lane '
                      $F[2] = Time::Piece->strptime($F[2], "%d/%m/%Y")->strftime("%d-%b-%Y");
                      print join ",", @F
                      ' file
                      a,b,19-Oct-2014,d






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 16 at 14:31









                      glenn jackman

                      46.3k265102




                      46.3k265102




















                          up vote
                          0
                          down vote













                          I have done by below method



                          command



                           j=`date -d "2014/10/19" +%b`;echo "19/10/2014" | awk -v j="$j" -F "/" 'print $1"-"j"-"$3'


                          Output



                          19-Oct-2014





                          share|improve this answer
























                            up vote
                            0
                            down vote













                            I have done by below method



                            command



                             j=`date -d "2014/10/19" +%b`;echo "19/10/2014" | awk -v j="$j" -F "/" 'print $1"-"j"-"$3'


                            Output



                            19-Oct-2014





                            share|improve this answer






















                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              I have done by below method



                              command



                               j=`date -d "2014/10/19" +%b`;echo "19/10/2014" | awk -v j="$j" -F "/" 'print $1"-"j"-"$3'


                              Output



                              19-Oct-2014





                              share|improve this answer












                              I have done by below method



                              command



                               j=`date -d "2014/10/19" +%b`;echo "19/10/2014" | awk -v j="$j" -F "/" 'print $1"-"j"-"$3'


                              Output



                              19-Oct-2014






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Feb 16 at 15:23









                              Praveen Kumar BS

                              1,010128




                              1,010128




















                                  up vote
                                  0
                                  down vote













                                  Another awk approach. Uses basic awk functionality, so likely to work with non-GNU implementations.



                                  $ cat infile
                                  foo,19/10/2014,bar
                                  foo2,19/01/2015,bar2
                                  $ awk -F, 'print $1","substr($2,0,2)"-"substr("JanFebMarAprMayJunJulAugSepOctNovDec",substr($2,4,2)*3-2,3)"-"substr($2,7,4)","$3' infile
                                  foo,19-Oct-2014,bar
                                  foo2,19-Jan-2015,bar2
                                  $





                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    Another awk approach. Uses basic awk functionality, so likely to work with non-GNU implementations.



                                    $ cat infile
                                    foo,19/10/2014,bar
                                    foo2,19/01/2015,bar2
                                    $ awk -F, 'print $1","substr($2,0,2)"-"substr("JanFebMarAprMayJunJulAugSepOctNovDec",substr($2,4,2)*3-2,3)"-"substr($2,7,4)","$3' infile
                                    foo,19-Oct-2014,bar
                                    foo2,19-Jan-2015,bar2
                                    $





                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      Another awk approach. Uses basic awk functionality, so likely to work with non-GNU implementations.



                                      $ cat infile
                                      foo,19/10/2014,bar
                                      foo2,19/01/2015,bar2
                                      $ awk -F, 'print $1","substr($2,0,2)"-"substr("JanFebMarAprMayJunJulAugSepOctNovDec",substr($2,4,2)*3-2,3)"-"substr($2,7,4)","$3' infile
                                      foo,19-Oct-2014,bar
                                      foo2,19-Jan-2015,bar2
                                      $





                                      share|improve this answer












                                      Another awk approach. Uses basic awk functionality, so likely to work with non-GNU implementations.



                                      $ cat infile
                                      foo,19/10/2014,bar
                                      foo2,19/01/2015,bar2
                                      $ awk -F, 'print $1","substr($2,0,2)"-"substr("JanFebMarAprMayJunJulAugSepOctNovDec",substr($2,4,2)*3-2,3)"-"substr($2,7,4)","$3' infile
                                      foo,19-Oct-2014,bar
                                      foo2,19-Jan-2015,bar2
                                      $






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Feb 16 at 22:07









                                      steve

                                      12.4k22048




                                      12.4k22048






















                                           

                                          draft saved


                                          draft discarded


























                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424577%2fi-have-the-date-written-in-csv-file-as-19-10-2014-i-want-it-in-the-format-19-oct%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Popular posts from this blog

                                          How to check contact read email or not when send email to Individual?

                                          Displaying single band from multi-band raster using QGIS

                                          How many registers does an x86_64 CPU actually have?