replace a string with sed from specific lines
up vote
1
down vote
favorite
I do know how to replace a string from a specific line such as:
sed -i "<line number>s/<old string>/<new string>/g" <file name>
however I do not know how to replace a string from multiple lines for example from a line number 1 then 10 and then 100
sed
add a comment |
up vote
1
down vote
favorite
I do know how to replace a string from a specific line such as:
sed -i "<line number>s/<old string>/<new string>/g" <file name>
however I do not know how to replace a string from multiple lines for example from a line number 1 then 10 and then 100
sed
sed doesn't support substitution by set of arbitrary numbers
– RomanPerekhrest
Dec 8 at 21:33
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I do know how to replace a string from a specific line such as:
sed -i "<line number>s/<old string>/<new string>/g" <file name>
however I do not know how to replace a string from multiple lines for example from a line number 1 then 10 and then 100
sed
I do know how to replace a string from a specific line such as:
sed -i "<line number>s/<old string>/<new string>/g" <file name>
however I do not know how to replace a string from multiple lines for example from a line number 1 then 10 and then 100
sed
sed
edited Dec 8 at 20:39
Jeff Schaller
38.1k1053124
38.1k1053124
asked Dec 8 at 20:37
Dimitris Mintis
425
425
sed doesn't support substitution by set of arbitrary numbers
– RomanPerekhrest
Dec 8 at 21:33
add a comment |
sed doesn't support substitution by set of arbitrary numbers
– RomanPerekhrest
Dec 8 at 21:33
sed doesn't support substitution by set of arbitrary numbers
– RomanPerekhrest
Dec 8 at 21:33
sed doesn't support substitution by set of arbitrary numbers
– RomanPerekhrest
Dec 8 at 21:33
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
down vote
Here is an awk answer.
awk 'NR == 1 || NR == 10 || NR == 100 {gsub(/old/,"new")}; {print}' <file name>
Explanation
NR == 1 || NR == 10 || NR == 100
: only do the following commands on one of these lines.
gsub(/old/,"new")
: substitute/old/
withnew
.
{print}
: regardless of what line you are on, print the line.
add a comment |
up vote
2
down vote
As far as I know, sed addresses may only consist of a single line, or a range of lines.
However you could cobble something together using sed -f -
to read commands from standard input, together with your shell. For example:
printf '%ds/<old string>/<new string>/gn' {1,10,100} | sed -f - file
Brilliant solution. Alternative to avoid the pipe:sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1
– George Vasiliou
Dec 9 at 17:36
add a comment |
up vote
1
down vote
An alternative directly with sed:
sed '1b1; 10b1; 100b1; b ;:1;s/<old string>/<new string>/g' <file name>
If the line number matches either 1, 10, or 100, branch to label 1; on other lines, just branch to the end (which, by default, prints the line).
somewhat automated:
sed -e $(printf '%sb1;' 1 10 100) -e 'b; :1;s/<old>/<new>/g' <file name>
POSIXly (with default IFS):
sed $(printf -- '-e %sb1 ' 1 10 100) -e 'b' -e':1' -e 's/<old>/<new>/g'
add a comment |
up vote
0
down vote
Well, how about:
sed -e '
/something_identifying_startline/,/something_identifying_endline/{
s/dog/cat/
s/black/white/
}
Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).
– Sparhawk
Dec 8 at 22:03
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%2f486840%2freplace-a-string-with-sed-from-specific-lines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Here is an awk answer.
awk 'NR == 1 || NR == 10 || NR == 100 {gsub(/old/,"new")}; {print}' <file name>
Explanation
NR == 1 || NR == 10 || NR == 100
: only do the following commands on one of these lines.
gsub(/old/,"new")
: substitute/old/
withnew
.
{print}
: regardless of what line you are on, print the line.
add a comment |
up vote
2
down vote
Here is an awk answer.
awk 'NR == 1 || NR == 10 || NR == 100 {gsub(/old/,"new")}; {print}' <file name>
Explanation
NR == 1 || NR == 10 || NR == 100
: only do the following commands on one of these lines.
gsub(/old/,"new")
: substitute/old/
withnew
.
{print}
: regardless of what line you are on, print the line.
add a comment |
up vote
2
down vote
up vote
2
down vote
Here is an awk answer.
awk 'NR == 1 || NR == 10 || NR == 100 {gsub(/old/,"new")}; {print}' <file name>
Explanation
NR == 1 || NR == 10 || NR == 100
: only do the following commands on one of these lines.
gsub(/old/,"new")
: substitute/old/
withnew
.
{print}
: regardless of what line you are on, print the line.
Here is an awk answer.
awk 'NR == 1 || NR == 10 || NR == 100 {gsub(/old/,"new")}; {print}' <file name>
Explanation
NR == 1 || NR == 10 || NR == 100
: only do the following commands on one of these lines.
gsub(/old/,"new")
: substitute/old/
withnew
.
{print}
: regardless of what line you are on, print the line.
answered Dec 8 at 22:06
Sparhawk
9,20863890
9,20863890
add a comment |
add a comment |
up vote
2
down vote
As far as I know, sed addresses may only consist of a single line, or a range of lines.
However you could cobble something together using sed -f -
to read commands from standard input, together with your shell. For example:
printf '%ds/<old string>/<new string>/gn' {1,10,100} | sed -f - file
Brilliant solution. Alternative to avoid the pipe:sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1
– George Vasiliou
Dec 9 at 17:36
add a comment |
up vote
2
down vote
As far as I know, sed addresses may only consist of a single line, or a range of lines.
However you could cobble something together using sed -f -
to read commands from standard input, together with your shell. For example:
printf '%ds/<old string>/<new string>/gn' {1,10,100} | sed -f - file
Brilliant solution. Alternative to avoid the pipe:sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1
– George Vasiliou
Dec 9 at 17:36
add a comment |
up vote
2
down vote
up vote
2
down vote
As far as I know, sed addresses may only consist of a single line, or a range of lines.
However you could cobble something together using sed -f -
to read commands from standard input, together with your shell. For example:
printf '%ds/<old string>/<new string>/gn' {1,10,100} | sed -f - file
As far as I know, sed addresses may only consist of a single line, or a range of lines.
However you could cobble something together using sed -f -
to read commands from standard input, together with your shell. For example:
printf '%ds/<old string>/<new string>/gn' {1,10,100} | sed -f - file
answered Dec 8 at 23:16
steeldriver
34.1k35083
34.1k35083
Brilliant solution. Alternative to avoid the pipe:sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1
– George Vasiliou
Dec 9 at 17:36
add a comment |
Brilliant solution. Alternative to avoid the pipe:sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1
– George Vasiliou
Dec 9 at 17:36
Brilliant solution. Alternative to avoid the pipe:
sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1
– George Vasiliou
Dec 9 at 17:36
Brilliant solution. Alternative to avoid the pipe:
sed -f <(printf '%ds/line/LINE/gn' {1,10,100}) file1
– George Vasiliou
Dec 9 at 17:36
add a comment |
up vote
1
down vote
An alternative directly with sed:
sed '1b1; 10b1; 100b1; b ;:1;s/<old string>/<new string>/g' <file name>
If the line number matches either 1, 10, or 100, branch to label 1; on other lines, just branch to the end (which, by default, prints the line).
somewhat automated:
sed -e $(printf '%sb1;' 1 10 100) -e 'b; :1;s/<old>/<new>/g' <file name>
POSIXly (with default IFS):
sed $(printf -- '-e %sb1 ' 1 10 100) -e 'b' -e':1' -e 's/<old>/<new>/g'
add a comment |
up vote
1
down vote
An alternative directly with sed:
sed '1b1; 10b1; 100b1; b ;:1;s/<old string>/<new string>/g' <file name>
If the line number matches either 1, 10, or 100, branch to label 1; on other lines, just branch to the end (which, by default, prints the line).
somewhat automated:
sed -e $(printf '%sb1;' 1 10 100) -e 'b; :1;s/<old>/<new>/g' <file name>
POSIXly (with default IFS):
sed $(printf -- '-e %sb1 ' 1 10 100) -e 'b' -e':1' -e 's/<old>/<new>/g'
add a comment |
up vote
1
down vote
up vote
1
down vote
An alternative directly with sed:
sed '1b1; 10b1; 100b1; b ;:1;s/<old string>/<new string>/g' <file name>
If the line number matches either 1, 10, or 100, branch to label 1; on other lines, just branch to the end (which, by default, prints the line).
somewhat automated:
sed -e $(printf '%sb1;' 1 10 100) -e 'b; :1;s/<old>/<new>/g' <file name>
POSIXly (with default IFS):
sed $(printf -- '-e %sb1 ' 1 10 100) -e 'b' -e':1' -e 's/<old>/<new>/g'
An alternative directly with sed:
sed '1b1; 10b1; 100b1; b ;:1;s/<old string>/<new string>/g' <file name>
If the line number matches either 1, 10, or 100, branch to label 1; on other lines, just branch to the end (which, by default, prints the line).
somewhat automated:
sed -e $(printf '%sb1;' 1 10 100) -e 'b; :1;s/<old>/<new>/g' <file name>
POSIXly (with default IFS):
sed $(printf -- '-e %sb1 ' 1 10 100) -e 'b' -e':1' -e 's/<old>/<new>/g'
edited Dec 9 at 1:05
Jeff Schaller
38.1k1053124
38.1k1053124
answered Dec 9 at 0:30
Isaac
11k11648
11k11648
add a comment |
add a comment |
up vote
0
down vote
Well, how about:
sed -e '
/something_identifying_startline/,/something_identifying_endline/{
s/dog/cat/
s/black/white/
}
Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).
– Sparhawk
Dec 8 at 22:03
add a comment |
up vote
0
down vote
Well, how about:
sed -e '
/something_identifying_startline/,/something_identifying_endline/{
s/dog/cat/
s/black/white/
}
Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).
– Sparhawk
Dec 8 at 22:03
add a comment |
up vote
0
down vote
up vote
0
down vote
Well, how about:
sed -e '
/something_identifying_startline/,/something_identifying_endline/{
s/dog/cat/
s/black/white/
}
Well, how about:
sed -e '
/something_identifying_startline/,/something_identifying_endline/{
s/dog/cat/
s/black/white/
}
answered Dec 8 at 22:00
Don W
1
1
Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).
– Sparhawk
Dec 8 at 22:03
add a comment |
Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).
– Sparhawk
Dec 8 at 22:03
Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).
– Sparhawk
Dec 8 at 22:03
Welsome to U/L and thanks for posting an answer. However, this won't let you select multiple ranges (nor select by line number).
– Sparhawk
Dec 8 at 22:03
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f486840%2freplace-a-string-with-sed-from-specific-lines%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
sed doesn't support substitution by set of arbitrary numbers
– RomanPerekhrest
Dec 8 at 21:33