replace full stop by comma












0















For the below values I have to replace the second and fourth "." by "," in the below file



input



1.351364711.103.7319660.2010-01-01 00:00:00
1.345529841.103.7372875.2010-01-01 00:00:49
1.342955629.103.7455272.2010-01-01 00:01:42
1.339694956.103.7520503.2010-01-01 00:02:28


desired intermediate output



    1.351364711,103.7319660,2010-01-01 00:00:00
1.345529841,103.7372875,2010-01-01 00:00:49
1.342955629,103.7455272,2010-01-01 00:01:42
1.339694956,103.7520503,2010-01-01 00:02:28


I know awk gsub(/./,",") this replaces everything by comma.But I only need the columns to be seperated by "," . I also wanted to switch the third column in the first place after this.



desired final output



2010-01-01 00:00:00,1.351364711,103.7319660
2010-01-01 00:00:49,1.345529841,103.7372875
2010-01-01 00:01:42,1.342955629,103.7455272
2010-01-01 00:02:28,1.339694956,103.7520503









share|improve this question




















  • 1





    So, the 2nd and 4th periods become commas?

    – Jeff Schaller
    Feb 10 '17 at 2:24











  • Yup exactly the second and fourth becomes commas

    – RKR
    Feb 10 '17 at 2:25








  • 2





    If that's the case, the question should clearly say so.

    – Jeff Schaller
    Feb 10 '17 at 2:26











  • final expected output is not clear, I think you want a space to separate field after time.. awk -F. '{print $5 " " $1 "." $2 "," $3 "." $4}'

    – Sundeep
    Feb 10 '17 at 2:35






  • 1





    @RKR why don't you post a original file contents and the command you used to extract the required fields. so we can offer a slight changes in your command itself.

    – Kamaraj
    Feb 10 '17 at 2:59
















0















For the below values I have to replace the second and fourth "." by "," in the below file



input



1.351364711.103.7319660.2010-01-01 00:00:00
1.345529841.103.7372875.2010-01-01 00:00:49
1.342955629.103.7455272.2010-01-01 00:01:42
1.339694956.103.7520503.2010-01-01 00:02:28


desired intermediate output



    1.351364711,103.7319660,2010-01-01 00:00:00
1.345529841,103.7372875,2010-01-01 00:00:49
1.342955629,103.7455272,2010-01-01 00:01:42
1.339694956,103.7520503,2010-01-01 00:02:28


I know awk gsub(/./,",") this replaces everything by comma.But I only need the columns to be seperated by "," . I also wanted to switch the third column in the first place after this.



desired final output



2010-01-01 00:00:00,1.351364711,103.7319660
2010-01-01 00:00:49,1.345529841,103.7372875
2010-01-01 00:01:42,1.342955629,103.7455272
2010-01-01 00:02:28,1.339694956,103.7520503









share|improve this question




















  • 1





    So, the 2nd and 4th periods become commas?

    – Jeff Schaller
    Feb 10 '17 at 2:24











  • Yup exactly the second and fourth becomes commas

    – RKR
    Feb 10 '17 at 2:25








  • 2





    If that's the case, the question should clearly say so.

    – Jeff Schaller
    Feb 10 '17 at 2:26











  • final expected output is not clear, I think you want a space to separate field after time.. awk -F. '{print $5 " " $1 "." $2 "," $3 "." $4}'

    – Sundeep
    Feb 10 '17 at 2:35






  • 1





    @RKR why don't you post a original file contents and the command you used to extract the required fields. so we can offer a slight changes in your command itself.

    – Kamaraj
    Feb 10 '17 at 2:59














0












0








0








For the below values I have to replace the second and fourth "." by "," in the below file



input



1.351364711.103.7319660.2010-01-01 00:00:00
1.345529841.103.7372875.2010-01-01 00:00:49
1.342955629.103.7455272.2010-01-01 00:01:42
1.339694956.103.7520503.2010-01-01 00:02:28


desired intermediate output



    1.351364711,103.7319660,2010-01-01 00:00:00
