Add prefix to each lines of file from other file












0















I have two files named file1.txt and file2.txt, I'm trying to add the prefix to file2.txt from file1.txt in a manner so I can pipe the results to the next utility I'm using.



cat file1.txt

aa
bb
cc

cat file2.txt

site.com
site2.com
site3.com


Expected result will look like:



aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com









share|improve this question























  • Please take a look at: What should I do when someone answers my question?

    – Cyrus
    Jan 13 at 12:42
















0















I have two files named file1.txt and file2.txt, I'm trying to add the prefix to file2.txt from file1.txt in a manner so I can pipe the results to the next utility I'm using.



cat file1.txt

aa
bb
cc

cat file2.txt

site.com
site2.com
site3.com


Expected result will look like:



aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com









share|improve this question























  • Please take a look at: What should I do when someone answers my question?

    – Cyrus
    Jan 13 at 12:42














0












0








0








I have two files named file1.txt and file2.txt, I'm trying to add the prefix to file2.txt from file1.txt in a manner so I can pipe the results to the next utility I'm using.



cat file1.txt

aa
bb
cc

cat file2.txt

site.com
site2.com
site3.com


Expected result will look like:



aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com









share|improve this question














I have two files named file1.txt and file2.txt, I'm trying to add the prefix to file2.txt from file1.txt in a manner so I can pipe the results to the next utility I'm using.



cat file1.txt

aa
bb
cc

cat file2.txt

site.com
site2.com
site3.com


Expected result will look like:



aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com






linux command-line sed






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 12 at 10:00









SamSam

1




1













  • Please take a look at: What should I do when someone answers my question?

    – Cyrus
    Jan 13 at 12:42



















  • Please take a look at: What should I do when someone answers my question?

    – Cyrus
    Jan 13 at 12:42

















Please take a look at: What should I do when someone answers my question?

– Cyrus
Jan 13 at 12:42





Please take a look at: What should I do when someone answers my question?

– Cyrus
Jan 13 at 12:42










2 Answers
2






active

oldest

votes


















1














With bash:



while IFS= read -r line1; do while IFS= read -r line2; do echo "$line1.$line2"; done <file2.txt; done <file1.txt


or



join -j 64 <(sort file1.txt) <(sort file2.txt) -o 1.1,2.1 | sed 's/ /./'


Output:




aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com





share|improve this answer


























  • Thank you for your answer @Cyrus, it works as expected.

    – Sam
    Jan 13 at 12:09



















1
















The easy part is that you can use paste to merge the lines of your files. Its -d option lets you choose a delimiter (here, a .).



The hard part here is the Cartesian product. Borrowing from this answer on SO, we can come up with a command like this:



paste -d '.' 
<(sed -n "$(yes 'p;' | head -n $(wc -l <file2.txt) )" file1.txt)
<(cat $(yes 'file2.txt' | head -n $(wc -l <file1.txt)))


Where we:




  • Combine yes and head to make a sed script that will print each line of file1.txt a number of times equal to the number of lines in file2.txt;

  • Combine yes and head to make cat print file2.txt a number of times equal to the number of lines in file1.txt;

  • Use paste to merge each pair of lines printed by the two process substitutions (<(...)), separated by a ..


Of course you can pipe the result into other commands.

Also, note that you can always pipe the output of commands, even if they are in a loop, as in this other answer you have. E.g. try



while ... do ... done <file | cat -


For convenience, you can define a function and make it available to your environment (e.g. defining it in your .bashrc if you use bash).

An example, here using loops to minimize the need for external tools:



function cart_prod () {
while IFS= read -r line1; do
while IFS= read -r line2; do
printf '%s.%sn' "$line1" "$line2"
done <"$2"
done <"$1"
}


Sample usage:



$ cart_prod file1.txt file2.txt | sort -r
cc.site.com
cc.site3.com
cc.site2.com
bb.site.com
bb.site3.com
bb.site2.com
aa.site.com
aa.site3.com
aa.site2.com





