Replace four times with sed2019 Community Moderator Electionhow to substitute some lines of one file according to the line's contentsed: Multiple -e or while loop?Have trouble with sed command on LinuxReplace matching parentheses with enclosing contentIs there a another way to display a part of a line using sedsed command to replace a word from one file with entire line of another fileDelete Substring from JSON file (malformed)How to change the line a string is found on with the output of another program to which you pass that string as an argument?What's the purpose of adding a prefix on both sides of a shell variable comparison to a string literal?How to improve this 'sed' search & replace command?

Deletion of copy-ctor & copy-assignment - public, private or protected?

Optimising a list searching algorithm

What exactly term 'companion plants' means?

Unfrosted light bulb

What are substitutions for coconut in curry?

Print last inputted byte

Does multi-classing into Fighter give you heavy armor proficiency?

What does "^L" mean in C?

How could an airship be repaired midflight?

Suggestions on how to spend Shaabath (constructively) alone

Could Sinn Fein swing any Brexit vote in Parliament?

How are passwords stolen from companies if they only store hashes?

Synchronized implementation of a bank account in Java

Brake pads destroying wheels

Pronounciation of the combination "st" in spanish accents

Recruiter wants very extensive technical details about all of my previous work

How do hiring committees for research positions view getting "scooped"?

Relation between independence and correlation of uniform random variables

Bash - pair each line of file

Worshiping one God at a time?

Can you move over difficult terrain with only 5 feet of movement?

Do US professors/group leaders only get a salary, but no group budget?

How to generate binary array whose elements with values 1 are randomly drawn

What are idioms that are antonymous to "don't skimp on"?



Replace four times with sed



2019 Community Moderator Electionhow to substitute some lines of one file according to the line's contentsed: Multiple -e or while loop?Have trouble with sed command on LinuxReplace matching parentheses with enclosing contentIs there a another way to display a part of a line using sedsed command to replace a word from one file with entire line of another fileDelete Substring from JSON file (malformed)How to change the line a string is found on with the output of another program to which you pass that string as an argument?What's the purpose of adding a prefix on both sides of a shell variable comparison to a string literal?How to improve this 'sed' search & replace command?










3















I want to replace some commas in this string



2019-03-17T11:32:28.143343Z;1234.5678;901.234;567.89012;3456.78;192.168.0.1


to this



2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1


In words:
replace the second, third, fourth and fifth dot with a comma. The first and sixth should stay the same.



I found this command, which I could execute multiple times (but maybe not the best practise):



echo $tmp | sed 's/./,/2'


How can I get this done in one command?










share|improve this question









New contributor




fty4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • The question is framed improperly. 2nd to 5th dots to comma, but why 1st and 6th mentioned when there are other dots as well which don't change.

    – Rakesh Sharma
    6 hours ago















3















I want to replace some commas in this string



2019-03-17T11:32:28.143343Z;1234.5678;901.234;567.89012;3456.78;192.168.0.1


to this



2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1


In words:
replace the second, third, fourth and fifth dot with a comma. The first and sixth should stay the same.



I found this command, which I could execute multiple times (but maybe not the best practise):



echo $tmp | sed 's/./,/2'


How can I get this done in one command?










share|improve this question









New contributor




fty4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • The question is framed improperly. 2nd to 5th dots to comma, but why 1st and 6th mentioned when there are other dots as well which don't change.

    – Rakesh Sharma
    6 hours ago













3












3








3








I want to replace some commas in this string



2019-03-17T11:32:28.143343Z;1234.5678;901.234;567.89012;3456.78;192.168.0.1


to this



2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1


In words:
replace the second, third, fourth and fifth dot with a comma. The first and sixth should stay the same.



I found this command, which I could execute multiple times (but maybe not the best practise):



echo $tmp | sed 's/./,/2'


How can I get this done in one command?










share|improve this question









New contributor




fty4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I want to replace some commas in this string



2019-03-17T11:32:28.143343Z;1234.5678;901.234;567.89012;3456.78;192.168.0.1


to this



2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1


In words:
replace the second, third, fourth and fifth dot with a comma. The first and sixth should stay the same.



I found this command, which I could execute multiple times (but maybe not the best practise):



echo $tmp | sed 's/./,/2'


How can I get this done in one command?







text-processing sed string






share|improve this question









New contributor




fty4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




fty4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 11 hours ago









Kusalananda

136k17256424




136k17256424






New contributor




fty4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 12 hours ago









fty4fty4

184




184




New contributor




fty4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





fty4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






fty4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • The question is framed improperly. 2nd to 5th dots to comma, but why 1st and 6th mentioned when there are other dots as well which don't change.

    – Rakesh Sharma
    6 hours ago

















  • The question is framed improperly. 2nd to 5th dots to comma, but why 1st and 6th mentioned when there are other dots as well which don't change.

    – Rakesh Sharma
    6 hours ago
















The question is framed improperly. 2nd to 5th dots to comma, but why 1st and 6th mentioned when there are other dots as well which don't change.

– Rakesh Sharma
6 hours ago





The question is framed improperly. 2nd to 5th dots to comma, but why 1st and 6th mentioned when there are other dots as well which don't change.