1.345529841,103.7372875,2010-01-01 00:00:49
1.342955629,103.7455272,2010-01-01 00:01:42
1.339694956,103.7520503,2010-01-01 00:02:28


I know awk gsub(/./,",") this replaces everything by comma.But I only need the columns to be seperated by "," . I also wanted to switch the third column in the first place after this.



desired final output



2010-01-01 00:00:00,1.351364711,103.7319660
2010-01-01 00:00:49,1.345529841,103.7372875
2010-01-01 00:01:42,1.342955629,103.7455272
2010-01-01 00:02:28,1.339694956,103.7520503









share|improve this question
















For the below values I have to replace the second and fourth "." by "," in the below file



input



1.351364711.103.7319660.2010-01-01 00:00:00
1.345529841.103.7372875.2010-01-01 00:00:49
1.342955629.103.7455272.2010-01-01 00:01:42
1.339694956.103.7520503.2010-01-01 00:02:28


desired intermediate output



    1.351364711,103.7319660,2010-01-01 00:00:00
1.345529841,103.7372875,2010-01-01 00:00:49
1.342955629,103.7455272,2010-01-01 00:01:42
1.339694956,103.7520503,2010-01-01 00:02:28


I know awk gsub(/./,",") this replaces everything by comma.But I only need the columns to be seperated by "," . I also wanted to switch the third column in the first place after this.



desired final output



2010-01-01 00:00:00,1.351364711,103.7319660
2010-01-01 00:00:49,1.345529841,103.7372875
2010-01-01 00:01:42,1.342955629,103.7455272
2010-01-01 00:02:28,1.339694956,103.7520503






linux awk sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 21:35









Rui F Ribeiro

40.2k1479136




40.2k1479136










asked Feb 10 '17 at 2:19









RKRRKR

23219




23219








  • 1





    So, the 2nd and 4th periods become commas?

    – Jeff Schaller
    Feb 10 '17 at 2:24











  • Yup exactly the second and fourth becomes commas

    – RKR
    Feb 10 '17 at 2:25








  • 2





    If that's the case, the question should clearly say so.

    – Jeff Schaller
    Feb 10 '17 at 2:26











  • final expected output is not clear, I think you want a space to separate field after time.. awk -F. '{print $5 " " $1 "." $2 "," $3 "." $4}'

    – Sundeep
    Feb 10 '17 at 2:35






  • 1





    @RKR why don't you post a original file contents and the command you used to extract the required fields. so we can offer a slight changes in your command itself.

    – Kamaraj
    Feb 10 '17 at 2:59














  • 1





    So, the 2nd and 4th periods become commas?

    – Jeff Schaller
    Feb 10 '17 at 2:24











  • Yup exactly the second and fourth becomes commas

    – RKR
    Feb 10 '17 at 2:25








  • 2





    If that's the case, the question should clearly say so.

    – Jeff Schaller
    Feb 10 '17 at 2:26











  • final expected output is not clear, I think you want a space to separate field after time.. awk -F. '{print $5 " " $1 "." $2 "," $3 "." $4}'

    – Sundeep
    Feb 10 '17 at 2:35






  • 1





    @RKR why don't you post a original file contents and the command you used to extract the required fields. so we can offer a slight changes in your command itself.

    – Kamaraj
    Feb 10 '17 at 2:59








1




1





So, the 2nd and 4th periods become commas?

– Jeff Schaller
Feb 10 '17 at 2:24





So, the 2nd and 4th periods become commas?

– Jeff Schaller
Feb 10 '17 at 2:24













Yup exactly the second and fourth becomes commas

– RKR
Feb 10 '17 at 2:25







Yup exactly the second and fourth becomes commas

– RKR
Feb 10 '17 at 2:25






2




2





If that's the case, the question should clearly say so.

– Jeff Schaller
Feb 10 '17 at 2:26





If that's the case, the question should clearly say so.

– Jeff Schaller
Feb 10 '17 at 2:26













final expected output is not clear, I think you want a space to separate field after time.. awk -F. '{print $5 " " $1 "." $2 "," $3 "." $4}'

– Sundeep
Feb 10 '17 at 2:35





