How can sed replace in the range of nth line regex specified?












0















I am to replace using sed the 3th (or nth to be more general) occurrence in respective line with specified match regex.
so far as i can is only to do the first:



$cat file   
# Golden dictionary is a versatile multi purpose reference
# Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# Copyright (C) 2015-2016 C


so far as i can is only



$ cat file| sed -E '0,/copy/I s//No-&/'

# Golden dictionary is a versatile multi purpose reference
# No-Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# Copyright (C) 2015-2016 C


How to do so by such way for the 3th match only?










share|improve this question

























  • What is the expected output?

    – Nasir Riley
    2 hours ago











  • You want the third line that contains the text to be prefixed with No-? Or the third occurrence no matter where it is?

    – Jeff Schaller
    1 hour ago











  • the third occurrence of respective line, no matter to many occurrences in a line, if 2nd line is # Copyright (C) 2004-2008 Copyright (C) 2009 Copyright (C) 2018 # Copyright (C) 2019 A ,still must find 4th line Copyright (C) 2015-2016 C to be replace then

    – abdan
    1 hour ago


















0















I am to replace using sed the 3th (or nth to be more general) occurrence in respective line with specified match regex.
so far as i can is only to do the first:



$cat file   
# Golden dictionary is a versatile multi purpose reference
# Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# Copyright (C) 2015-2016 C


so far as i can is only



$ cat file| sed -E '0,/copy/I s//No-&/'

# Golden dictionary is a versatile multi purpose reference
# No-Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# Copyright (C) 2015-2016 C


How to do so by such way for the 3th match only?










share|improve this question

























  • What is the expected output?

    – Nasir Riley
    2 hours ago











  • You want the third line that contains the text to be prefixed with No-? Or the third occurrence no matter where it is?

    – Jeff Schaller
    1 hour ago











  • the third occurrence of respective line, no matter to many occurrences in a line, if 2nd line is # Copyright (C) 2004-2008 Copyright (C) 2009 Copyright (C) 2018 # Copyright (C) 2019 A ,still must find 4th line Copyright (C) 2015-2016 C to be replace then

    – abdan
    1 hour ago
















0












0








0








I am to replace using sed the 3th (or nth to be more general) occurrence in respective line with specified match regex.
so far as i can is only to do the first:



$cat file   
# Golden dictionary is a versatile multi purpose reference
# Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# Copyright (C) 2015-2016 C


so far as i can is only



$ cat file| sed -E '0,/copy/I s//No-&/'

# Golden dictionary is a versatile multi purpose reference
# No-Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# Copyright (C) 2015-2016 C


How to do so by such way for the 3th match only?










share|improve this question
















I am to replace using sed the 3th (or nth to be more general) occurrence in respective line with specified match regex.
so far as i can is only to do the first:



$cat file   
# Golden dictionary is a versatile multi purpose reference
# Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# Copyright (C) 2015-2016 C


so far as i can is only



$ cat file| sed -E '0,/copy/I s//No-&/'

# Golden dictionary is a versatile multi purpose reference
# No-Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# Copyright (C) 2015-2016 C


How to do so by such way for the 3th match only?







sed regular-expression gnu






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago







abdan

















asked 2 hours ago









abdanabdan

213




213













  • What is the expected output?

    – Nasir Riley
    2 hours ago











  • You want the third line that contains the text to be prefixed with No-? Or the third occurrence no matter where it is?

    – Jeff Schaller
    1 hour ago











  • the third occurrence of respective line, no matter to many occurrences in a line, if 2nd line is # Copyright (C) 2004-2008 Copyright (C) 2009 Copyright (C) 2018 # Copyright (C) 2019 A ,still must find 4th line Copyright (C) 2015-2016 C to be replace then

    – abdan
    1 hour ago





















  • What is the expected output?

    – Nasir Riley
    2 hours ago











  • You want the third line that contains the text to be prefixed with No-? Or the third occurrence no matter where it is?

    – Jeff Schaller
    1 hour ago











  • the third occurrence of respective line, no matter to many occurrences in a line, if 2nd line is # Copyright (C) 2004-2008 Copyright (C) 2009 Copyright (C) 2018 # Copyright (C) 2019 A ,still must find 4th line Copyright (C) 2015-2016 C to be replace then

    – abdan
    1 hour ago



















What is the expected output?

– Nasir Riley
2 hours ago





What is the expected output?

– Nasir Riley
2 hours ago













You want the third line that contains the text to be prefixed with No-? Or the third occurrence no matter where it is?

– Jeff Schaller
1 hour ago





You want the third line that contains the text to be prefixed with No-? Or the third occurrence no matter where it is?

– Jeff Schaller
1 hour ago













the third occurrence of respective line, no matter to many occurrences in a line, if 2nd line is # Copyright (C) 2004-2008 Copyright (C) 2009 Copyright (C) 2018 # Copyright (C) 2019 A ,still must find 4th line Copyright (C) 2015-2016 C to be replace then

– abdan
1 hour ago







the third occurrence of respective line, no matter to many occurrences in a line, if 2nd line is # Copyright (C) 2004-2008 Copyright (C) 2009 Copyright (C) 2018 # Copyright (C) 2019 A ,still must find 4th line Copyright (C) 2015-2016 C to be replace then

– abdan
1 hour ago












1 Answer
1






active

oldest

votes


















0














With ed, prefixing the 3rd case-insensitive "copy" with "No-" would be:



ed -s file <<< $'/[Cc][Oo][Pp][Yy]/n//n//ns//No-&/nwnq' > /dev/null


The commands are:





  • /[Cc][Oo][Pp][Yy]/ -- search, in a manually case-insensitive way for "copy:


  • // -- repeat the search, twice


  • s//No-&/ -- replace the last match with the "No-" prefix


  • w -- write the change file to disk


  • q -- quit ed


