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
    Jan 27 at 1:15











  • 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
    Jan 27 at 1:32











  • 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
    Jan 27 at 2:15


















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
    Jan 27 at 1:15











  • 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
    Jan 27 at 1:32











  • 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
    Jan 27 at 2:15
















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 Jan 27 at 2:11







abdan

















asked Jan 27 at 1:05









abdanabdan

314




314













  • What is the expected output?

    – Nasir Riley
    Jan 27 at 1:15











  • 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
    Jan 27 at 1:32











  • 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
    Jan 27 at 2:15





















  • What is the expected output?

    – Nasir Riley
    Jan 27 at 1:15











  • 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
    Jan 27 at 1:32











  • 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
    Jan 27 at 2:15



















What is the expected output?

– Nasir Riley
Jan 27 at 1:15





What is the expected output?

– Nasir Riley
Jan 27 at 1:15













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
Jan 27 at 1:32





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
Jan 27 at 1:32













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
Jan 27 at 2:15







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
Jan 27 at 2:15












1 Answer
1






active

oldest

votes


















1














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



















  • 1





    Or newish gawk could do the change itself: awk -i inplace -vIGNORECASE=1 '/copy/&&++n==3 {sub(/copy/,"No-&")} 1' file (for other awk you could do [Cc] etc and the awk ... file >temp && mv temp file dance)

    – dave_thompson_085
    Jan 27 at 7:41













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









1














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



















  • 1





    Or newish gawk could do the change itself: awk -i inplace -vIGNORECASE=1 '/copy/&&++n==3 {sub(/copy/,"No-&")} 1' file (for other awk you could do [Cc] etc and the awk ... file >temp && mv temp file dance)

    – dave_thompson_085
    Jan 27 at 7:41


















1














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



















  • 1





    Or newish gawk could do the change itself: awk -i inplace -vIGNORECASE=1 '/copy/&&++n==3 {sub(/copy/,"No-&")} 1' file (for other awk you could do [Cc] etc and the awk ... file >temp && mv temp file dance)

    – dave_thompson_085
    Jan 27 at 7:41
















1












1








1







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 Jan 27 at 1:53









Jeff SchallerJeff Schaller

40.9k1056130




40.9k1056130








  • 1





    Or newish gawk could do the change itself: awk -i inplace -vIGNORECASE=1 '/copy/&&++n==3 {sub(/copy/,"No-&")} 1' file (for other awk you could do [Cc] etc and the awk ... file >temp && mv temp file dance)

    – dave_thompson_085
    Jan 27 at 7:41
















  • 1





    Or newish gawk could do the change itself: awk -i inplace -vIGNORECASE=1 '/copy/&&++n==3 {sub(/copy/,"No-&")} 1' file (for other awk you could do [Cc] etc and the awk ... file >temp && mv temp file dance)

    – dave_thompson_085
    Jan 27 at 7:41










1




1





Or newish gawk could do the change itself: awk -i inplace -vIGNORECASE=1 '/copy/&&++n==3 {sub(/copy/,"No-&")} 1' file (for other awk you could do [Cc] etc and the awk ... file >temp && mv temp file dance)

– dave_thompson_085
Jan 27 at 7:41







Or newish gawk could do the change itself: awk -i inplace -vIGNORECASE=1 '/copy/&&++n==3 {sub(/copy/,"No-&")} 1' file (for other awk you could do [Cc] etc and the awk ... file >temp && mv temp file dance)

– dave_thompson_085
Jan 27 at 7:41




















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

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?