– Rakesh Sharma
6 hours ago










4 Answers
4






active

oldest

votes


















6














Your data consists of six ;-delimited fields, and you'd like to replace the dots in fields 2 through to 5 (not 1 or 6) with commas.



This is easiest done with awk:



awk -F ';' 'BEGIN OFS=FS for (i=2; i<=5; ++i) gsub("\.", ",", $i); print ' file


With the example data given, this produces



2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1


The code simply iterates of the ;-delimited fields of each input line and calls gsub() to do a global search and replace (as you would do with s/./,/g or y/./,/ in sed) on the individual fields that the loop iterates over.



The modified line is then printed.



The -F option sets the input field separator to a semicolon, and we use the BEGIN block to also set the output field separator to the same value (you would otherwise get space-separated fields).




Using sed, you might do something like



sed 's/./,/2; s/./,/2; s/./,/2; s/./,/2' file


I.e., replace the 2nd dot four times (which one is the 2nd dot will change with each substitution, since you substitute them). This does however assume that the number of values within each field remains static.



To work around this in case you at some point have more than two dot-delimited things in a field, you can do



sed 'h; s/^[^;]*;//; s/;[^;]*$//; y/./,/; G;H;x; s/;[^n]*n/;/; s/n.*;/;/' file


In short, these commands do



  1. Copy the original line to the hold space.

  2. Remove the first and last fields in the pattern space.

  3. Change all dots to commas in the pattern space (that's the y command). All dots that should change into commas have now been changed. Now we must reassemble the line from the middle bit in the pattern space and the original data in the hold space.


  4. Make (with G;H;x) the pattern space contain



    1. The original string, followed by a newline,

    2. The modified middle bit, followed by a newline

    3. The original string again.


  5. So now the pattern space contains three lines. Remove everything but the first field on the first line, and the newline, and replace that removed bit with a ;.


  6. Do a similar thing with the last line, i.e. remove the (now lone) newline and everything up to the last ;, and replace with a ;.


  7. Done.


Or you could just use the awk code.






share|improve this answer
































    1














    Since the other answers are making assumptions about the input
    that are not stated in the question
    (e.g., that it is a bunch of ;-separated values,
    or that there are exactly six dots),
    I’ll provide this slightly clunky answer
    that does what the question asks for:



    sed 's/^([^.]*.[^.]*).([^.]*).([^.]*).([^.]*)./1,2,3,4,/'


    This breaks down each input line as follows:



    • Capture group 1: Starting at the beginning of the line,
      any number of characters other than .,
      then one . (the first one in the line),
      then another arbitrarily long sequence of characters other than .,

    • A . (the second one in the line),

    • Capture group 2: Any number of characters other than .,

    • A . (the third one in the line),

    • Capture group 3: Any number of characters other than .,

    • A . (the fourth one in the line),

    • Capture group 4: Any number of characters other than .,

    • A . (the fifth one in the line),

    • Whatever follows (not matched by the regular expression,
      but there can be more to the line than the above,
      because the regex doesn’t end with $).

    And replaces it with



    • Capture group 1: Everything up to the second . in the line
      (including the first one),

    • A , (replacing the second .),

    • Capture group 2: Everything between the second . and the third one,

    • A , (replacing the third .),

    • Capture group 3: Everything between the third . and the fourth one,

    • A , (replacing the fourth .),

    • Capture group 4: Everything between the fourth . and the fifth one,

    • A , (replacing the fifth .),

    • Whatever follows the fifth ..

    So it replaces the second, third, fourth and fifth dots with commas.



    • This will make no changes on a line with fewer than five dots.

    • This will leave an arbitrary number of dots after the fifth unchanged.

    • This will replace the second, third, fourth and fifth dots,
      even if there are only five dots in the line (i.e., there is no sixth one).





    share|improve this answer






























      -1














      Another sed with a loop :



      sed ':A;s/([^.]*.[^.]*).(.*;[^;]*$)/1,2/;tA' infile





      share|improve this answer


















      • 1





        -1 for no explanation and for not answering the question as asked.

        – G-Man
        10 hours ago











      • @G-Man : Thanks for downvote : I don't look for reputation. I'm here to learn and try to help other. no explanation ? I say it is a loop, no? but you forgot to tell why not answering the question ! So -1 for my answer, It is a bad answer. Someone come here don't look at that but I get the espected result, no? As you can see, a lot of time there is no feedback. Askers got answer and go away. A lot of time, they are not interested by explanation. If they are, they ask for explanation via comment.

        – ctac_
        6 hours ago











      • @G-Man : An other thing, all here don't speak english very well, like me. It's more difficult for them first to understand the question and second to answer with explanation. Sometime they get downvote because explanation are not clear enough.

        – ctac_
        6 hours ago











      • The question said “I want to replace … the second, third, fourth and fifth dot [in this string] with a comma.”  If you take the question super-literally, you could say that echo "2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1" was an answer — but that would be ridiculous.  But you assumed that the input would be exactly like the example — that it would contain some semicolons — and you posted an answer that depends on that assumption.  Based on what the question says, any answer should convert a.b.c.d.e.f.g into a.b,c,d,e,f.g, and yours doesn’t.

        – G-Man
        6 hours ago











      • @G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.

        – ctac_
        5 hours ago


















      -1














      You could approach this using sed editor as follows:



      $ sed -e '
      y/./n/
      s/n(.*)n/.1./
      y/n/,/
      ' input.txt


      The premise being that we first turn all dots into newlines, a char guaranteed to not be there in the pattern space.
      Then we change the last and the first newlines back into dots.
      The remaining newlines all get converted to commas.



      HTH.






      share|improve this answer


















      • 1





        That's rather neat, but it does seem to change some dots in the IP number at the end.

        – Kusalananda
        6 hours ago












      • Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.

        – Rakesh Sharma
        6 hours ago










      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
      );



      );






      fty4 is a new contributor. Be nice, and check out our Code of Conduct.









      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506807%2freplace-four-times-with-sed%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      6














      Your data consists of six ;-delimited fields, and you'd like to replace the dots in fields 2 through to 5 (not 1 or 6) with commas.



      This is easiest done with awk:



      awk -F ';' 'BEGIN OFS=FS for (i=2; i<=5; ++i) gsub("\.", ",", $i); print ' file


      With the example data given, this produces



      2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1


      The code simply iterates of the ;-delimited fields of each input line and calls gsub() to do a global search and replace (as you would do with s/./,/g or y/./,/ in sed) on the individual fields that the loop iterates over.



      The modified line is then printed.



      The -F option sets the input field separator to a semicolon, and we use the BEGIN block to also set the output field separator to the same value (you would otherwise get space-separated fields).




      Using sed, you might do something like



      sed 's/./,/2; s/./,/2; s/./,/2; s/./,/2' file


      I.e., replace the 2nd dot four times (which one is the 2nd dot will change with each substitution, since you substitute them). This does however assume that the number of values within each field remains static.



      To work around this in case you at some point have more than two dot-delimited things in a field, you can do



      sed 'h; s/^[^;]*;//; s/;[^;]*$//; y/./,/; G;H;x; s/;[^n]*n/;/; s/n.*;/;/' file


      In short, these commands do



      1. Copy the original line to the hold space.

      2. Remove the first and last fields in the pattern space.

      3. Change all dots to commas in the pattern space (that's the y command). All dots that should change into commas have now been changed. Now we must reassemble the line from the middle bit in the pattern space and the original data in the hold space.


      4. Make (with G;H;x) the pattern space contain



        1. The original string, followed by a newline,

        2. The modified middle bit, followed by a newline

        3. The original string again.


      5. So now the pattern space contains three lines. Remove everything but the first field on the first line, and the newline, and replace that removed bit with a ;.


      6. Do a similar thing with the last line, i.e. remove the (now lone) newline and everything up to the last ;, and replace with a ;.


      7. Done.


      Or you could just use the awk code.






      share|improve this answer





























        6














        Your data consists of six ;-delimited fields, and you'd like to replace the dots in fields 2 through to 5 (not 1 or 6) with commas.



        This is easiest done with awk:



        awk -F ';' 'BEGIN OFS=FS for (i=2; i<=5; ++i) gsub("\.", ",", $i); print ' file


        With the example data given, this produces



        2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1


        The code simply iterates of the ;-delimited fields of each input line and calls gsub() to do a global search and replace (as you would do with s/./,/g or y/./,/ in sed) on the individual fields that the loop iterates over.



        The modified line is then printed.



        The -F option sets the input field separator to a semicolon, and we use the BEGIN block to also set the output field separator to the same value (you would otherwise get space-separated fields).




        Using sed, you might do something like



        sed 's/./,/2; s/./,/2; s/./,/2; s/./,/2' file


        I.e., replace the 2nd dot four times (which one is the 2nd dot will change with each substitution, since you substitute them). This does however assume that the number of values within each field remains static.



        To work around this in case you at some point have more than two dot-delimited things in a field, you can do



        sed 'h; s/^[^;]*;//; s/;[^;]*$//; y/./,/; G;H;x; s/;[^n]*n/;/; s/n.*;/;/' file


        In short, these commands do



        1. Copy the original line to the hold space.

        2. Remove the first and last fields in the pattern space.

        3. Change all dots to commas in the pattern space (that's the y command). All dots that should change into commas have now been changed. Now we must reassemble the line from the middle bit in the pattern space and the original data in the hold space.


        4. Make (with G;H;x) the pattern space contain



          1. The original string, followed by a newline,

          2. The modified middle bit, followed by a newline

          3. The original string again.


        5. So now the pattern space contains three lines. Remove everything but the first field on the first line, and the newline, and replace that removed bit with a ;.


        6. Do a similar thing with the last line, i.e. remove the (now lone) newline and everything up to the last ;, and replace with a ;.


        7. Done.


        Or you could just use the awk code.






        share|improve this answer



























          6












          6








          6







          Your data consists of six ;-delimited fields, and you'd like to replace the dots in fields 2 through to 5 (not 1 or 6) with commas.



          This is easiest done with awk:



          awk -F ';' 'BEGIN OFS=FS for (i=2; i<=5; ++i) gsub("\.", ",", $i); print ' file


          With the example data given, this produces



          2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1


          The code simply iterates of the ;-delimited fields of each input line and calls gsub() to do a global search and replace (as you would do with s/./,/g or y/./,/ in sed) on the individual fields that the loop iterates over.



          The modified line is then printed.



          The -F option sets the input field separator to a semicolon, and we use the BEGIN block to also set the output field separator to the same value (you would otherwise get space-separated fields).




          Using sed, you might do something like



          sed 's/./,/2; s/./,/2; s/./,/2; s/./,/2' file


          I.e., replace the 2nd dot four times (which one is the 2nd dot will change with each substitution, since you substitute them). This does however assume that the number of values within each field remains static.



          To work around this in case you at some point have more than two dot-delimited things in a field, you can do



          sed 'h; s/^[^;]*;//; s/;[^;]*$//; y/./,/; G;H;x; s/;[^n]*n/;/; s/n.*;/;/' file


          In short, these commands do



          1. Copy the original line to the hold space.

          2. Remove the first and last fields in the pattern space.

          3. Change all dots to commas in the pattern space (that's the y command). All dots that should change into commas have now been changed. Now we must reassemble the line from the middle bit in the pattern space and the original data in the hold space.


          4. Make (with G;H;x) the pattern space contain



            1. The original string, followed by a newline,

            2. The modified middle bit, followed by a newline

            3. The original string again.


          5. So now the pattern space contains three lines. Remove everything but the first field on the first line, and the newline, and replace that removed bit with a ;.


          6. Do a similar thing with the last line, i.e. remove the (now lone) newline and everything up to the last ;, and replace with a ;.


          7. Done.


          Or you could just use the awk code.






          share|improve this answer















          Your data consists of six ;-delimited fields, and you'd like to replace the dots in fields 2 through to 5 (not 1 or 6) with commas.



          This is easiest done with awk:



          awk -F ';' 'BEGIN OFS=FS for (i=2; i<=5; ++i) gsub("\.", ",", $i); print ' file


          With the example data given, this produces



          2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1


          The code simply iterates of the ;-delimited fields of each input line and calls gsub() to do a global search and replace (as you would do with s/./,/g or y/./,/ in sed) on the individual fields that the loop iterates over.



          The modified line is then printed.



          The -F option sets the input field separator to a semicolon, and we use the BEGIN block to also set the output field separator to the same value (you would otherwise get space-separated fields).




          Using sed, you might do something like



          sed 's/./,/2; s/./,/2; s/./,/2; s/./,/2' file


          I.e., replace the 2nd dot four times (which one is the 2nd dot will change with each substitution, since you substitute them). This does however assume that the number of values within each field remains static.



          To work around this in case you at some point have more than two dot-delimited things in a field, you can do



          sed 'h; s/^[^;]*;//; s/;[^;]*$//; y/./,/; G;H;x; s/;[^n]*n/;/; s/n.*;/;/' file


          In short, these commands do



          1. Copy the original line to the hold space.

          2. Remove the first and last fields in the pattern space.

          3. Change all dots to commas in the pattern space (that's the y command). All dots that should change into commas have now been changed. Now we must reassemble the line from the middle bit in the pattern space and the original data in the hold space.


          4. Make (with G;H;x) the pattern space contain



            1. The original string, followed by a newline,

            2. The modified middle bit, followed by a newline

            3. The original string again.


          5. So now the pattern space contains three lines. Remove everything but the first field on the first line, and the newline, and replace that removed bit with a ;.


          6. Do a similar thing with the last line, i.e. remove the (now lone) newline and everything up to the last ;, and replace with a ;.


          7. Done.


          Or you could just use the awk code.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 11 hours ago

























          answered 11 hours ago









          KusalanandaKusalananda

          136k17256424




          136k17256424























              1














              Since the other answers are making assumptions about the input
              that are not stated in the question
              (e.g., that it is a bunch of ;-separated values,
              or that there are exactly six dots),
              I’ll provide this slightly clunky answer
              that does what the question asks for:



              sed 's/^([^.]*.[^.]*).([^.]*).([^.]*).([^.]*)./1,2,3,4,/'


              This breaks down each input line as follows:



              • Capture group 1: Starting at the beginning of the line,
                any number of characters other than .,
                then one . (the first one in the line),
                then another arbitrarily long sequence of characters other than .,

              • A . (the second one in the line),

              • Capture group 2: Any number of characters other than .,

              • A . (the third one in the line),

              • Capture group 3: Any number of characters other than .,

              • A . (the fourth one in the line),

              • Capture group 4: Any number of characters other than .,

              • A . (the fifth one in the line),

              • Whatever follows (not matched by the regular expression,
                but there can be more to the line than the above,
                because the regex doesn’t end with $).

              And replaces it with



              • Capture group 1: Everything up to the second . in the line
                (including the first one),

              • A , (replacing the second .),

              • Capture group 2: Everything between the second . and the third one,

              • A , (replacing the third .),

              • Capture group 3: Everything between the third . and the fourth one,

              • A , (replacing the fourth .),

              • Capture group 4: Everything between the fourth . and the fifth one,

              • A , (replacing the fifth .),

              • Whatever follows the fifth ..

              So it replaces the second, third, fourth and fifth dots with commas.



              • This will make no changes on a line with fewer than five dots.

              • This will leave an arbitrary number of dots after the fifth unchanged.

              • This will replace the second, third, fourth and fifth dots,
                even if there are only five dots in the line (i.e., there is no sixth one).





              share|improve this answer



























                1














                Since the other answers are making assumptions about the input
                that are not stated in the question
                (e.g., that it is a bunch of ;-separated values,
                or that there are exactly six dots),
                I’ll provide this slightly clunky answer
                that does what the question asks for:



                sed 's/^([^.]*.[^.]*).([^.]*).([^.]*).([^.]*)./1,2,3,4,/'


                This breaks down each input line as follows:



                • Capture group 1: Starting at the beginning of the line,
                  any number of characters other than .,
                  then one . (the first one in the line),
                  then another arbitrarily long sequence of characters other than .,

                • A . (the second one in the line),

                • Capture group 2: Any number of characters other than .,

                • A . (the third one in the line),

                • Capture group 3: Any number of characters other than .,

                • A . (the fourth one in the line),

                • Capture group 4: Any number of characters other than .,

                • A . (the fifth one in the line),

                • Whatever follows (not matched by the regular expression,
                  but there can be more to the line than the above,
                  because the regex doesn’t end with $).

                And replaces it with



                • Capture group 1: Everything up to the second . in the line
                  (including the first one),

                • A , (replacing the second .),

                • Capture group 2: Everything between the second . and the third one,

                • A , (replacing the third .),

                • Capture group 3: Everything between the third . and the fourth one,

                • A , (replacing the fourth .),

                • Capture group 4: Everything between the fourth . and the fifth one,

                • A , (replacing the fifth .),

                • Whatever follows the fifth ..

                So it replaces the second, third, fourth and fifth dots with commas.



                • This will make no changes on a line with fewer than five dots.

                • This will leave an arbitrary number of dots after the fifth unchanged.

                • This will replace the second, third, fourth and fifth dots,
                  even if there are only five dots in the line (i.e., there is no sixth one).





                share|improve this answer

























                  1












                  1








                  1







                  Since the other answers are making assumptions about the input
                  that are not stated in the question
                  (e.g., that it is a bunch of ;-separated values,
                  or that there are exactly six dots),
                  I’ll provide this slightly clunky answer
                  that does what the question asks for:



                  sed 's/^([^.]*.[^.]*).([^.]*).([^.]*).([^.]*)./1,2,3,4,/'


                  This breaks down each input line as follows:



                  • Capture group 1: Starting at the beginning of the line,
                    any number of characters other than .,
                    then one . (the first one in the line),
                    then another arbitrarily long sequence of characters other than .,

                  • A . (the second one in the line),

                  • Capture group 2: Any number of characters other than .,

                  • A . (the third one in the line),

                  • Capture group 3: Any number of characters other than .,

                  • A . (the fourth one in the line),

                  • Capture group 4: Any number of characters other than .,

                  • A . (the fifth one in the line),

                  • Whatever follows (not matched by the regular expression,
                    but there can be more to the line than the above,
                    because the regex doesn’t end with $).

                  And replaces it with



                  • Capture group 1: Everything up to the second . in the line
                    (including the first one),

                  • A , (replacing the second .),

                  • Capture group 2: Everything between the second . and the third one,

                  • A , (replacing the third .),

                  • Capture group 3: Everything between the third . and the fourth one,

                  • A , (replacing the fourth .),

                  • Capture group 4: Everything between the fourth . and the fifth one,

                  • A , (replacing the fifth .),

                  • Whatever follows the fifth ..

                  So it replaces the second, third, fourth and fifth dots with commas.



                  • This will make no changes on a line with fewer than five dots.

                  • This will leave an arbitrary number of dots after the fifth unchanged.

                  • This will replace the second, third, fourth and fifth dots,
                    even if there are only five dots in the line (i.e., there is no sixth one).





                  share|improve this answer













                  Since the other answers are making assumptions about the input
                  that are not stated in the question
                  (e.g., that it is a bunch of ;-separated values,
                  or that there are exactly six dots),
                  I’ll provide this slightly clunky answer
                  that does what the question asks for:



                  sed 's/^([^.]*.[^.]*).([^.]*).([^.]*).([^.]*)./1,2,3,4,/'


                  This breaks down each input line as follows:



                  • Capture group 1: Starting at the beginning of the line,
                    any number of characters other than .,
                    then one . (the first one in the line),
                    then another arbitrarily long sequence of characters other than .,

                  • A . (the second one in the line),

                  • Capture group 2: Any number of characters other than .,

                  • A . (the third one in the line),

                  • Capture group 3: Any number of characters other than .,

                  • A . (the fourth one in the line),

                  • Capture group 4: Any number of characters other than .,

                  • A . (the fifth one in the line),

                  • Whatever follows (not matched by the regular expression,
                    but there can be more to the line than the above,
                    because the regex doesn’t end with $).

                  And replaces it with



                  • Capture group 1: Everything up to the second . in the line
                    (including the first one),

                  • A , (replacing the second .),

                  • Capture group 2: Everything between the second . and the third one,

                  • A , (replacing the third .),

                  • Capture group 3: Everything between the third . and the fourth one,

                  • A , (replacing the fourth .),

                  • Capture group 4: Everything between the fourth . and the fifth one,

                  • A , (replacing the fifth .),

                  • Whatever follows the fifth ..

                  So it replaces the second, third, fourth and fifth dots with commas.



                  • This will make no changes on a line with fewer than five dots.

                  • This will leave an arbitrary number of dots after the fifth unchanged.

                  • This will replace the second, third, fourth and fifth dots,
                    even if there are only five dots in the line (i.e., there is no sixth one).






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 5 hours ago









                  G-ManG-Man

                  13.5k93668




                  13.5k93668





















                      -1














                      Another sed with a loop :



                      sed ':A;s/([^.]*.[^.]*).(.*;[^;]*$)/1,2/;tA' infile





                      share|improve this answer


















                      • 1





                        -1 for no explanation and for not answering the question as asked.

                        – G-Man
                        10 hours ago











                      • @G-Man : Thanks for downvote : I don't look for reputation. I'm here to learn and try to help other. no explanation ? I say it is a loop, no? but you forgot to tell why not answering the question ! So -1 for my answer, It is a bad answer. Someone come here don't look at that but I get the espected result, no? As you can see, a lot of time there is no feedback. Askers got answer and go away. A lot of time, they are not interested by explanation. If they are, they ask for explanation via comment.

                        – ctac_
                        6 hours ago











                      • @G-Man : An other thing, all here don't speak english very well, like me. It's more difficult for them first to understand the question and second to answer with explanation. Sometime they get downvote because explanation are not clear enough.

                        – ctac_
                        6 hours ago











                      • The question said “I want to replace … the second, third, fourth and fifth dot [in this string] with a comma.”  If you take the question super-literally, you could say that echo "2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1" was an answer — but that would be ridiculous.  But you assumed that the input would be exactly like the example — that it would contain some semicolons — and you posted an answer that depends on that assumption.  Based on what the question says, any answer should convert a.b.c.d.e.f.g into a.b,c,d,e,f.g, and yours doesn’t.

                        – G-Man
                        6 hours ago











                      • @G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.

                        – ctac_
                        5 hours ago















                      -1














                      Another sed with a loop :



                      sed ':A;s/([^.]*.[^.]*).(.*;[^;]*$)/1,2/;tA' infile





                      share|improve this answer


















                      • 1





                        -1 for no explanation and for not answering the question as asked.

                        – G-Man
                        10 hours ago











                      • @G-Man : Thanks for downvote : I don't look for reputation. I'm here to learn and try to help other. no explanation ? I say it is a loop, no? but you forgot to tell why not answering the question ! So -1 for my answer, It is a bad answer. Someone come here don't look at that but I get the espected result, no? As you can see, a lot of time there is no feedback. Askers got answer and go away. A lot of time, they are not interested by explanation. If they are, they ask for explanation via comment.

                        – ctac_
                        6 hours ago











                      • @G-Man : An other thing, all here don't speak english very well, like me. It's more difficult for them first to understand the question and second to answer with explanation. Sometime they get downvote because explanation are not clear enough.

                        – ctac_
                        6 hours ago











                      • The question said “I want to replace … the second, third, fourth and fifth dot [in this string] with a comma.”  If you take the question super-literally, you could say that echo "2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1" was an answer — but that would be ridiculous.  But you assumed that the input would be exactly like the example — that it would contain some semicolons — and you posted an answer that depends on that assumption.  Based on what the question says, any answer should convert a.b.c.d.e.f.g into a.b,c,d,e,f.g, and yours doesn’t.

                        – G-Man
                        6 hours ago











                      • @G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.

                        – ctac_
                        5 hours ago













                      -1












                      -1








                      -1







                      Another sed with a loop :



                      sed ':A;s/([^.]*.[^.]*).(.*;[^;]*$)/1,2/;tA' infile





                      share|improve this answer













                      Another sed with a loop :



                      sed ':A;s/([^.]*.[^.]*).(.*;[^;]*$)/1,2/;tA' infile






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 10 hours ago









                      ctac_ctac_

                      1,4301210




                      1,4301210







                      • 1





                        -1 for no explanation and for not answering the question as asked.

                        – G-Man
                        10 hours ago











                      • @G-Man : Thanks for downvote : I don't look for reputation. I'm here to learn and try to help other. no explanation ? I say it is a loop, no? but you forgot to tell why not answering the question ! So -1 for my answer, It is a bad answer. Someone come here don't look at that but I get the espected result, no? As you can see, a lot of time there is no feedback. Askers got answer and go away. A lot of time, they are not interested by explanation. If they are, they ask for explanation via comment.

                        – ctac_
                        6 hours ago











                      • @G-Man : An other thing, all here don't speak english very well, like me. It's more difficult for them first to understand the question and second to answer with explanation. Sometime they get downvote because explanation are not clear enough.

                        – ctac_
                        6 hours ago











                      • The question said “I want to replace … the second, third, fourth and fifth dot [in this string] with a comma.”  If you take the question super-literally, you could say that echo "2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1" was an answer — but that would be ridiculous.  But you assumed that the input would be exactly like the example — that it would contain some semicolons — and you posted an answer that depends on that assumption.  Based on what the question says, any answer should convert a.b.c.d.e.f.g into a.b,c,d,e,f.g, and yours doesn’t.

                        – G-Man
                        6 hours ago











                      • @G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.

                        – ctac_
                        5 hours ago












                      • 1





                        -1 for no explanation and for not answering the question as asked.

                        – G-Man
                        10 hours ago











                      • @G-Man : Thanks for downvote : I don't look for reputation. I'm here to learn and try to help other. no explanation ? I say it is a loop, no? but you forgot to tell why not answering the question ! So -1 for my answer, It is a bad answer. Someone come here don't look at that but I get the espected result, no? As you can see, a lot of time there is no feedback. Askers got answer and go away. A lot of time, they are not interested by explanation. If they are, they ask for explanation via comment.

                        – ctac_
                        6 hours ago











                      • @G-Man : An other thing, all here don't speak english very well, like me. It's more difficult for them first to understand the question and second to answer with explanation. Sometime they get downvote because explanation are not clear enough.

                        – ctac_
                        6 hours ago











                      • The question said “I want to replace … the second, third, fourth and fifth dot [in this string] with a comma.”  If you take the question super-literally, you could say that echo "2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1" was an answer — but that would be ridiculous.  But you assumed that the input would be exactly like the example — that it would contain some semicolons — and you posted an answer that depends on that assumption.  Based on what the question says, any answer should convert a.b.c.d.e.f.g into a.b,c,d,e,f.g, and yours doesn’t.

                        – G-Man
                        6 hours ago











                      • @G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.

                        – ctac_
                        5 hours ago







                      1




                      1





                      -1 for no explanation and for not answering the question as asked.

                      – G-Man
                      10 hours ago





                      -1 for no explanation and for not answering the question as asked.

                      – G-Man
                      10 hours ago













                      @G-Man : Thanks for downvote : I don't look for reputation. I'm here to learn and try to help other. no explanation ? I say it is a loop, no? but you forgot to tell why not answering the question ! So -1 for my answer, It is a bad answer. Someone come here don't look at that but I get the espected result, no? As you can see, a lot of time there is no feedback. Askers got answer and go away. A lot of time, they are not interested by explanation. If they are, they ask for explanation via comment.

                      – ctac_
                      6 hours ago





                      @G-Man : Thanks for downvote : I don't look for reputation. I'm here to learn and try to help other. no explanation ? I say it is a loop, no? but you forgot to tell why not answering the question ! So -1 for my answer, It is a bad answer. Someone come here don't look at that but I get the espected result, no? As you can see, a lot of time there is no feedback. Askers got answer and go away. A lot of time, they are not interested by explanation. If they are, they ask for explanation via comment.

                      – ctac_
                      6 hours ago













                      @G-Man : An other thing, all here don't speak english very well, like me. It's more difficult for them first to understand the question and second to answer with explanation. Sometime they get downvote because explanation are not clear enough.

                      – ctac_
                      6 hours ago





                      @G-Man : An other thing, all here don't speak english very well, like me. It's more difficult for them first to understand the question and second to answer with explanation. Sometime they get downvote because explanation are not clear enough.

                      – ctac_
                      6 hours ago













                      The question said “I want to replace … the second, third, fourth and fifth dot [in this string] with a comma.”  If you take the question super-literally, you could say that echo "2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1" was an answer — but that would be ridiculous.  But you assumed that the input would be exactly like the example — that it would contain some semicolons — and you posted an answer that depends on that assumption.  Based on what the question says, any answer should convert a.b.c.d.e.f.g into a.b,c,d,e,f.g, and yours doesn’t.

                      – G-Man
                      6 hours ago





                      The question said “I want to replace … the second, third, fourth and fifth dot [in this string] with a comma.”  If you take the question super-literally, you could say that echo "2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1" was an answer — but that would be ridiculous.  But you assumed that the input would be exactly like the example — that it would contain some semicolons — and you posted an answer that depends on that assumption.  Based on what the question says, any answer should convert a.b.c.d.e.f.g into a.b,c,d,e,f.g, and yours doesn’t.

                      – G-Man
                      6 hours ago













                      @G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.

                      – ctac_
                      5 hours ago





                      @G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.

                      – ctac_
                      5 hours ago











                      -1














                      You could approach this using sed editor as follows:



                      $ sed -e '
                      y/./n/
                      s/n(.*)n/.1./
                      y/n/,/
                      ' input.txt


                      The premise being that we first turn all dots into newlines, a char guaranteed to not be there in the pattern space.
                      Then we change the last and the first newlines back into dots.
                      The remaining newlines all get converted to commas.



                      HTH.






                      share|improve this answer


















                      • 1





                        That's rather neat, but it does seem to change some dots in the IP number at the end.

                        – Kusalananda
                        6 hours ago












                      • Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.

                        – Rakesh Sharma
                        6 hours ago















                      -1














                      You could approach this using sed editor as follows:



                      $ sed -e '
                      y/./n/
                      s/n(.*)n/.1./
                      y/n/,/
                      ' input.txt


                      The premise being that we first turn all dots into newlines, a char guaranteed to not be there in the pattern space.
                      Then we change the last and the first newlines back into dots.
                      The remaining newlines all get converted to commas.



                      HTH.






                      share|improve this answer


















                      • 1





                        That's rather neat, but it does seem to change some dots in the IP number at the end.

                        – Kusalananda
                        6 hours ago












                      • Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.

                        – Rakesh Sharma
                        6 hours ago













                      -1












                      -1








                      -1







                      You could approach this using sed editor as follows:



                      $ sed -e '
                      y/./n/
                      s/n(.*)n/.1./
                      y/n/,/
                      ' input.txt


                      The premise being that we first turn all dots into newlines, a char guaranteed to not be there in the pattern space.
                      Then we change the last and the first newlines back into dots.
                      The remaining newlines all get converted to commas.



                      HTH.






                      share|improve this answer













                      You could approach this using sed editor as follows:



                      $ sed -e '
                      y/./n/
                      s/n(.*)n/.1./
                      y/n/,/
                      ' input.txt


                      The premise being that we first turn all dots into newlines, a char guaranteed to not be there in the pattern space.
                      Then we change the last and the first newlines back into dots.
                      The remaining newlines all get converted to commas.



                      HTH.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 6 hours ago









                      Rakesh SharmaRakesh Sharma

                      342115




                      342115







                      • 1





                        That's rather neat, but it does seem to change some dots in the IP number at the end.

                        – Kusalananda
                        6 hours ago












                      • Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.

                        – Rakesh Sharma
                        6 hours ago












                      • 1





                        That's rather neat, but it does seem to change some dots in the IP number at the end.

                        – Kusalananda
                        6 hours ago












                      • Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.

                        – Rakesh Sharma
                        6 hours ago







                      1




                      1





                      That's rather neat, but it does seem to change some dots in the IP number at the end.

                      – Kusalananda
                      6 hours ago






                      That's rather neat, but it does seem to change some dots in the IP number at the end.

                      – Kusalananda
                      6 hours ago














                      Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.

                      – Rakesh Sharma
                      6 hours ago





                      Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.

                      – Rakesh Sharma
                      6 hours ago










                      fty4 is a new contributor. Be nice, and check out our Code of Conduct.









                      draft saved

                      draft discarded


















                      fty4 is a new contributor. Be nice, and check out our Code of Conduct.












                      fty4 is a new contributor. Be nice, and check out our Code of Conduct.











                      fty4 is a new contributor. Be nice, and check out our Code of Conduct.














                      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%2f506807%2freplace-four-times-with-sed%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 create a command for the “strange m” symbol in latex? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)How do you make your own symbol when Detexify fails?Writing bold small caps with mathpazo packageplus-minus symbol with parenthesis around the minus signGreek character in Beamer document titleHow to create dashed right arrow over symbol?Currency symbol: Turkish LiraDouble prec as a single symbol?Plus Sign Too Big; How to Call adfbullet?Is there a TeX macro for three-legged pi?How do I get my integral-like symbol to align like the integral?How to selectively substitute a letter with another symbol representing the same letterHow do I generate a less than symbol and vertical bar that are the same height?

                      Българска екзархия Съдържание История | Български екзарси | Вижте също | Външни препратки | Литература | Бележки | НавигацияУстав за управлението на българската екзархия. Цариград, 1870Слово на Ловешкия митрополит Иларион при откриването на Българския народен събор в Цариград на 23. II. 1870 г.Българската правда и гръцката кривда. От С. М. (= Софийски Мелетий). Цариград, 1872Предстоятели на Българската екзархияПодмененият ВеликденИнформационна агенция „Фокус“Димитър Ризов. Българите в техните исторически, етнографически и политически граници (Атлас съдържащ 40 карти). Berlin, Königliche Hoflithographie, Hof-Buch- und -Steindruckerei Wilhelm Greve, 1917Report of the International Commission to Inquire into the Causes and Conduct of the Balkan Wars

                      Чепеларе Съдържание География | История | Население | Спортни и природни забележителности | Културни и исторически обекти | Религии | Обществени институции | Известни личности | Редовни събития | Галерия | Източници | Литература | Външни препратки | Навигация41°43′23.99″ с. ш. 24°41′09.99″ и. д. / 41.723333° с. ш. 24.686111° и. д.*ЧепелареЧепеларски Linux fest 2002Начало на Зимен сезон 2005/06Национални хайдушки празници „Капитан Петко Войвода“Град ЧепелареЧепеларе – народният ски курортbgrod.orgwww.terranatura.hit.bgСправка за населението на гр. Исперих, общ. Исперих, обл. РазградМузей на родопския карстМузей на спорта и скитеЧепеларебългарскибългарскианглийскитукИстория на градаСки писти в ЧепелареВремето в ЧепелареРадио и телевизия в ЧепелареЧепеларе мами с родопски чар и добри пистиЕвтин туризъм и снежни атракции в ЧепелареМестоположениеИнформация и снимки от музея на родопския карст3D панорами от ЧепелареЧепелареррр