How to insert text alternatively from two files with a common pattern

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











up vote
4
down vote

favorite












I have two files...



First File Input...



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.


Second File Input...



1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.


I need Output like...



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.


I am using command ...



sed -e '/Ans./r file2' -e:n -e 'n;bn' -e file1


But the second o/p from 2 is reversed



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

**2. Which day is today ?**
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.

**2 How many stairs in room?**
A.6
B.10
C.15
D.23









share|improve this question























  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
    – Kusalananda
    Oct 12 '17 at 19:34














up vote
4
down vote

favorite












I have two files...



First File Input...



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.


Second File Input...



1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.


I need Output like...



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.


I am using command ...



sed -e '/Ans./r file2' -e:n -e 'n;bn' -e file1


But the second o/p from 2 is reversed



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

**2. Which day is today ?**
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.

**2 How many stairs in room?**
A.6
B.10
C.15
D.23









share|improve this question























  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
    – Kusalananda
    Oct 12 '17 at 19:34












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I have two files...



First File Input...



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.


Second File Input...



1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.


I need Output like...



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.


I am using command ...



sed -e '/Ans./r file2' -e:n -e 'n;bn' -e file1


But the second o/p from 2 is reversed



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

**2. Which day is today ?**
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.

**2 How many stairs in room?**
A.6
B.10
C.15
D.23









share|improve this question















I have two files...



First File Input...



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.


Second File Input...



1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.


I need Output like...



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.


I am using command ...



sed -e '/Ans./r file2' -e:n -e 'n;bn' -e file1


But the second o/p from 2 is reversed



1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

**2. Which day is today ?**
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.

**2 How many stairs in room?**
A.6
B.10
C.15
D.23






shell-script text-processing sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 9 '17 at 8:51









Philippos

5,93211546




5,93211546










asked Oct 9 '17 at 0:50









Pawan

212




212











  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
    – Kusalananda
    Oct 12 '17 at 19:34
















  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
    – Kusalananda
    Oct 12 '17 at 19:34















If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
– Kusalananda
Oct 12 '17 at 19:34




If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
– Kusalananda
Oct 12 '17 at 19:34










3 Answers
3






active

oldest

votes

















up vote
1
down vote