With sed, you could do some pre-work to find the line number to change:



sed -i $(grep -in copy file |awk -F: 'NR==3 { print $1 }')'s/copy/No-&/i' input


Working from left to right,





  • -i -- GNU sed's in-place edit option


  • $( ... ) -- find the line number of the 3rd match of "copy"



    • grep -in copy file -- find the word "copy", case-insensitively in file and report the line numbers of the matches


    • awk -F: 'NR==3 { print $1 }' -- on line 3 of grep's output, split the line on colons and report back column 1 (the line number)




  • s/copy/No-&/i -- replace "copy" with "No-copy", case-insensitively






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%2f496953%2fhow-can-sed-replace-in-the-range-of-nth-line-regex-specified%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    With ed, prefixing the 3rd case-insensitive "copy" with "No-" would be:



    ed -s file <<< $'/[Cc][Oo][Pp][Yy]/n//n//ns//No-&/nwnq' > /dev/null


    The commands are:





    • /[Cc][Oo][Pp][Yy]/ -- search, in a manually case-insensitive way for "copy:


    • // -- repeat the search, twice


    • s//No-&/ -- replace the last match with the "No-" prefix


    • w -- write the change file to disk


    • q -- quit ed


    With sed, you could do some pre-work to find the line number to change:



    sed -i $(grep -in copy file |awk -F: 'NR==3 { print $1 }')'s/copy/No-&/i' input


    Working from left to right,





    • -i -- GNU sed's in-place edit option


    • $( ... ) -- find the line number of the 3rd match of "copy"



      • grep -in copy file -- find the word "copy", case-insensitively in file and report the line numbers of the matches


      • awk -F: 'NR==3 { print $1 }' -- on line 3 of grep's output, split the line on colons and report back column 1 (the line number)




    • s/copy/No-&/i -- replace "copy" with "No-copy", case-insensitively






    share|improve this answer




























      0














      With ed, prefixing the 3rd case-insensitive "copy" with "No-" would be:



      ed -s file <<< $'/[Cc][Oo][Pp][Yy]/n//n//ns//No-&/nwnq' > /dev/null


      The commands are:





      • /[Cc][Oo][Pp][Yy]/ -- search, in a manually case-insensitive way for "copy:


      • // -- repeat the search, twice


      • s//No-&/ -- replace the last match with the "No-" prefix


      • w -- write the change file to disk


      • q -- quit ed


      With sed, you could do some pre-work to find the line number to change:



      sed -i $(grep -in copy file |awk -F: 'NR==3 { print $1 }')'s/copy/No-&/i' input


      Working from left to right,





      • -i -- GNU sed's in-place edit option


      • $( ... ) -- find the line number of the 3rd match of "copy"



        • grep -in copy file -- find the word "copy", case-insensitively in file and report the line numbers of the matches


        • awk -F: 'NR==3 { print $1 }' -- on line 3 of grep's output, split the line on colons and report back column 1 (the line number)




      • s/copy/No-&/i -- replace "copy" with "No-copy", case-insensitively






      share|improve this answer


























        0












        0








        0







        With ed, prefixing the 3rd case-insensitive "copy" with "No-" would be:



        ed -s file <<< $'/[Cc][Oo][Pp][Yy]/n//n//ns//No-&/nwnq' > /dev/null


        The commands are:





        • /[Cc][Oo][Pp][Yy]/ -- search, in a manually case-insensitive way for "copy:


        • // -- repeat the search, twice


        • s//No-&/ -- replace the last match with the "No-" prefix


        • w -- write the change file to disk


        • q -- quit ed


        With sed, you could do some pre-work to find the line number to change:



        sed -i $(grep -in copy file |awk -F: 'NR==3 { print $1 }')'s/copy/No-&/i' input


        Working from left to right,





        • -i -- GNU sed's in-place edit option


        • $( ... ) -- find the line number of the 3rd match of "copy"



          • grep -in copy file -- find the word "copy", case-insensitively in file and report the line numbers of the matches


          • awk -F: 'NR==3 { print $1 }' -- on line 3 of grep's output, split the line on colons and report back column 1 (the line number)




        • s/copy/No-&/i -- replace "copy" with "No-copy", case-insensitively






        share|improve this answer













        With ed, prefixing the 3rd case-insensitive "copy" with "No-" would be:



        ed -s file <<< $'/[Cc][Oo][Pp][Yy]/n//n//ns//No-&/nwnq' > /dev/null


        The commands are:





        • /[Cc][Oo][Pp][Yy]/ -- search, in a manually case-insensitive way for "copy:


        • // -- repeat the search, twice


        • s//No-&/ -- replace the last match with the "No-" prefix


        • w -- write the change file to disk


        • q -- quit ed


        With sed, you could do some pre-work to find the line number to change:



        sed -i $(grep -in copy file |awk -F: 'NR==3 { print $1 }')'s/copy/No-&/i' input


        Working from left to right,





        • -i -- GNU sed's in-place edit option


        • $( ... ) -- find the line number of the 3rd match of "copy"



          • grep -in copy file -- find the word "copy", case-insensitively in file and report the line numbers of the matches


          • awk -F: 'NR==3 { print $1 }' -- on line 3 of grep's output, split the line on colons and report back column 1 (the line number)




        • s/copy/No-&/i -- replace "copy" with "No-copy", case-insensitively







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        Jeff SchallerJeff Schaller

        40.1k1054126




        40.1k1054126






























            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%2f496953%2fhow-can-sed-replace-in-the-range-of-nth-line-regex-specified%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

            Loup dans la culture

            How to solve the problem of ntp “Unable to contact time server” from KDE?

            ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux