Replace four times with sed2019 Community Moderator ElectionReplace or append a line with string within a fileHow can I replace string in a file?sed command to replace a string from one file with entire contents of another fileInserting into middle of a string using sedSplit line into key-value pairs based on first stringThe use of multiple sed statements on the command lineNeed to remove a specific column in a CSVhow to use sed to replace some pattern with a list of string in bashIs there a another way to display a part of a line using sedsed find and replace html tag

Telemetry for feature health

Why would five hundred and five be same as one?

Why does a 97 / 92 key piano exist by Bösendorfer?

Given this phrasing in the lease, when should I pay my rent?

What properties make a magic weapon befit a Rogue more than a DEX-based Fighter?

I keep switching characters, how do I stop?

Why does the frost depth increase when the surface temperature warms up?

Where are they being held?

Adding up numbers in Portuguese is strange

How to preserve electronics (computers, iPads and phones) for hundreds of years

Why does the Persian emissary display a string of crowned skulls?

win_unzip does not extract file

How to test the sharpness of a knife?

Purpose of creating non root user

The Digit Triangles

What (if any) is the reason to buy in small local stores?

Slur or Tie when they are mixed?

"Oh no!" in Latin

Why is participating in the European Parliamentary elections used as a threat?

Animation: customize bounce interpolation

What should be the ideal length of sentences in a blog post for ease of reading?

Do I have to take mana from my deck or hand when tapping a dual land?

Limit max CPU usage SQL SERVER with WSRM

Weird lines in Microsoft Word



Replace four times with sed



2019 Community Moderator ElectionReplace or append a line with string within a fileHow can I replace string in a file?sed command to replace a string from one file with entire contents of another fileInserting into middle of a string using sedSplit line into key-value pairs based on first stringThe use of multiple sed statements on the command lineNeed to remove a specific column in a CSVhow to use sed to replace some pattern with a list of string in bashIs there a another way to display a part of a line using sedsed find and replace html tag










3















I want to replace the second, third, fourth and fifth dots in this string



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


with a commas, to get this result:



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


The first comma and the sixth (and any after that) 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
    2 days ago















3















I want to replace the second, third, fourth and fifth dots in this string



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


with a commas, to get this result:



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


The first comma and the sixth (and any after that) 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
    2 days ago













3












3








3








I want to replace the second, third, fourth and fifth dots in this string



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


with a commas, to get this result:



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


The first comma and the sixth (and any after that) 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 the second, third, fourth and fifth dots in this string



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


with a commas, to get this result:



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


The first comma and the sixth (and any after that) 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 2 days ago









G-Man

13.5k93768




13.5k93768






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 Mar 17 at 13:09









fty4fty4

185




185




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
    2 days 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
    2 days 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
2 days 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
2 days ago










4 Answers
4






active

oldest

votes


















