How do i split a CR delimited string into multiple lines and append id to each new lineHow to split output...

Is it true that "only photographers care about noise"?

If the pressure inside and outside a balloon balance, then why does air leave when it pops?

How can I find out about the game world without meta-influencing it?

What is the STRONGEST end-of-line knot to use if you want to use a steel-thimble at the end, so that you've got a steel-eyelet at the end of the line?

Should I list a completely different profession in my technical resume?

What is Gilligan's full Name?

If absolute velocity does not exist, how can we say a rocket accelerates in empty space?

How to Handle Many Times Series Simultaneously?

Mathematica 12 has gotten worse at solving simple equations?

Traceroute showing inter-vlan routing?

Etymology of the expression "to entertain an idea"

How to generate list of *all* available commands and functions?

Deciphering old handwriting from a 1850 church record

Selecting by attribute using Python and a list

When to use и or а as “and”?

How to make a composition of functions prettier?

What does "lit." mean in boiling point or melting point specification?

What plausible reason could I give for my FTL drive only working in space

Placement of positioning lights on A320 winglets

Problem with pronounciation

DateTime.addMonths skips a month (from feb to mar)

Is it advisable to add a location heads-up when a scene changes in a novel?

How much web presence should I have?

Why do I seem to lose data using this bash pipe construction?



How do i split a CR delimited string into multiple lines and append id to each new line


How to split output lines into multiple lines (only lines not files)?Split line into key-value pairs based on first stringsplit file into multiple piecesMerge lines into single lineAppend line to previous line where a column is split into multiple linesSplit single line into multiple lines, Newline character missing for all the lines in input fileAppend string after each line except header and footer lineConcatenate multiple lines into “special-character”-delimited stringSplit a delimited file into multiple files based on the values in one columnSplit string using multiple delimiter






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















Below is the sample delimited file



EMP|123|10rINFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH
EMP|456|10rINFO|DAVID|PIRT|M|02/12/1980rADDR|257|BRIDGE RD|COLUMBUS|OH
EMP|789|10rINFO|JENN|MCKENZI|F|03/12/1980rADDR|389|BRIDGE RD|COLUMBUS|OH


Expected Output:



EMP|123|10
123|INFO|JOHN|SMITH|M|01/12/1980
123|ADDR|125|BRIDGE RD|COLUMBUS|OH
EMP|456|10
456|INFO|DAVID|PIRT|M|02/12/1980
456|ADDR|257|BRIDGE RD|COLUMBUS|OH
EMP|789|10
789|INFO|JENN|MCKENZI|F|03/12/1980
789|ADDR|389|BRIDGE RD|COLUMBUS|OH


So the Id is repeated so we can know that these 3 lines belong to 1. I tried below command but i am not sure how to append the Id to new lines



What i did and my out put:



tr 'r' 'n' < test.txt > new.txt

EMP|123|10
INFO|JOHN|SMITH|M|01/12/1980
ADDR|125|BRIDGE RD|COLUMBUS|OH
EMP|456|10
INFO|DAVID|PIRT|M|02/12/1980
ADDR|257|BRIDGE RD|COLUMBUS|OH
EMP|789|10
INFO|JENN|MCKENZI|F|03/12/1980
ADDR|389|BRIDGE RD|COLUMBUS|OH


Any help is appreciated










share|improve this question