You can get pretty close using awk in paragraph mode, with getline (it doesn't quite preserve the record separators - you may be able to do better with GNU awk, using the RT variable):



$ awk 'BEGINRS=""; ORS="nn" print; if( (getline < "second") > -1) print' first
1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.





share|improve this answer




















  • its Working fine
    – Pawan
    Oct 9 '17 at 16:56






  • 1




    @Pawan Then why not accept this as an answer?
    – Philippos
    Oct 11 '17 at 7:43










  • @Pawan Good! If this solves your issue, please consider accepting the answer.
    – Kusalananda
    Oct 12 '17 at 19:36

















up vote
1
down vote













You may achieve this with awk



awk -v RS='' '
FNR == NR block[NR] = $0; next
print block[FNR] "nn" $0 "n"
' file1 file2


Blocks are separated by empty lines (RS=''). First line (FNR == NR) stores the blocks of file1 into the block array. For each block of file2, the second line prints the corresponding block in file1 then the block in file2.



Notes:



  • I'm assuming each file contains the same number of text blocks

  • For a proper formatting, ensure that the last block in each file is followed by en empty line.


If there is a different number of blocks in each file, here is a (admittedly hackish) solution using bash and GNU tools sed and paste:



paste -z -d 'n' <(sed -z 's/nn/nx00/g' file1) <(sed -z 's/nn/nnx00/g' file2)


The sed commands append a zero byte to the block separators (empty lines). paste uses that zero byte as a delimiter and prints each block in turn, separating them with a new line.






share|improve this answer





























    up vote
    0
    down vote













    Here is a script and output to illustrate the use of standard utility paste for this. First the simple case of pasting lines alternately from 2 separate files. Then all we need to do is make the paragraph, stanza input into a single line and use the paste method. The gathering of lines in a group into a "superline" is done with a short perl script that transforms each line by replacing the newline with some other character, in this case "@". Then we paste, and then we separate the superlines back into paragraphs. This is a useful tehcnique because most *nix commands operate at the line level. It is useful enough, in fact, that at our shop we have a utility "masuli" to make such superlines.



    The data files are smaller in the interest of conserving visual space.



    #!/usr/bin/env bash

    # @(#) s1 Demonstrate modular approach to multi-line data, perl, paste.

    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    pe() for _i;do printf "%s" "$_i";done; printf "n";
    pl() pe;pe "-----" ;pe "$*";
    em() pe "$*" >&2 ;
    db() ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "n" ) >&2 ;
    db() : ;
    C=$HOME/bin/context && [ -f $C ] && $C paste perl

    pl " Input data file data[12]:"
    head data[12]

    pl " Results, proof of concept:"
    paste -d'n' data[12]

    pl " Input data files data[34]:"
    head data[34]

    pl " Results, collect, paste, separate:"
    paste -d'n' <( perl -00 -wp -e 's/n/@/g;s/@@/=n/' data3 )
    <( perl -00 -wp -e 's/n/@/g;s/@@/=n/' data4 ) |
    tee f1 |
    perl -000 -wp -e 's/@/n/g;s/=/nn/g;s/nnn/nn/g'

    exit 0


    producing:



    $ ./s1

    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
    Distribution : Debian 8.9 (jessie)
    bash GNU bash 4.3.30
    paste (GNU coreutils) 8.23
    perl 5.20.2

    -----
    Input data file data[12]:
    ==> data1 <==
    1.1
    1.2

    ==> data2 <==
    2.1
    2.2

    -----
    Results, proof of concept:
    1.1
    2.1
    1.2
    2.2

    -----
    Input data files data[34]:
    ==> data3 <==
    3.1a
    3.1b

    3.2a
    3.2b


    ==> data4 <==
    4.1a
    4.1b

    4.2a
    4.2b


    -----
    Results, collect, paste, separate:
    3.1a
    3.1b

    4.1a
    4.1b

    3.2a
    3.2b

    4.2a
    4.2b


    Best wishes ... cheers, drl






    share|improve this answer




















      Your Answer







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

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

      else
      createEditor();

      );

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



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f396913%2fhow-to-insert-text-alternatively-from-two-files-with-a-common-pattern%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      You can get pretty close using awk in paragraph mode, with getline (it doesn't quite preserve the record separators - you may be able to do better with GNU awk, using the RT variable):



      $ awk 'BEGINRS=""; ORS="nn" print; if( (getline < "second") > -1) print' first
      1 How many steps are in programming?
      A.5
      B.4
      C.3
      D.9
      Ans.

      1. Who is outside of the room ?
      A.john
      B.Mary
      C.Scot
      D.Fery
      Ans.

      2 How many stairs in room?
      A.6
      B.10
      C.15
      D.23
      Ans.

      2. Which day is today ?
      A.Mon
      B.Wed
      C.Friday
      D.Sunday
      Ans.





      share|improve this answer




















      • its Working fine
        – Pawan
        Oct 9 '17 at 16:56






      • 1




        @Pawan Then why not accept this as an answer?
        – Philippos
        Oct 11 '17 at 7:43










      • @Pawan Good! If this solves your issue, please consider accepting the answer.
        – Kusalananda
        Oct 12 '17 at 19:36














      up vote
      1
      down vote













      You can get pretty close using awk in paragraph mode, with getline (it doesn't quite preserve the record separators - you may be able to do better with GNU awk, using the RT variable):



      $ awk 'BEGINRS=""; ORS="nn" print; if( (getline < "second") > -1) print' first
      1 How many steps are in programming?
      A.5
      B.4
      C.3
      D.9
      Ans.

      1. Who is outside of the room ?
      A.john
      B.Mary
      C.Scot
      D.Fery
      Ans.

      2 How many stairs in room?
      A.6
      B.10
      C.15
      D.23
      Ans.

      2. Which day is today ?
      A.Mon
      B.Wed
      C.Friday
      D.Sunday
      Ans.





      share|improve this answer




















      • its Working fine
        – Pawan
        Oct 9 '17 at 16:56






      • 1




        @Pawan Then why not accept this as an answer?
        – Philippos
        Oct 11 '17 at 7:43










      • @Pawan Good! If this solves your issue, please consider accepting the answer.
        – Kusalananda
        Oct 12 '17 at 19:36












      up vote
      1
      down vote










      up vote
      1
      down vote









      You can get pretty close using awk in paragraph mode, with getline (it doesn't quite preserve the record separators - you may be able to do better with GNU awk, using the RT variable):



      $ awk 'BEGINRS=""; ORS="nn" print; if( (getline < "second") > -1) print' first
      1 How many steps are in programming?
      A.5
      B.4
      C.3
      D.9
      Ans.

      1. Who is outside of the room ?
      A.john
      B.Mary
      C.Scot
      D.Fery
      Ans.

      2 How many stairs in room?
      A.6
      B.10
      C.15
      D.23
      Ans.

      2. Which day is today ?
      A.Mon
      B.Wed
      C.Friday
      D.Sunday
      Ans.





      share|improve this answer












      You can get pretty close using awk in paragraph mode, with getline (it doesn't quite preserve the record separators - you may be able to do better with GNU awk, using the RT variable):



      $ awk 'BEGINRS=""; ORS="nn" print; if( (getline < "second") > -1) print' first
      1 How many steps are in programming?
      A.5
      B.4
      C.3
      D.9
      Ans.

      1. Who is outside of the room ?
      A.john
      B.Mary
      C.Scot
      D.Fery
      Ans.

      2 How many stairs in room?
      A.6
      B.10
      C.15
      D.23
      Ans.

      2. Which day is today ?
      A.Mon
      B.Wed
      C.Friday
      D.Sunday
      Ans.






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Oct 9 '17 at 1:25









      steeldriver

      32.1k34979




      32.1k34979











      • its Working fine
        – Pawan
        Oct 9 '17 at 16:56






      • 1




        @Pawan Then why not accept this as an answer?
        – Philippos
        Oct 11 '17 at 7:43










      • @Pawan Good! If this solves your issue, please consider accepting the answer.
        – Kusalananda
        Oct 12 '17 at 19:36
















      • its Working fine
        – Pawan
        Oct 9 '17 at 16:56






      • 1




        @Pawan Then why not accept this as an answer?
        – Philippos
        Oct 11 '17 at 7:43










      • @Pawan Good! If this solves your issue, please consider accepting the answer.
        – Kusalananda
        Oct 12 '17 at 19:36















      its Working fine
      – Pawan
      Oct 9 '17 at 16:56




      its Working fine
      – Pawan
      Oct 9 '17 at 16:56




      1




      1




      @Pawan Then why not accept this as an answer?
      – Philippos
      Oct 11 '17 at 7:43




      @Pawan Then why not accept this as an answer?
      – Philippos
      Oct 11 '17 at 7:43












      @Pawan Good! If this solves your issue, please consider accepting the answer.
      – Kusalananda
      Oct 12 '17 at 19:36




      @Pawan Good! If this solves your issue, please consider accepting the answer.
      – Kusalananda
      Oct 12 '17 at 19:36












      up vote
      1
      down vote













      You may achieve this with awk



      awk -v RS='' '
      FNR == NR block[NR] = $0; next
      print block[FNR] "nn" $0 "n"
      ' file1 file2


      Blocks are separated by empty lines (RS=''). First line (FNR == NR) stores the blocks of file1 into the block array. For each block of file2, the second line prints the corresponding block in file1 then the block in file2.



      Notes:



      • I'm assuming each file contains the same number of text blocks

      • For a proper formatting, ensure that the last block in each file is followed by en empty line.


      If there is a different number of blocks in each file, here is a (admittedly hackish) solution using bash and GNU tools sed and paste:



      paste -z -d 'n' <(sed -z 's/nn/nx00/g' file1) <(sed -z 's/nn/nnx00/g' file2)


      The sed commands append a zero byte to the block separators (empty lines). paste uses that zero byte as a delimiter and prints each block in turn, separating them with a new line.






      share|improve this answer


























        up vote
        1
        down vote













        You may achieve this with awk



        awk -v RS='' '
        FNR == NR block[NR] = $0; next
        print block[FNR] "nn" $0 "n"
        ' file1 file2


        Blocks are separated by empty lines (RS=''). First line (FNR == NR) stores the blocks of file1 into the block array. For each block of file2, the second line prints the corresponding block in file1 then the block in file2.



        Notes:



        • I'm assuming each file contains the same number of text blocks

        • For a proper formatting, ensure that the last block in each file is followed by en empty line.


        If there is a different number of blocks in each file, here is a (admittedly hackish) solution using bash and GNU tools sed and paste:



        paste -z -d 'n' <(sed -z 's/nn/nx00/g' file1) <(sed -z 's/nn/nnx00/g' file2)


        The sed commands append a zero byte to the block separators (empty lines). paste uses that zero byte as a delimiter and prints each block in turn, separating them with a new line.






        share|improve this answer
























          up vote
          1
          down vote










          up vote
          1
          down vote









          You may achieve this with awk



          awk -v RS='' '
          FNR == NR block[NR] = $0; next
          print block[FNR] "nn" $0 "n"
          ' file1 file2


          Blocks are separated by empty lines (RS=''). First line (FNR == NR) stores the blocks of file1 into the block array. For each block of file2, the second line prints the corresponding block in file1 then the block in file2.



          Notes:



          • I'm assuming each file contains the same number of text blocks

          • For a proper formatting, ensure that the last block in each file is followed by en empty line.


          If there is a different number of blocks in each file, here is a (admittedly hackish) solution using bash and GNU tools sed and paste:



          paste -z -d 'n' <(sed -z 's/nn/nx00/g' file1) <(sed -z 's/nn/nnx00/g' file2)


          The sed commands append a zero byte to the block separators (empty lines). paste uses that zero byte as a delimiter and prints each block in turn, separating them with a new line.






          share|improve this answer














          You may achieve this with awk



          awk -v RS='' '
          FNR == NR block[NR] = $0; next
          print block[FNR] "nn" $0 "n"
          ' file1 file2


          Blocks are separated by empty lines (RS=''). First line (FNR == NR) stores the blocks of file1 into the block array. For each block of file2, the second line prints the corresponding block in file1 then the block in file2.



          Notes:



          • I'm assuming each file contains the same number of text blocks

          • For a proper formatting, ensure that the last block in each file is followed by en empty line.


          If there is a different number of blocks in each file, here is a (admittedly hackish) solution using bash and GNU tools sed and paste:



          paste -z -d 'n' <(sed -z 's/nn/nx00/g' file1) <(sed -z 's/nn/nnx00/g' file2)


          The sed commands append a zero byte to the block separators (empty lines). paste uses that zero byte as a delimiter and prints each block in turn, separating them with a new line.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 9 '17 at 1:49

























          answered Oct 9 '17 at 0:55









          xhienne

          11.7k2553




          11.7k2553




















              up vote
              0
              down vote













              Here is a script and output to illustrate the use of standard utility paste for this. First the simple case of pasting lines alternately from 2 separate files. Then all we need to do is make the paragraph, stanza input into a single line and use the paste method. The gathering of lines in a group into a "superline" is done with a short perl script that transforms each line by replacing the newline with some other character, in this case "@". Then we paste, and then we separate the superlines back into paragraphs. This is a useful tehcnique because most *nix commands operate at the line level. It is useful enough, in fact, that at our shop we have a utility "masuli" to make such superlines.



              The data files are smaller in the interest of conserving visual space.



              #!/usr/bin/env bash

              # @(#) s1 Demonstrate modular approach to multi-line data, perl, paste.

              # Utility functions: print-as-echo, print-line-with-visual-space, debug.
              # export PATH="/usr/local/bin:/usr/bin:/bin"
              LC_ALL=C ; LANG=C ; export LC_ALL LANG
              pe() for _i;do printf "%s" "$_i";done; printf "n";
              pl() pe;pe "-----" ;pe "$*";
              em() pe "$*" >&2 ;
              db() ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "n" ) >&2 ;
              db() : ;
              C=$HOME/bin/context && [ -f $C ] && $C paste perl

              pl " Input data file data[12]:"
              head data[12]

              pl " Results, proof of concept:"
              paste -d'n' data[12]

              pl " Input data files data[34]:"
              head data[34]

              pl " Results, collect, paste, separate:"
              paste -d'n' <( perl -00 -wp -e 's/n/@/g;s/@@/=n/' data3 )
              <( perl -00 -wp -e 's/n/@/g;s/@@/=n/' data4 ) |
              tee f1 |
              perl -000 -wp -e 's/@/n/g;s/=/nn/g;s/nnn/nn/g'

              exit 0


              producing:



              $ ./s1

              Environment: LC_ALL = C, LANG = C
              (Versions displayed with local utility "version")
              OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
              Distribution : Debian 8.9 (jessie)
              bash GNU bash 4.3.30
              paste (GNU coreutils) 8.23
              perl 5.20.2

              -----
              Input data file data[12]:
              ==> data1 <==
              1.1
              1.2

              ==> data2 <==
              2.1
              2.2

              -----
              Results, proof of concept:
              1.1
              2.1
              1.2
              2.2

              -----
              Input data files data[34]:
              ==> data3 <==
              3.1a
              3.1b

              3.2a
              3.2b


              ==> data4 <==
              4.1a
              4.1b

              4.2a
              4.2b


              -----
              Results, collect, paste, separate:
              3.1a
              3.1b

              4.1a
              4.1b

              3.2a
              3.2b

              4.2a
              4.2b


              Best wishes ... cheers, drl






              share|improve this answer
























                up vote
                0
                down vote













                Here is a script and output to illustrate the use of standard utility paste for this. First the simple case of pasting lines alternately from 2 separate files. Then all we need to do is make the paragraph, stanza input into a single line and use the paste method. The gathering of lines in a group into a "superline" is done with a short perl script that transforms each line by replacing the newline with some other character, in this case "@". Then we paste, and then we separate the superlines back into paragraphs. This is a useful tehcnique because most *nix commands operate at the line level. It is useful enough, in fact, that at our shop we have a utility "masuli" to make such superlines.



                The data files are smaller in the interest of conserving visual space.



                #!/usr/bin/env bash

                # @(#) s1 Demonstrate modular approach to multi-line data, perl, paste.

                # Utility functions: print-as-echo, print-line-with-visual-space, debug.
                # export PATH="/usr/local/bin:/usr/bin:/bin"
                LC_ALL=C ; LANG=C ; export LC_ALL LANG
                pe() for _i;do printf "%s" "$_i";done; printf "n";
                pl() pe;pe "-----" ;pe "$*";
                em() pe "$*" >&2 ;
                db() ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "n" ) >&2 ;
                db() : ;
                C=$HOME/bin/context && [ -f $C ] && $C paste perl

                pl " Input data file data[12]:"
                head data[12]

                pl " Results, proof of concept:"
                paste -d'n' data[12]

                pl " Input data files data[34]:"
                head data[34]

                pl " Results, collect, paste, separate:"
                paste -d'n' <( perl -00 -wp -e 's/n/@/g;s/@@/=n/' data3 )
                <( perl -00 -wp -e 's/n/@/g;s/@@/=n/' data4 ) |
                tee f1 |
                perl -000 -wp -e 's/@/n/g;s/=/nn/g;s/nnn/nn/g'

                exit 0


                producing:



                $ ./s1

                Environment: LC_ALL = C, LANG = C
                (Versions displayed with local utility "version")
                OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
                Distribution : Debian 8.9 (jessie)
                bash GNU bash 4.3.30
                paste (GNU coreutils) 8.23
                perl 5.20.2

                -----
                Input data file data[12]:
                ==> data1 <==
                1.1
                1.2

                ==> data2 <==
                2.1
                2.2

                -----
                Results, proof of concept:
                1.1
                2.1
                1.2
                2.2

                -----
                Input data files data[34]:
                ==> data3 <==
                3.1a
                3.1b

                3.2a
                3.2b


                ==> data4 <==
                4.1a
                4.1b

                4.2a
                4.2b


                -----
                Results, collect, paste, separate:
                3.1a
                3.1b

                4.1a
                4.1b

                3.2a
                3.2b

                4.2a
                4.2b


                Best wishes ... cheers, drl






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Here is a script and output to illustrate the use of standard utility paste for this. First the simple case of pasting lines alternately from 2 separate files. Then all we need to do is make the paragraph, stanza input into a single line and use the paste method. The gathering of lines in a group into a "superline" is done with a short perl script that transforms each line by replacing the newline with some other character, in this case "@". Then we paste, and then we separate the superlines back into paragraphs. This is a useful tehcnique because most *nix commands operate at the line level. It is useful enough, in fact, that at our shop we have a utility "masuli" to make such superlines.



                  The data files are smaller in the interest of conserving visual space.



                  #!/usr/bin/env bash

                  # @(#) s1 Demonstrate modular approach to multi-line data, perl, paste.

                  # Utility functions: print-as-echo, print-line-with-visual-space, debug.
                  # export PATH="/usr/local/bin:/usr/bin:/bin"
                  LC_ALL=C ; LANG=C ; export LC_ALL LANG
                  pe() for _i;do printf "%s" "$_i";done; printf "n";
                  pl() pe;pe "-----" ;pe "$*";
                  em() pe "$*" >&2 ;
                  db() ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "n" ) >&2 ;
                  db() : ;
                  C=$HOME/bin/context && [ -f $C ] && $C paste perl

                  pl " Input data file data[12]:"
                  head data[12]

                  pl " Results, proof of concept:"
                  paste -d'n' data[12]

                  pl " Input data files data[34]:"
                  head data[34]

                  pl " Results, collect, paste, separate:"
                  paste -d'n' <( perl -00 -wp -e 's/n/@/g;s/@@/=n/' data3 )
                  <( perl -00 -wp -e 's/n/@/g;s/@@/=n/' data4 ) |
                  tee f1 |
                  perl -000 -wp -e 's/@/n/g;s/=/nn/g;s/nnn/nn/g'

                  exit 0


                  producing:



                  $ ./s1

                  Environment: LC_ALL = C, LANG = C
                  (Versions displayed with local utility "version")
                  OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
                  Distribution : Debian 8.9 (jessie)
                  bash GNU bash 4.3.30
                  paste (GNU coreutils) 8.23
                  perl 5.20.2

                  -----
                  Input data file data[12]:
                  ==> data1 <==
                  1.1
                  1.2

                  ==> data2 <==
                  2.1
                  2.2

                  -----
                  Results, proof of concept:
                  1.1
                  2.1
                  1.2
                  2.2

                  -----
                  Input data files data[34]:
                  ==> data3 <==
                  3.1a
                  3.1b

                  3.2a
                  3.2b


                  ==> data4 <==
                  4.1a
                  4.1b

                  4.2a
                  4.2b


                  -----
                  Results, collect, paste, separate:
                  3.1a
                  3.1b

                  4.1a
                  4.1b

                  3.2a
                  3.2b

                  4.2a
                  4.2b


                  Best wishes ... cheers, drl






                  share|improve this answer












                  Here is a script and output to illustrate the use of standard utility paste for this. First the simple case of pasting lines alternately from 2 separate files. Then all we need to do is make the paragraph, stanza input into a single line and use the paste method. The gathering of lines in a group into a "superline" is done with a short perl script that transforms each line by replacing the newline with some other character, in this case "@". Then we paste, and then we separate the superlines back into paragraphs. This is a useful tehcnique because most *nix commands operate at the line level. It is useful enough, in fact, that at our shop we have a utility "masuli" to make such superlines.



                  The data files are smaller in the interest of conserving visual space.



                  #!/usr/bin/env bash

                  # @(#) s1 Demonstrate modular approach to multi-line data, perl, paste.

                  # Utility functions: print-as-echo, print-line-with-visual-space, debug.
                  # export PATH="/usr/local/bin:/usr/bin:/bin"
                  LC_ALL=C ; LANG=C ; export LC_ALL LANG
                  pe() for _i;do printf "%s" "$_i";done; printf "n";
                  pl() pe;pe "-----" ;pe "$*";
                  em() pe "$*" >&2 ;
                  db() ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "n" ) >&2 ;
                  db() : ;
                  C=$HOME/bin/context && [ -f $C ] && $C paste perl

                  pl " Input data file data[12]:"
                  head data[12]

                  pl " Results, proof of concept:"
                  paste -d'n' data[12]

                  pl " Input data files data[34]:"
                  head data[34]

                  pl " Results, collect, paste, separate:"
                  paste -d'n' <( perl -00 -wp -e 's/n/@/g;s/@@/=n/' data3 )
                  <( perl -00 -wp -e 's/n/@/g;s/@@/=n/' data4 ) |
                  tee f1 |
                  perl -000 -wp -e 's/@/n/g;s/=/nn/g;s/nnn/nn/g'

                  exit 0


                  producing:



                  $ ./s1

                  Environment: LC_ALL = C, LANG = C
                  (Versions displayed with local utility "version")
                  OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
                  Distribution : Debian 8.9 (jessie)
                  bash GNU bash 4.3.30
                  paste (GNU coreutils) 8.23
                  perl 5.20.2

                  -----
                  Input data file data[12]:
                  ==> data1 <==
                  1.1
                  1.2

                  ==> data2 <==
                  2.1
                  2.2

                  -----
                  Results, proof of concept:
                  1.1
                  2.1
                  1.2
                  2.2

                  -----
                  Input data files data[34]:
                  ==> data3 <==
                  3.1a
                  3.1b

                  3.2a
                  3.2b


                  ==> data4 <==
                  4.1a
                  4.1b

                  4.2a
                  4.2b


                  -----
                  Results, collect, paste, separate:
                  3.1a
                  3.1b

                  4.1a
                  4.1b

                  3.2a
                  3.2b

                  4.2a
                  4.2b


                  Best wishes ... cheers, drl







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 9 '17 at 16:57









                  drl

                  45225




                  45225



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f396913%2fhow-to-insert-text-alternatively-from-two-files-with-a-common-pattern%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

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

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?