8














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


    Here’s another approach that’s specifically for GNU sed:



    sed 's/./n/6g; s/./,/2g; s/n/./g'



    • s/./n/6g replaces all dots starting with the sixth one with newlines.


    • s/./,/2g  replaces all dots starting with the second one with commas. 
      But this is really only the second through the fifth,
      since the first command eliminated all dots past the fifth (if any).


    • s/n/./g changes all the newlines back to dots. 
      Of course, the only newlines in the line
      are the ones that were originally dots,
      so this just changes them back to what they were.

    So, if a line has only three dots,
    this will change the second and the third
    (even though the fourth and fifth don’t exist).



    Warning: 
    The behavior of the combination of a number and a g
    as flags on an s command is not specified by POSIX
    and may vary between implementations. 
    This is how it works for GNU SED, as documented in the GNU SED manual.






    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
        Mar 17 at 15:01











      • @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_
        2 days 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_
        2 days 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
        2 days 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_
        2 days 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
        2 days ago












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

        – Rakesh Sharma
        2 days 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









      8














      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





























        8














        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



























          8












          8








          8







          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 Mar 17 at 14:06

























          answered Mar 17 at 13:37









          KusalanandaKusalananda

          136k17257426




          136k17257426























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


              Here’s another approach that’s specifically for GNU sed:



              sed 's/./n/6g; s/./,/2g; s/n/./g'



              • s/./n/6g replaces all dots starting with the sixth one with newlines.


              • s/./,/2g  replaces all dots starting with the second one with commas. 
                But this is really only the second through the fifth,
                since the first command eliminated all dots past the fifth (if any).


              • s/n/./g changes all the newlines back to dots. 
                Of course, the only newlines in the line
                are the ones that were originally dots,
                so this just changes them back to what they were.

              So, if a line has only three dots,
              this will change the second and the third
              (even though the fourth and fifth don’t exist).



              Warning: 
              The behavior of the combination of a number and a g
              as flags on an s command is not specified by POSIX
              and may vary between implementations. 
              This is how it works for GNU SED, as documented in the GNU SED manual.






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


                Here’s another approach that’s specifically for GNU sed:



                sed 's/./n/6g; s/./,/2g; s/n/./g'



                • s/./n/6g replaces all dots starting with the sixth one with newlines.


                • s/./,/2g  replaces all dots starting with the second one with commas. 
                  But this is really only the second through the fifth,
                  since the first command eliminated all dots past the fifth (if any).


                • s/n/./g changes all the newlines back to dots. 
                  Of course, the only newlines in the line
                  are the ones that were originally dots,
                  so this just changes them back to what they were.

                So, if a line has only three dots,
                this will change the second and the third
                (even though the fourth and fifth don’t exist).



                Warning: 
                The behavior of the combination of a number and a g
                as flags on an s command is not specified by POSIX
                and may vary between implementations. 
                This is how it works for GNU SED, as documented in the GNU SED manual.






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


                  Here’s another approach that’s specifically for GNU sed:



                  sed 's/./n/6g; s/./,/2g; s/n/./g'



                  • s/./n/6g replaces all dots starting with the sixth one with newlines.


                  • s/./,/2g  replaces all dots starting with the second one with commas. 
                    But this is really only the second through the fifth,
                    since the first command eliminated all dots past the fifth (if any).


                  • s/n/./g changes all the newlines back to dots. 
                    Of course, the only newlines in the line
                    are the ones that were originally dots,
                    so this just changes them back to what they were.

                  So, if a line has only three dots,
                  this will change the second and the third
                  (even though the fourth and fifth don’t exist).



                  Warning: 
                  The behavior of the combination of a number and a g
                  as flags on an s command is not specified by POSIX
                  and may vary between implementations. 
                  This is how it works for GNU SED, as documented in the GNU SED manual.






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


                  Here’s another approach that’s specifically for GNU sed:



                  sed 's/./n/6g; s/./,/2g; s/n/./g'



                  • s/./n/6g replaces all dots starting with the sixth one with newlines.


                  • s/./,/2g  replaces all dots starting with the second one with commas. 
                    But this is really only the second through the fifth,
                    since the first command eliminated all dots past the fifth (if any).


                  • s/n/./g changes all the newlines back to dots. 
                    Of course, the only newlines in the line
                    are the ones that were originally dots,
                    so this just changes them back to what they were.

                  So, if a line has only three dots,
                  this will change the second and the third
                  (even though the fourth and fifth don’t exist).



                  Warning: 
                  The behavior of the combination of a number and a g
                  as flags on an s command is not specified by POSIX
                  and may vary between implementations. 
                  This is how it works for GNU SED, as documented in the GNU SED manual.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday

























                  answered 2 days ago









                  G-ManG-Man

                  13.5k93768




                  13.5k93768





















                      -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
                        Mar 17 at 15:01











                      • @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_
                        2 days 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_
                        2 days 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
                        2 days 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_
                        2 days 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
                        Mar 17 at 15:01











                      • @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_
                        2 days 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_
                        2 days 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
                        2 days 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_
                        2 days 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 Mar 17 at 14:30









                      ctac_ctac_

                      1,4301211




                      1,4301211







                      • 1





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

                        – G-Man
                        Mar 17 at 15:01











                      • @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_
                        2 days 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_
                        2 days 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
                        2 days 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_
                        2 days ago












                      • 1





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

                        – G-Man
                        Mar 17 at 15:01











                      • @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_
                        2 days 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_
                        2 days 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
                        2 days 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_
                        2 days ago







                      1




                      1





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

                      – G-Man
                      Mar 17 at 15:01





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

                      – G-Man
                      Mar 17 at 15:01













                      @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_
                      2 days 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_
                      2 days 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_
                      2 days 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_
                      2 days 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
                      2 days 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
                      2 days 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_
                      2 days 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_
                      2 days 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
                        2 days ago












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

                        – Rakesh Sharma
                        2 days 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
                        2 days ago












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

                        – Rakesh Sharma
                        2 days 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 2 days 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
                        2 days ago












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

                        – Rakesh Sharma
                        2 days ago












                      • 1





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

                        – Kusalananda
                        2 days ago












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

                        – Rakesh Sharma
                        2 days ago







                      1




                      1





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

                      – Kusalananda
                      2 days ago






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

                      – Kusalananda
                      2 days ago














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

                      – Rakesh Sharma
                      2 days ago





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

                      – Rakesh Sharma
                      2 days 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

                      Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

                      Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

                      Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?