New contributor



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


























    0















    Below is the sample delimited file



    EMP|123|10rINFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH
    EMP|456|10rINFO|DAVID|PIRT|M|02/12/1980rADDR|257|BRIDGE RD|COLUMBUS|OH
    EMP|789|10rINFO|JENN|MCKENZI|F|03/12/1980rADDR|389|BRIDGE RD|COLUMBUS|OH


    Expected Output:



    EMP|123|10
    123|INFO|JOHN|SMITH|M|01/12/1980
    123|ADDR|125|BRIDGE RD|COLUMBUS|OH
    EMP|456|10
    456|INFO|DAVID|PIRT|M|02/12/1980
    456|ADDR|257|BRIDGE RD|COLUMBUS|OH
    EMP|789|10
    789|INFO|JENN|MCKENZI|F|03/12/1980
    789|ADDR|389|BRIDGE RD|COLUMBUS|OH


    So the Id is repeated so we can know that these 3 lines belong to 1. I tried below command but i am not sure how to append the Id to new lines



    What i did and my out put:



    tr 'r' 'n' < test.txt > new.txt

    EMP|123|10
    INFO|JOHN|SMITH|M|01/12/1980
    ADDR|125|BRIDGE RD|COLUMBUS|OH
    EMP|456|10
    INFO|DAVID|PIRT|M|02/12/1980
    ADDR|257|BRIDGE RD|COLUMBUS|OH
    EMP|789|10
    INFO|JENN|MCKENZI|F|03/12/1980
    ADDR|389|BRIDGE RD|COLUMBUS|OH


    Any help is appreciated










    share|improve this question









    New contributor



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






















      0












      0








      0








      Below is the sample delimited file



      EMP|123|10rINFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH
      EMP|456|10rINFO|DAVID|PIRT|M|02/12/1980rADDR|257|BRIDGE RD|COLUMBUS|OH
      EMP|789|10rINFO|JENN|MCKENZI|F|03/12/1980rADDR|389|BRIDGE RD|COLUMBUS|OH


      Expected Output:



      EMP|123|10
      123|INFO|JOHN|SMITH|M|01/12/1980
      123|ADDR|125|BRIDGE RD|COLUMBUS|OH
      EMP|456|10
      456|INFO|DAVID|PIRT|M|02/12/1980
      456|ADDR|257|BRIDGE RD|COLUMBUS|OH
      EMP|789|10
      789|INFO|JENN|MCKENZI|F|03/12/1980
      789|ADDR|389|BRIDGE RD|COLUMBUS|OH


      So the Id is repeated so we can know that these 3 lines belong to 1. I tried below command but i am not sure how to append the Id to new lines



      What i did and my out put:



      tr 'r' 'n' < test.txt > new.txt

      EMP|123|10
      INFO|JOHN|SMITH|M|01/12/1980
      ADDR|125|BRIDGE RD|COLUMBUS|OH
      EMP|456|10
      INFO|DAVID|PIRT|M|02/12/1980
      ADDR|257|BRIDGE RD|COLUMBUS|OH
      EMP|789|10
      INFO|JENN|MCKENZI|F|03/12/1980
      ADDR|389|BRIDGE RD|COLUMBUS|OH


      Any help is appreciated










      share|improve this question









      New contributor



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











      Below is the sample delimited file



      EMP|123|10rINFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH
      EMP|456|10rINFO|DAVID|PIRT|M|02/12/1980rADDR|257|BRIDGE RD|COLUMBUS|OH
      EMP|789|10rINFO|JENN|MCKENZI|F|03/12/1980rADDR|389|BRIDGE RD|COLUMBUS|OH


      Expected Output:



      EMP|123|10
      123|INFO|JOHN|SMITH|M|01/12/1980
      123|ADDR|125|BRIDGE RD|COLUMBUS|OH
      EMP|456|10
      456|INFO|DAVID|PIRT|M|02/12/1980
      456|ADDR|257|BRIDGE RD|COLUMBUS|OH
      EMP|789|10
      789|INFO|JENN|MCKENZI|F|03/12/1980
      789|ADDR|389|BRIDGE RD|COLUMBUS|OH


      So the Id is repeated so we can know that these 3 lines belong to 1. I tried below command but i am not sure how to append the Id to new lines



      What i did and my out put:



      tr 'r' 'n' < test.txt > new.txt

      EMP|123|10
      INFO|JOHN|SMITH|M|01/12/1980
      ADDR|125|BRIDGE RD|COLUMBUS|OH
      EMP|456|10
      INFO|DAVID|PIRT|M|02/12/1980
      ADDR|257|BRIDGE RD|COLUMBUS|OH
      EMP|789|10
      INFO|JENN|MCKENZI|F|03/12/1980
      ADDR|389|BRIDGE RD|COLUMBUS|OH


      Any help is appreciated







      text-processing aix






      share|improve this question









      New contributor



      Phaneendra Pavuluri 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



      Phaneendra Pavuluri 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 hours ago









      Tomasz

      10.6k73473




      10.6k73473






      New contributor



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








      asked 2 hours ago









      Phaneendra PavuluriPhaneendra Pavuluri

      31




      31




      New contributor



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




      New contributor




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
























          2 Answers
          2






          active

          oldest

          votes


















          1














          How about



          $ awk -F 'r' '{
          print $1;
          split($1,a,"|");
          for(i=2;i<=NF;i++) print a[2] "|" $i;
          }' file
          EMP|123|10
          123|INFO|JOHN|SMITH|M|01/12/1980
          123|ADDR|125|BRIDGE RD|COLUMBUS|OH
          EMP|456|10
          456|INFO|DAVID|PIRT|M|02/12/1980
          456|ADDR|257|BRIDGE RD|COLUMBUS|OH
          EMP|789|10
          789|INFO|JENN|MCKENZI|F|03/12/1980
          789|ADDR|389|BRIDGE RD|COLUMBUS|OH





          share|improve this answer
























          • Thank you it helped , but i have 1 more question how do i add another string to line for example a[3] also like 123|10|..

            – Phaneendra Pavuluri
            1 hour ago













          • @PhaneendraPavuluri you mean like a[2] "|" a[3] "|" $i ? you can just concatenate strings

            – steeldriver
            1 hour ago



















          0














          In sed:



          sed ':loop; s/^([^|]*|)([^|]*|)([^r]*)r/123n2/; t loop'


          It’s a (somewhat) simple substitution:
          Match a pattern that consists of all the characters through
          (i.e., up to and including) the first r in the buffer. 
          Break it into three capture groups:




          • Everything through (i.e., up to and including)
            the first | in the buffer. 
            This will be EMP| for every line in your file.

          • Everything after that, through (i.e., up to and including)
            the next (i.e., second) | in the buffer. 
            This will be 123|, 456| or 789| in your file.

          • Everything after that, up to (but not including)
            the first r in the buffer.


          Then it replaces the above with




          • The three capture groups (123);
            i.e., everything up to (but not including) the first r in the buffer,

          • A newline, and

          • The second capture group (2); i.e., the id and the |.


          Then the rest of the buffer
          (everything after the first r in the buffer) is left in place,
          so it falls after the repeated id on the newly created line.



          Then, if the above match and substitute succeeded
          (t is test and goto if success), go back and try it again.



          An aspect of this that might not be obvious to the beginner is that,
          even after we have inserted a newline n into the buffer
          with a substitute command, thereby creating a new line,
          sed still operates on the entire buffer
          (including the internal / embedded newline character)
          and not just the ‘new line’ (i.e., the portion after the newline). 
          So, sed reads the first line,



          EMP|123|10rINFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH


          into the buffer. 
          After the s command is executed for the first time, the buffer contains



          EMP|123|10
          123|INFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH


          and so the next iteration of the s command matches





          • 1 = EMP|,


          • 2 = 123|, and


          • 3 = 10
            123|INFO|JOHN|SMITH|M|01/12/1980

            (including the newline that was inserted in the first iteration).


          Warning: This may make a mess
          if there is a r before the second | in an input line.






          share|improve this answer
























            Your Answer








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

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

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            Phaneendra Pavuluri 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%2f524106%2fhow-do-i-split-a-cr-delimited-string-into-multiple-lines-and-append-id-to-each-n%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            How about



            $ awk -F 'r' '{
            print $1;
            split($1,a,"|");
            for(i=2;i<=NF;i++) print a[2] "|" $i;
            }' file
            EMP|123|10
            123|INFO|JOHN|SMITH|M|01/12/1980
            123|ADDR|125|BRIDGE RD|COLUMBUS|OH
            EMP|456|10
            456|INFO|DAVID|PIRT|M|02/12/1980
            456|ADDR|257|BRIDGE RD|COLUMBUS|OH
            EMP|789|10
            789|INFO|JENN|MCKENZI|F|03/12/1980
            789|ADDR|389|BRIDGE RD|COLUMBUS|OH





            share|improve this answer
























            • Thank you it helped , but i have 1 more question how do i add another string to line for example a[3] also like 123|10|..

              – Phaneendra Pavuluri
              1 hour ago













            • @PhaneendraPavuluri you mean like a[2] "|" a[3] "|" $i ? you can just concatenate strings

              – steeldriver
              1 hour ago
















            1














            How about



            $ awk -F 'r' '{
            print $1;
            split($1,a,"|");
            for(i=2;i<=NF;i++) print a[2] "|" $i;
            }' file
            EMP|123|10
            123|INFO|JOHN|SMITH|M|01/12/1980
            123|ADDR|125|BRIDGE RD|COLUMBUS|OH
            EMP|456|10
            456|INFO|DAVID|PIRT|M|02/12/1980
            456|ADDR|257|BRIDGE RD|COLUMBUS|OH
            EMP|789|10
            789|INFO|JENN|MCKENZI|F|03/12/1980
            789|ADDR|389|BRIDGE RD|COLUMBUS|OH





            share|improve this answer
























            • Thank you it helped , but i have 1 more question how do i add another string to line for example a[3] also like 123|10|..

              – Phaneendra Pavuluri
              1 hour ago













            • @PhaneendraPavuluri you mean like a[2] "|" a[3] "|" $i ? you can just concatenate strings

              – steeldriver
              1 hour ago














            1












            1








            1







            How about



            $ awk -F 'r' '{
            print $1;
            split($1,a,"|");
            for(i=2;i<=NF;i++) print a[2] "|" $i;
            }' file
            EMP|123|10
            123|INFO|JOHN|SMITH|M|01/12/1980
            123|ADDR|125|BRIDGE RD|COLUMBUS|OH
            EMP|456|10
            456|INFO|DAVID|PIRT|M|02/12/1980
            456|ADDR|257|BRIDGE RD|COLUMBUS|OH
            EMP|789|10
            789|INFO|JENN|MCKENZI|F|03/12/1980
            789|ADDR|389|BRIDGE RD|COLUMBUS|OH





            share|improve this answer













            How about



            $ awk -F 'r' '{
            print $1;
            split($1,a,"|");
            for(i=2;i<=NF;i++) print a[2] "|" $i;
            }' file
            EMP|123|10
            123|INFO|JOHN|SMITH|M|01/12/1980
            123|ADDR|125|BRIDGE RD|COLUMBUS|OH
            EMP|456|10
            456|INFO|DAVID|PIRT|M|02/12/1980
            456|ADDR|257|BRIDGE RD|COLUMBUS|OH
            EMP|789|10
            789|INFO|JENN|MCKENZI|F|03/12/1980
            789|ADDR|389|BRIDGE RD|COLUMBUS|OH






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 1 hour ago









            steeldriversteeldriver

            39.7k45492




            39.7k45492













            • Thank you it helped , but i have 1 more question how do i add another string to line for example a[3] also like 123|10|..

              – Phaneendra Pavuluri
              1 hour ago













            • @PhaneendraPavuluri you mean like a[2] "|" a[3] "|" $i ? you can just concatenate strings

              – steeldriver
              1 hour ago



















            • Thank you it helped , but i have 1 more question how do i add another string to line for example a[3] also like 123|10|..

              – Phaneendra Pavuluri
              1 hour ago













            • @PhaneendraPavuluri you mean like a[2] "|" a[3] "|" $i ? you can just concatenate strings

              – steeldriver
              1 hour ago

















            Thank you it helped , but i have 1 more question how do i add another string to line for example a[3] also like 123|10|..

            – Phaneendra Pavuluri
            1 hour ago







            Thank you it helped , but i have 1 more question how do i add another string to line for example a[3] also like 123|10|..

            – Phaneendra Pavuluri
            1 hour ago















            @PhaneendraPavuluri you mean like a[2] "|" a[3] "|" $i ? you can just concatenate strings

            – steeldriver
            1 hour ago





            @PhaneendraPavuluri you mean like a[2] "|" a[3] "|" $i ? you can just concatenate strings

            – steeldriver
            1 hour ago













            0














            In sed:



            sed ':loop; s/^([^|]*|)([^|]*|)([^r]*)r/123n2/; t loop'


            It’s a (somewhat) simple substitution:
            Match a pattern that consists of all the characters through
            (i.e., up to and including) the first r in the buffer. 
            Break it into three capture groups:




            • Everything through (i.e., up to and including)
              the first | in the buffer. 
              This will be EMP| for every line in your file.

            • Everything after that, through (i.e., up to and including)
              the next (i.e., second) | in the buffer. 
              This will be 123|, 456| or 789| in your file.

            • Everything after that, up to (but not including)
              the first r in the buffer.


            Then it replaces the above with




            • The three capture groups (123);
              i.e., everything up to (but not including) the first r in the buffer,

            • A newline, and

            • The second capture group (2); i.e., the id and the |.


            Then the rest of the buffer
            (everything after the first r in the buffer) is left in place,
            so it falls after the repeated id on the newly created line.



            Then, if the above match and substitute succeeded
            (t is test and goto if success), go back and try it again.



            An aspect of this that might not be obvious to the beginner is that,
            even after we have inserted a newline n into the buffer
            with a substitute command, thereby creating a new line,
            sed still operates on the entire buffer
            (including the internal / embedded newline character)
            and not just the ‘new line’ (i.e., the portion after the newline). 
            So, sed reads the first line,



            EMP|123|10rINFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH


            into the buffer. 
            After the s command is executed for the first time, the buffer contains



            EMP|123|10
            123|INFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH


            and so the next iteration of the s command matches





            • 1 = EMP|,


            • 2 = 123|, and


            • 3 = 10
              123|INFO|JOHN|SMITH|M|01/12/1980

              (including the newline that was inserted in the first iteration).


            Warning: This may make a mess
            if there is a r before the second | in an input line.






            share|improve this answer




























              0














              In sed:



              sed ':loop; s/^([^|]*|)([^|]*|)([^r]*)r/123n2/; t loop'


              It’s a (somewhat) simple substitution:
              Match a pattern that consists of all the characters through
              (i.e., up to and including) the first r in the buffer. 
              Break it into three capture groups:




              • Everything through (i.e., up to and including)
                the first | in the buffer. 
                This will be EMP| for every line in your file.

              • Everything after that, through (i.e., up to and including)
                the next (i.e., second) | in the buffer. 
                This will be 123|, 456| or 789| in your file.

              • Everything after that, up to (but not including)
                the first r in the buffer.


              Then it replaces the above with




              • The three capture groups (123);
                i.e., everything up to (but not including) the first r in the buffer,

              • A newline, and

              • The second capture group (2); i.e., the id and the |.


              Then the rest of the buffer
              (everything after the first r in the buffer) is left in place,
              so it falls after the repeated id on the newly created line.



              Then, if the above match and substitute succeeded
              (t is test and goto if success), go back and try it again.



              An aspect of this that might not be obvious to the beginner is that,
              even after we have inserted a newline n into the buffer
              with a substitute command, thereby creating a new line,
              sed still operates on the entire buffer
              (including the internal / embedded newline character)
              and not just the ‘new line’ (i.e., the portion after the newline). 
              So, sed reads the first line,



              EMP|123|10rINFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH


              into the buffer. 
              After the s command is executed for the first time, the buffer contains



              EMP|123|10
              123|INFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH


              and so the next iteration of the s command matches





              • 1 = EMP|,


              • 2 = 123|, and


              • 3 = 10
                123|INFO|JOHN|SMITH|M|01/12/1980

                (including the newline that was inserted in the first iteration).


              Warning: This may make a mess
              if there is a r before the second | in an input line.






              share|improve this answer


























                0












                0








                0







                In sed:



                sed ':loop; s/^([^|]*|)([^|]*|)([^r]*)r/123n2/; t loop'


                It’s a (somewhat) simple substitution:
                Match a pattern that consists of all the characters through
                (i.e., up to and including) the first r in the buffer. 
                Break it into three capture groups:




                • Everything through (i.e., up to and including)
                  the first | in the buffer. 
                  This will be EMP| for every line in your file.

                • Everything after that, through (i.e., up to and including)
                  the next (i.e., second) | in the buffer. 
                  This will be 123|, 456| or 789| in your file.

                • Everything after that, up to (but not including)
                  the first r in the buffer.


                Then it replaces the above with




                • The three capture groups (123);
                  i.e., everything up to (but not including) the first r in the buffer,

                • A newline, and

                • The second capture group (2); i.e., the id and the |.


                Then the rest of the buffer
                (everything after the first r in the buffer) is left in place,
                so it falls after the repeated id on the newly created line.



                Then, if the above match and substitute succeeded
                (t is test and goto if success), go back and try it again.



                An aspect of this that might not be obvious to the beginner is that,
                even after we have inserted a newline n into the buffer
                with a substitute command, thereby creating a new line,
                sed still operates on the entire buffer
                (including the internal / embedded newline character)
                and not just the ‘new line’ (i.e., the portion after the newline). 
                So, sed reads the first line,



                EMP|123|10rINFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH


                into the buffer. 
                After the s command is executed for the first time, the buffer contains



                EMP|123|10
                123|INFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH


                and so the next iteration of the s command matches





                • 1 = EMP|,


                • 2 = 123|, and


                • 3 = 10
                  123|INFO|JOHN|SMITH|M|01/12/1980

                  (including the newline that was inserted in the first iteration).


                Warning: This may make a mess
                if there is a r before the second | in an input line.






                share|improve this answer













                In sed:



                sed ':loop; s/^([^|]*|)([^|]*|)([^r]*)r/123n2/; t loop'


                It’s a (somewhat) simple substitution:
                Match a pattern that consists of all the characters through
                (i.e., up to and including) the first r in the buffer. 
                Break it into three capture groups:




                • Everything through (i.e., up to and including)
                  the first | in the buffer. 
                  This will be EMP| for every line in your file.

                • Everything after that, through (i.e., up to and including)
                  the next (i.e., second) | in the buffer. 
                  This will be 123|, 456| or 789| in your file.

                • Everything after that, up to (but not including)
                  the first r in the buffer.


                Then it replaces the above with




                • The three capture groups (123);
                  i.e., everything up to (but not including) the first r in the buffer,

                • A newline, and

                • The second capture group (2); i.e., the id and the |.


                Then the rest of the buffer
                (everything after the first r in the buffer) is left in place,
                so it falls after the repeated id on the newly created line.



                Then, if the above match and substitute succeeded
                (t is test and goto if success), go back and try it again.



                An aspect of this that might not be obvious to the beginner is that,
                even after we have inserted a newline n into the buffer
                with a substitute command, thereby creating a new line,
                sed still operates on the entire buffer
                (including the internal / embedded newline character)
                and not just the ‘new line’ (i.e., the portion after the newline). 
                So, sed reads the first line,



                EMP|123|10rINFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH


                into the buffer. 
                After the s command is executed for the first time, the buffer contains



                EMP|123|10
                123|INFO|JOHN|SMITH|M|01/12/1980rADDR|125|BRIDGE RD|COLUMBUS|OH


                and so the next iteration of the s command matches





                • 1 = EMP|,


                • 2 = 123|, and


                • 3 = 10
                  123|INFO|JOHN|SMITH|M|01/12/1980

                  (including the newline that was inserted in the first iteration).


                Warning: This may make a mess
                if there is a r before the second | in an input line.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 34 mins ago









                G-ManG-Man

                14.7k94175




                14.7k94175






















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










                    draft saved

                    draft discarded


















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













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












                    Phaneendra Pavuluri 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%2f524106%2fhow-do-i-split-a-cr-delimited-string-into-multiple-lines-and-append-id-to-each-n%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

                    Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

                    The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

                    Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...