Replace lines in multiple files matching a pattern with lines from another file in order

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












0















I want to replace the lines matching a pattern from multiple files from the lines in order from another file, i have up to 500 txt files, with structure:
test1.txt, test2.txt, test3.txt...



11111
22222
mock
55555
77777


and so reading file one by one we like to replace in first test1.txt file mock line with first line from mock.txt file, in second test2.txt replace mock line with second line from mock.txt file, which has structure like:



mock.txt



randomText1
randomText2
randomText3
randomText4
randomText5


and so on till the last .txt file in filder










share|improve this question

















  • 2





    Welcome to U&L! What have you tried? How did it not work as expected or intended?

    – DopeGhoti
    Jan 29 at 20:32











  • im completely new to this field, i was searching for similar cases but its kinda far from what im looking for

    – Ilya Pribil
    Jan 29 at 20:37















0















I want to replace the lines matching a pattern from multiple files from the lines in order from another file, i have up to 500 txt files, with structure:
test1.txt, test2.txt, test3.txt...



11111
22222
mock
55555
77777


and so reading file one by one we like to replace in first test1.txt file mock line with first line from mock.txt file, in second test2.txt replace mock line with second line from mock.txt file, which has structure like:



mock.txt



randomText1
randomText2
randomText3
randomText4
randomText5


and so on till the last .txt file in filder










share|improve this question

















  • 2





    Welcome to U&L! What have you tried? How did it not work as expected or intended?

    – DopeGhoti
    Jan 29 at 20:32











  • im completely new to this field, i was searching for similar cases but its kinda far from what im looking for

    – Ilya Pribil
    Jan 29 at 20:37













0












0








0








I want to replace the lines matching a pattern from multiple files from the lines in order from another file, i have up to 500 txt files, with structure:
test1.txt, test2.txt, test3.txt...



11111
22222
mock
55555
77777


and so reading file one by one we like to replace in first test1.txt file mock line with first line from mock.txt file, in second test2.txt replace mock line with second line from mock.txt file, which has structure like:



mock.txt



randomText1
randomText2
randomText3
randomText4
randomText5


and so on till the last .txt file in filder










share|improve this question














I want to replace the lines matching a pattern from multiple files from the lines in order from another file, i have up to 500 txt files, with structure:
test1.txt, test2.txt, test3.txt...



11111
22222
mock
55555
77777


and so reading file one by one we like to replace in first test1.txt file mock line with first line from mock.txt file, in second test2.txt replace mock line with second line from mock.txt file, which has structure like:



mock.txt



randomText1
randomText2
randomText3
randomText4
randomText5


and so on till the last .txt file in filder







shell-script shell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 29 at 20:24









Ilya PribilIlya Pribil

1




1







  • 2





    Welcome to U&L! What have you tried? How did it not work as expected or intended?

    – DopeGhoti
    Jan 29 at 20:32











  • im completely new to this field, i was searching for similar cases but its kinda far from what im looking for

    – Ilya Pribil
    Jan 29 at 20:37












  • 2





    Welcome to U&L! What have you tried? How did it not work as expected or intended?

    – DopeGhoti
    Jan 29 at 20:32











  • im completely new to this field, i was searching for similar cases but its kinda far from what im looking for

    – Ilya Pribil
    Jan 29 at 20:37







2




2





Welcome to U&L! What have you tried? How did it not work as expected or intended?

– DopeGhoti
Jan 29 at 20:32





Welcome to U&L! What have you tried? How did it not work as expected or intended?

– DopeGhoti
Jan 29 at 20:32













im completely new to this field, i was searching for similar cases but its kinda far from what im looking for

– Ilya Pribil
Jan 29 at 20:37





im completely new to this field, i was searching for similar cases but its kinda far from what im looking for

– Ilya Pribil
Jan 29 at 20:37










2 Answers
2






active

oldest

votes


















2














With GNU sed, you can use



cat mock.txt | sed -i -e '/mock/R/dev/stdin' -e 'd;' test1..3.txt


The GNU specific R command reads and inserts a single line at a time, unlike the standard r option which would insert all of the contents.



$ head test1..3.txt
==> test1.txt <==
11111
22222
randomText1
55555
77777

