How can sed replace in the range of nth line regex specified?
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
add a comment |
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
What is the expected output?
– Nasir Riley
Jan 27 at 1:15
You want the third line that contains the text to be prefixed withNo-
? 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
add a comment |
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
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
sed regular-expression gnu
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 withNo-
? 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
add a comment |
What is the expected output?
– Nasir Riley
Jan 27 at 1:15
You want the third line that contains the text to be prefixed withNo-
? 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
add a comment |
1 Answer
1
active
oldest
votes
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 infile
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
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 theawk ... file >temp && mv temp file
dance)
– dave_thompson_085
Jan 27 at 7:41
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 infile
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
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 theawk ... file >temp && mv temp file
dance)
– dave_thompson_085
Jan 27 at 7:41
add a comment |
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 infile
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
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 theawk ... file >temp && mv temp file
dance)
– dave_thompson_085
Jan 27 at 7:41
add a comment |
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 infile
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
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 infile
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
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 theawk ... file >temp && mv temp file
dance)
– dave_thompson_085
Jan 27 at 7:41
add a comment |
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 theawk ... 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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