final expected output is not clear, I think you want a space to separate field after time.. awk -F. '{print $5 " " $1 "." $2 "," $3 "." $4}'

– Sundeep
Feb 10 '17 at 2:35




1




1





@RKR why don't you post a original file contents and the command you used to extract the required fields. so we can offer a slight changes in your command itself.

– Kamaraj
Feb 10 '17 at 2:59





@RKR why don't you post a original file contents and the command you used to extract the required fields. so we can offer a slight changes in your command itself.

– Kamaraj
Feb 10 '17 at 2:59










2 Answers
2






active

oldest

votes


















2














bash-4.1$ cat file
1.351364711.103.7319660.2010-01-01 00:00:00
1.345529841.103.7372875.2010-01-01 00:00:49
1.342955629.103.7455272.2010-01-01 00:01:42
1.339694956.103.7520503.2010-01-01 00:02:28

bash-4.1$ awk -F. '{print $NF,$1"."$2,$3"."$4}' OFS=, file
2010-01-01 00:00:00,1.351364711,103.7319660
2010-01-01 00:00:49,1.345529841,103.7372875
2010-01-01 00:01:42,1.342955629,103.7455272
2010-01-01 00:02:28,1.339694956,103.7520503





share|improve this answer































    1














    Using sed:



    $ sed 's/./,/4; s/./,/2' file
    1.351364711,103.7319660,2010-01-01 00:00:00
    1.345529841,103.7372875,2010-01-01 00:00:49
    1.342955629,103.7455272,2010-01-01 00:01:42
    1.339694956,103.7520503,2010-01-01 00:02:28


    This first replaces the 4th dot with a comma, then the 2nd.



    We could have done the substitutions in the opposite order too, but since replacing the 2nd dot removes a dot from the line, the dot originally being 4th becomes the 3rd dot:



    $ sed 's/./,/2; s/./,/3' file
    1.351364711,103.7319660,2010-01-01 00:00:00
    1.345529841,103.7372875,2010-01-01 00:00:49
    1.342955629,103.7455272,2010-01-01 00:01:42
    1.339694956,103.7520503,2010-01-01 00:02:28


    The s/.../.../n syntax (where n is a digit) substitutes the n:th matching occurrence on a line.



    Moving the last column to the front may be done with



    s/^(.*),([^,]*)/2,1/


    I.e., match stuff from the start of the line up to a comma, then the comma, then stuff that contains no more commas.



    So the full command may be



    $ sed 's/./,/4; s/./,/2; s/^(.*),([^,]*)/2,1/' file
    2010-01-01 00:00:00,1.351364711,103.7319660
    2010-01-01 00:00:49,1.345529841,103.7372875
    2010-01-01 00:01:42,1.342955629,103.7455272
    2010-01-01 00:02:28,1.339694956,103.7520503





    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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      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%2f343890%2freplace-full-stop-by-comma%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      bash-4.1$ cat file
      1.351364711.103.7319660.2010-01-01 00:00:00
      1.345529841.103.7372875.2010-01-01 00:00:49
      1.342955629.103.7455272.2010-01-01 00:01:42
      1.339694956.103.7520503.2010-01-01 00:02:28

      bash-4.1$ awk -F. '{print $NF,$1"."$2,$3"."$4}' OFS=, file
      2010-01-01 00:00:00,1.351364711,103.7319660
      2010-01-01 00:00:49,1.345529841,103.7372875
      2010-01-01 00:01:42,1.342955629,103.7455272
      2010-01-01 00:02:28,1.339694956,103.7520503





      share|improve this answer




























        2














        bash-4.1$ cat file
        1.351364711.103.7319660.2010-01-01 00:00:00
        1.345529841.103.7372875.2010-01-01 00:00:49
        1.342955629.103.7455272.2010-01-01 00:01:42
        1.339694956.103.7520503.2010-01-01 00:02:28

        bash-4.1$ awk -F. '{print $NF,$1"."$2,$3"."$4}' OFS=, file
        2010-01-01 00:00:00,1.351364711,103.7319660
        2010-01-01 00:00:49,1.345529841,103.7372875
        2010-01-01 00:01:42,1.342955629,103.7455272
        2010-01-01 00:02:28,1.339694956,103.7520503





        share|improve this answer


























          2












          2








          2







          bash-4.1$ cat file
          1.351364711.103.7319660.2010-01-01 00:00:00
          1.345529841.103.7372875.2010-01-01 00:00:49
          1.342955629.103.7455272.2010-01-01 00:01:42
          1.339694956.103.7520503.2010-01-01 00:02:28

          bash-4.1$ awk -F. '{print $NF,$1"."$2,$3"."$4}' OFS=, file
          2010-01-01 00:00:00,1.351364711,103.7319660
          2010-01-01 00:00:49,1.345529841,103.7372875
          2010-01-01 00:01:42,1.342955629,103.7455272
          2010-01-01 00:02:28,1.339694956,103.7520503





          share|improve this answer













          bash-4.1$ cat file
          1.351364711.103.7319660.2010-01-01 00:00:00
          1.345529841.103.7372875.2010-01-01 00:00:49
          1.342955629.103.7455272.2010-01-01 00:01:42
          1.339694956.103.7520503.2010-01-01 00:02:28

          bash-4.1$ awk -F. '{print $NF,$1"."$2,$3"."$4}' OFS=, file
          2010-01-01 00:00:00,1.351364711,103.7319660
          2010-01-01 00:00:49,1.345529841,103.7372875
          2010-01-01 00:01:42,1.342955629,103.7455272
          2010-01-01 00:02:28,1.339694956,103.7520503






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 10 '17 at 3:43









          KamarajKamaraj

          2,9641513




          2,9641513

























              1














              Using sed:



              $ sed 's/./,/4; s/./,/2' file
              1.351364711,103.7319660,2010-01-01 00:00:00
              1.345529841,103.7372875,2010-01-01 00:00:49
              1.342955629,103.7455272,2010-01-01 00:01:42
              1.339694956,103.7520503,2010-01-01 00:02:28


              This first replaces the 4th dot with a comma, then the 2nd.



              We could have done the substitutions in the opposite order too, but since replacing the 2nd dot removes a dot from the line, the dot originally being 4th becomes the 3rd dot:



              $ sed 's/./,/2; s/./,/3' file
              1.351364711,103.7319660,2010-01-01 00:00:00
              1.345529841,103.7372875,2010-01-01 00:00:49
              1.342955629,103.7455272,2010-01-01 00:01:42
              1.339694956,103.7520503,2010-01-01 00:02:28


              The s/.../.../n syntax (where n is a digit) substitutes the n:th matching occurrence on a line.



              Moving the last column to the front may be done with



              s/^(.*),([^,]*)/2,1/


              I.e., match stuff from the start of the line up to a comma, then the comma, then stuff that contains no more commas.



              So the full command may be



              $ sed 's/./,/4; s/./,/2; s/^(.*),([^,]*)/2,1/' file
              2010-01-01 00:00:00,1.351364711,103.7319660
              2010-01-01 00:00:49,1.345529841,103.7372875
              2010-01-01 00:01:42,1.342955629,103.7455272
              2010-01-01 00:02:28,1.339694956,103.7520503





              share|improve this answer






























                1














                Using sed:



                $ sed 's/./,/4; s/./,/2' file
                1.351364711,103.7319660,2010-01-01 00:00:00
                1.345529841,103.7372875,2010-01-01 00:00:49
                1.342955629,103.7455272,2010-01-01 00:01:42
                1.339694956,103.7520503,2010-01-01 00:02:28


                This first replaces the 4th dot with a comma, then the 2nd.



                We could have done the substitutions in the opposite order too, but since replacing the 2nd dot removes a dot from the line, the dot originally being 4th becomes the 3rd dot:



                $ sed 's/./,/2; s/./,/3' file
                1.351364711,103.7319660,2010-01-01 00:00:00
                1.345529841,103.7372875,2010-01-01 00:00:49
                1.342955629,103.7455272,2010-01-01 00:01:42
                1.339694956,103.7520503,2010-01-01 00:02:28


                The s/.../.../n syntax (where n is a digit) substitutes the n:th matching occurrence on a line.



                Moving the last column to the front may be done with



                s/^(.*),([^,]*)/2,1/


                I.e., match stuff from the start of the line up to a comma, then the comma, then stuff that contains no more commas.



                So the full command may be



                $ sed 's/./,/4; s/./,/2; s/^(.*),([^,]*)/2,1/' file
                2010-01-01 00:00:00,1.351364711,103.7319660
                2010-01-01 00:00:49,1.345529841,103.7372875
                2010-01-01 00:01:42,1.342955629,103.7455272
                2010-01-01 00:02:28,1.339694956,103.7520503





                share|improve this answer




























                  1












                  1








                  1







                  Using sed:



                  $ sed 's/./,/4; s/./,/2' file
                  1.351364711,103.7319660,2010-01-01 00:00:00
                  1.345529841,103.7372875,2010-01-01 00:00:49
                  1.342955629,103.7455272,2010-01-01 00:01:42
                  1.339694956,103.7520503,2010-01-01 00:02:28


                  This first replaces the 4th dot with a comma, then the 2nd.



                  We could have done the substitutions in the opposite order too, but since replacing the 2nd dot removes a dot from the line, the dot originally being 4th becomes the 3rd dot:



                  $ sed 's/./,/2; s/./,/3' file
                  1.351364711,103.7319660,2010-01-01 00:00:00
                  1.345529841,103.7372875,2010-01-01 00:00:49
                  1.342955629,103.7455272,2010-01-01 00:01:42
                  1.339694956,103.7520503,2010-01-01 00:02:28


                  The s/.../.../n syntax (where n is a digit) substitutes the n:th matching occurrence on a line.



                  Moving the last column to the front may be done with



                  s/^(.*),([^,]*)/2,1/


                  I.e., match stuff from the start of the line up to a comma, then the comma, then stuff that contains no more commas.



                  So the full command may be



                  $ sed 's/./,/4; s/./,/2; s/^(.*),([^,]*)/2,1/' file
                  2010-01-01 00:00:00,1.351364711,103.7319660
                  2010-01-01 00:00:49,1.345529841,103.7372875
                  2010-01-01 00:01:42,1.342955629,103.7455272
                  2010-01-01 00:02:28,1.339694956,103.7520503





                  share|improve this answer















                  Using sed:



                  $ sed 's/./,/4; s/./,/2' file
                  1.351364711,103.7319660,2010-01-01 00:00:00
                  1.345529841,103.7372875,2010-01-01 00:00:49
                  1.342955629,103.7455272,2010-01-01 00:01:42
                  1.339694956,103.7520503,2010-01-01 00:02:28


                  This first replaces the 4th dot with a comma, then the 2nd.



                  We could have done the substitutions in the opposite order too, but since replacing the 2nd dot removes a dot from the line, the dot originally being 4th becomes the 3rd dot:



                  $ sed 's/./,/2; s/./,/3' file
                  1.351364711,103.7319660,2010-01-01 00:00:00
                  1.345529841,103.7372875,2010-01-01 00:00:49
                  1.342955629,103.7455272,2010-01-01 00:01:42
                  1.339694956,103.7520503,2010-01-01 00:02:28


                  The s/.../.../n syntax (where n is a digit) substitutes the n:th matching occurrence on a line.



                  Moving the last column to the front may be done with



                  s/^(.*),([^,]*)/2,1/


                  I.e., match stuff from the start of the line up to a comma, then the comma, then stuff that contains no more commas.



                  So the full command may be



                  $ sed 's/./,/4; s/./,/2; s/^(.*),([^,]*)/2,1/' file
                  2010-01-01 00:00:00,1.351364711,103.7319660
                  2010-01-01 00:00:49,1.345529841,103.7372875
                  2010-01-01 00:01:42,1.342955629,103.7455272
                  2010-01-01 00:02:28,1.339694956,103.7520503






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 3 at 20:13

























                  answered Feb 3 at 20:07









                  KusalanandaKusalananda

                  130k17246406




                  130k17246406






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f343890%2freplace-full-stop-by-comma%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                      is 'sed' thread safe

                      How to make a Squid Proxy server?