share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1393467%2fadd-prefix-to-each-lines-of-file-from-other-file%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














    With bash:



    while IFS= read -r line1; do while IFS= read -r line2; do echo "$line1.$line2"; done <file2.txt; done <file1.txt


    or



    join -j 64 <(sort file1.txt) <(sort file2.txt) -o 1.1,2.1 | sed 's/ /./'


    Output:




    aa.site.com
    aa.site2.com
    aa.site3.com
    bb.site.com
    bb.site2.com
    bb.site3.com
    cc.site.com
    cc.site2.com
    cc.site3.com





    share|improve this answer


























    • Thank you for your answer @Cyrus, it works as expected.

      – Sam
      Jan 13 at 12:09
















    1














    With bash:



    while IFS= read -r line1; do while IFS= read -r line2; do echo "$line1.$line2"; done <file2.txt; done <file1.txt


    or



    join -j 64 <(sort file1.txt) <(sort file2.txt) -o 1.1,2.1 | sed 's/ /./'


    Output:




    aa.site.com
    aa.site2.com
    aa.site3.com
    bb.site.com
    bb.site2.com
    bb.site3.com
    cc.site.com
    cc.site2.com
    cc.site3.com





    share|improve this answer


























    • Thank you for your answer @Cyrus, it works as expected.

      – Sam
      Jan 13 at 12:09














    1












    1








    1







    With bash:



    while IFS= read -r line1; do while IFS= read -r line2; do echo "$line1.$line2"; done <file2.txt; done <file1.txt


    or



    join -j 64 <(sort file1.txt) <(sort file2.txt) -o 1.1,2.1 | sed 's/ /./'


    Output:




    aa.site.com
    aa.site2.com
    aa.site3.com
    bb.site.com
    bb.site2.com
    bb.site3.com
    cc.site.com
    cc.site2.com
    cc.site3.com





    share|improve this answer















    With bash:



    while IFS= read -r line1; do while IFS= read -r line2; do echo "$line1.$line2"; done <file2.txt; done <file1.txt


    or



    join -j 64 <(sort file1.txt) <(sort file2.txt) -o 1.1,2.1 | sed 's/ /./'


    Output:




    aa.site.com
    aa.site2.com
    aa.site3.com
    bb.site.com
    bb.site2.com
    bb.site3.com
    cc.site.com
    cc.site2.com
    cc.site3.com






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 12 at 11:13

























    answered Jan 12 at 11:08









    CyrusCyrus

    3,79611024




    3,79611024













    • Thank you for your answer @Cyrus, it works as expected.

      – Sam
      Jan 13 at 12:09



















    • Thank you for your answer @Cyrus, it works as expected.

      – Sam
      Jan 13 at 12:09

















    Thank you for your answer @Cyrus, it works as expected.

    – Sam
    Jan 13 at 12:09





    Thank you for your answer @Cyrus, it works as expected.

    – Sam
    Jan 13 at 12:09













    1
















    The easy part is that you can use paste to merge the lines of your files. Its -d option lets you choose a delimiter (here, a .).



    The hard part here is the Cartesian product. Borrowing from this answer on SO, we can come up with a command like this:



    paste -d '.' 
    <(sed -n "$(yes 'p;' | head -n $(wc -l <file2.txt) )" file1.txt)
    <(cat $(yes 'file2.txt' | head -n $(wc -l <file1.txt)))


    Where we:




    • Combine yes and head to make a sed script that will print each line of file1.txt a number of times equal to the number of lines in file2.txt;

    • Combine yes and head to make cat print file2.txt a number of times equal to the number of lines in file1.txt;

    • Use paste to merge each pair of lines printed by the two process substitutions (<(...)), separated by a ..


    Of course you can pipe the result into other commands.

    Also, note that you can always pipe the output of commands, even if they are in a loop, as in this other answer you have. E.g. try



    while ... do ... done <file | cat -


    For convenience, you can define a function and make it available to your environment (e.g. defining it in your .bashrc if you use bash).

    An example, here using loops to minimize the need for external tools:



    function cart_prod () {
    while IFS= read -r line1; do
    while IFS= read -r line2; do
    printf '%s.%sn' "$line1" "$line2"
    done <"$2"
    done <"$1"
    }


    Sample usage:



    $ cart_prod file1.txt file2.txt | sort -r
    cc.site.com
    cc.site3.com
    cc.site2.com
    bb.site.com
    bb.site3.com
    bb.site2.com
    aa.site.com
    aa.site3.com
    aa.site2.com





    share|improve this answer






























      1
















      The easy part is that you can use paste to merge the lines of your files. Its -d option lets you choose a delimiter (here, a .).



      The hard part here is the Cartesian product. Borrowing from this answer on SO, we can come up with a command like this:



      paste -d '.' 
      <(sed -n "$(yes 'p;' | head -n $(wc -l <file2.txt) )" file1.txt)
      <(cat $(yes 'file2.txt' | head -n $(wc -l <file1.txt)))


      Where we:




      • Combine yes and head to make a sed script that will print each line of file1.txt a number of times equal to the number of lines in file2.txt;

      • Combine yes and head to make cat print file2.txt a number of times equal to the number of lines in file1.txt;

      • Use paste to merge each pair of lines printed by the two process substitutions (<(...)), separated by a ..


      Of course you can pipe the result into other commands.

      Also, note that you can always pipe the output of commands, even if they are in a loop, as in this other answer you have. E.g. try



      while ... do ... done <file | cat -


      For convenience, you can define a function and make it available to your environment (e.g. defining it in your .bashrc if you use bash).

      An example, here using loops to minimize the need for external tools:



      function cart_prod () {
      while IFS= read -r line1; do
      while IFS= read -r line2; do
      printf '%s.%sn' "$line1" "$line2"
      done <"$2"
      done <"$1"
      }


      Sample usage:



      $ cart_prod file1.txt file2.txt | sort -r
      cc.site.com
      cc.site3.com
      cc.site2.com
      bb.site.com
      bb.site3.com
      bb.site2.com
      aa.site.com
      aa.site3.com
      aa.site2.com





      share|improve this answer




























        1












        1








        1









        The easy part is that you can use paste to merge the lines of your files. Its -d option lets you choose a delimiter (here, a .).



        The hard part here is the Cartesian product. Borrowing from this answer on SO, we can come up with a command like this:



        paste -d '.' 
        <(sed -n "$(yes 'p;' | head -n $(wc -l <file2.txt) )" file1.txt)
        <(cat $(yes 'file2.txt' | head -n $(wc -l <file1.txt)))


        Where we:




        • Combine yes and head to make a sed script that will print each line of file1.txt a number of times equal to the number of lines in file2.txt;

        • Combine yes and head to make cat print file2.txt a number of times equal to the number of lines in file1.txt;

        • Use paste to merge each pair of lines printed by the two process substitutions (<(...)), separated by a ..


        Of course you can pipe the result into other commands.

        Also, note that you can always pipe the output of commands, even if they are in a loop, as in this other answer you have. E.g. try



        while ... do ... done <file | cat -


        For convenience, you can define a function and make it available to your environment (e.g. defining it in your .bashrc if you use bash).

        An example, here using loops to minimize the need for external tools:



        function cart_prod () {
        while IFS= read -r line1; do
        while IFS= read -r line2; do
        printf '%s.%sn' "$line1" "$line2"
        done <"$2"
        done <"$1"
        }


        Sample usage:



        $ cart_prod file1.txt file2.txt | sort -r
        cc.site.com
        cc.site3.com
        cc.site2.com
        bb.site.com
        bb.site3.com
        bb.site2.com
        aa.site.com
        aa.site3.com
        aa.site2.com





        share|improve this answer

















        The easy part is that you can use paste to merge the lines of your files. Its -d option lets you choose a delimiter (here, a .).



        The hard part here is the Cartesian product. Borrowing from this answer on SO, we can come up with a command like this:



        paste -d '.' 
        <(sed -n "$(yes 'p;' | head -n $(wc -l <file2.txt) )" file1.txt)
        <(cat $(yes 'file2.txt' | head -n $(wc -l <file1.txt)))


        Where we:




        • Combine yes and head to make a sed script that will print each line of file1.txt a number of times equal to the number of lines in file2.txt;

        • Combine yes and head to make cat print file2.txt a number of times equal to the number of lines in file1.txt;

        • Use paste to merge each pair of lines printed by the two process substitutions (<(...)), separated by a ..


        Of course you can pipe the result into other commands.

        Also, note that you can always pipe the output of commands, even if they are in a loop, as in this other answer you have. E.g. try



        while ... do ... done <file | cat -


        For convenience, you can define a function and make it available to your environment (e.g. defining it in your .bashrc if you use bash).

        An example, here using loops to minimize the need for external tools:



        function cart_prod () {
        while IFS= read -r line1; do
        while IFS= read -r line2; do
        printf '%s.%sn' "$line1" "$line2"
        done <"$2"
        done <"$1"
        }


        Sample usage:



        $ cart_prod file1.txt file2.txt | sort -r
        cc.site.com
        cc.site3.com
        cc.site2.com
        bb.site.com
        bb.site3.com
        bb.site2.com
        aa.site.com
        aa.site3.com
        aa.site2.com






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 13 at 11:32

























        answered Jan 12 at 11:08









        fra-sanfra-san

        22614




        22614






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • 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%2fsuperuser.com%2fquestions%2f1393467%2fadd-prefix-to-each-lines-of-file-from-other-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

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

            is 'sed' thread safe

            How to make a Squid Proxy server?