==> test2.txt <==
11111
22222
randomText2
55555
77777

==> test3.txt <==
11111
22222
randomText3
55555
77777



R filename


Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is
read. Note that if filename cannot be read, or if its end is reached,
no line is appended, without any error indication.



As with the r command, the special value /dev/stdin is supported for the file name, which reads a line from the standard input.




Note that reading the lines directly from the file like /mock/R mock.txt doesn't work in this context, because the -i option implies the -s option, so that the first line of mock.txt is inserted into every file.






share|improve this answer























  • Thank you, that works well, but if i have lines like this sometimes "222mock 222", and i need just replace 'mock'

    – Ilya Pribil
    Jan 31 at 20:46


















1














With GNU awk (i.e. gawk) you could do:



gawk -i inplace '/mock/ getline < "mock.txt" 1' test1..3.txt


Like sed, gawk offers inplace editing. The above command looks for the regex-pattern mock, and when it finds it, it replaces it with the next line from mock.txt. The 1 is a pattern that always matches, and thus causes gawk to perform its default action, which is to print the (newly) read line. Note that you won't actually see this line during inplace-editing; the print-action is necessary for the output to be recorded.






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



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f497542%2freplace-lines-in-multiple-files-matching-a-pattern-with-lines-from-another-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









    2














    With GNU sed, you can use



    cat mock.txt | sed -i -e '/mock/R/dev/stdin' -e 'd;' test1..3.txt


    The GNU specific R command reads and inserts a single line at a time, unlike the standard r option which would insert all of the contents.



    $ head test1..3.txt
    ==> test1.txt <==
    11111
    22222
    randomText1
    55555
    77777

    ==> test2.txt <==
    11111
    22222
    randomText2
    55555
    77777

    ==> test3.txt <==
    11111
    22222
    randomText3
    55555
    77777



    R filename


    Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is
    read. Note that if filename cannot be read, or if its end is reached,
    no line is appended, without any error indication.



    As with the r command, the special value /dev/stdin is supported for the file name, which reads a line from the standard input.




    Note that reading the lines directly from the file like /mock/R mock.txt doesn't work in this context, because the -i option implies the -s option, so that the first line of mock.txt is inserted into every file.






    share|improve this answer























    • Thank you, that works well, but if i have lines like this sometimes "222mock 222", and i need just replace 'mock'

      – Ilya Pribil
      Jan 31 at 20:46















    2














    With GNU sed, you can use



    cat mock.txt | sed -i -e '/mock/R/dev/stdin' -e 'd;' test1..3.txt


    The GNU specific R command reads and inserts a single line at a time, unlike the standard r option which would insert all of the contents.



    $ head test1..3.txt
    ==> test1.txt <==
    11111
    22222
    randomText1
    55555
    77777

    ==> test2.txt <==
    11111
    22222
    randomText2
    55555
    77777

    ==> test3.txt <==
    11111
    22222
    randomText3
    55555
    77777



    R filename


    Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is
    read. Note that if filename cannot be read, or if its end is reached,
    no line is appended, without any error indication.



    As with the r command, the special value /dev/stdin is supported for the file name, which reads a line from the standard input.




    Note that reading the lines directly from the file like /mock/R mock.txt doesn't work in this context, because the -i option implies the -s option, so that the first line of mock.txt is inserted into every file.






    share|improve this answer























    • Thank you, that works well, but if i have lines like this sometimes "222mock 222", and i need just replace 'mock'

      – Ilya Pribil
      Jan 31 at 20:46













    2












    2








    2







    With GNU sed, you can use



    cat mock.txt | sed -i -e '/mock/R/dev/stdin' -e 'd;' test1..3.txt


    The GNU specific R command reads and inserts a single line at a time, unlike the standard r option which would insert all of the contents.



    $ head test1..3.txt
    ==> test1.txt <==
    11111
    22222
    randomText1
    55555
    77777

    ==> test2.txt <==
    11111
    22222
    randomText2
    55555
    77777

    ==> test3.txt <==
    11111
    22222
    randomText3
    55555
    77777



    R filename


    Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is
    read. Note that if filename cannot be read, or if its end is reached,
    no line is appended, without any error indication.



    As with the r command, the special value /dev/stdin is supported for the file name, which reads a line from the standard input.




    Note that reading the lines directly from the file like /mock/R mock.txt doesn't work in this context, because the -i option implies the -s option, so that the first line of mock.txt is inserted into every file.






    share|improve this answer













    With GNU sed, you can use



    cat mock.txt | sed -i -e '/mock/R/dev/stdin' -e 'd;' test1..3.txt


    The GNU specific R command reads and inserts a single line at a time, unlike the standard r option which would insert all of the contents.



    $ head test1..3.txt
    ==> test1.txt <==
    11111
    22222
    randomText1
    55555
    77777

    ==> test2.txt <==
    11111
    22222
    randomText2
    55555
    77777

    ==> test3.txt <==
    11111
    22222
    randomText3
    55555
    77777



    R filename


    Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is
    read. Note that if filename cannot be read, or if its end is reached,
    no line is appended, without any error indication.



    As with the r command, the special value /dev/stdin is supported for the file name, which reads a line from the standard input.




    Note that reading the lines directly from the file like /mock/R mock.txt doesn't work in this context, because the -i option implies the -s option, so that the first line of mock.txt is inserted into every file.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 29 at 20:48









    steeldriversteeldriver

    36.2k35286




    36.2k35286












    • Thank you, that works well, but if i have lines like this sometimes "222mock 222", and i need just replace 'mock'

      – Ilya Pribil
      Jan 31 at 20:46

















    • Thank you, that works well, but if i have lines like this sometimes "222mock 222", and i need just replace 'mock'

      – Ilya Pribil
      Jan 31 at 20:46
















    Thank you, that works well, but if i have lines like this sometimes "222mock 222", and i need just replace 'mock'

    – Ilya Pribil
    Jan 31 at 20:46





    Thank you, that works well, but if i have lines like this sometimes "222mock 222", and i need just replace 'mock'

    – Ilya Pribil
    Jan 31 at 20:46













    1














    With GNU awk (i.e. gawk) you could do:



    gawk -i inplace '/mock/ getline < "mock.txt" 1' test1..3.txt


    Like sed, gawk offers inplace editing. The above command looks for the regex-pattern mock, and when it finds it, it replaces it with the next line from mock.txt. The 1 is a pattern that always matches, and thus causes gawk to perform its default action, which is to print the (newly) read line. Note that you won't actually see this line during inplace-editing; the print-action is necessary for the output to be recorded.






    share|improve this answer





























      1














      With GNU awk (i.e. gawk) you could do:



      gawk -i inplace '/mock/ getline < "mock.txt" 1' test1..3.txt


      Like sed, gawk offers inplace editing. The above command looks for the regex-pattern mock, and when it finds it, it replaces it with the next line from mock.txt. The 1 is a pattern that always matches, and thus causes gawk to perform its default action, which is to print the (newly) read line. Note that you won't actually see this line during inplace-editing; the print-action is necessary for the output to be recorded.






      share|improve this answer



























        1












        1








        1







        With GNU awk (i.e. gawk) you could do:



        gawk -i inplace '/mock/ getline < "mock.txt" 1' test1..3.txt


        Like sed, gawk offers inplace editing. The above command looks for the regex-pattern mock, and when it finds it, it replaces it with the next line from mock.txt. The 1 is a pattern that always matches, and thus causes gawk to perform its default action, which is to print the (newly) read line. Note that you won't actually see this line during inplace-editing; the print-action is necessary for the output to be recorded.






        share|improve this answer















        With GNU awk (i.e. gawk) you could do:



        gawk -i inplace '/mock/ getline < "mock.txt" 1' test1..3.txt


        Like sed, gawk offers inplace editing. The above command looks for the regex-pattern mock, and when it finds it, it replaces it with the next line from mock.txt. The 1 is a pattern that always matches, and thus causes gawk to perform its default action, which is to print the (newly) read line. Note that you won't actually see this line during inplace-editing; the print-action is necessary for the output to be recorded.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 29 at 22:53

























        answered Jan 29 at 21:43









        ozzyozzy

        74715




        74715



























            draft saved

            draft discarded
















































            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%2f497542%2freplace-lines-in-multiple-files-matching-a-pattern-with-lines-from-another-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 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?