How to search and replace with sed in a line with variable content in it?
I tried to use sed to edit some part of a specific line in a text file and save the change to the original file. OK I now understand that this is not the way sed directly works.
But how can I use sed in a script to edit the part of a line with variable content in it?
I already used sed to change complete lines or used lines as trigger to cut and insert to a new file. But this is different :)
I think what I need is
sed '/baz/s/foo/bar/g'
sed '/beginning of line/s/static text/new static text/g'
This gives me the right output but how can I save it to the source file?
I understand that sed outputs to stoud but when I add a ">> outpufile" it gives me an error message
sed: -e Ausdruck #1, Zeichen 55: Unbekannte Option für `s'
Between static text and new static text is some content that changes from time to time so
sed -n '/static text/new static text/,$p' tmp >> source
will work only for as long as the source content hasn't changed.
Any suggestions?
Greetz
shell-script sed
add a comment |
I tried to use sed to edit some part of a specific line in a text file and save the change to the original file. OK I now understand that this is not the way sed directly works.
But how can I use sed in a script to edit the part of a line with variable content in it?
I already used sed to change complete lines or used lines as trigger to cut and insert to a new file. But this is different :)
I think what I need is
sed '/baz/s/foo/bar/g'
sed '/beginning of line/s/static text/new static text/g'
This gives me the right output but how can I save it to the source file?
I understand that sed outputs to stoud but when I add a ">> outpufile" it gives me an error message
sed: -e Ausdruck #1, Zeichen 55: Unbekannte Option für `s'
Between static text and new static text is some content that changes from time to time so
sed -n '/static text/new static text/,$p' tmp >> source
will work only for as long as the source content hasn't changed.
Any suggestions?
Greetz
shell-script sed
add a comment |
I tried to use sed to edit some part of a specific line in a text file and save the change to the original file. OK I now understand that this is not the way sed directly works.
But how can I use sed in a script to edit the part of a line with variable content in it?
I already used sed to change complete lines or used lines as trigger to cut and insert to a new file. But this is different :)
I think what I need is
sed '/baz/s/foo/bar/g'
sed '/beginning of line/s/static text/new static text/g'
This gives me the right output but how can I save it to the source file?
I understand that sed outputs to stoud but when I add a ">> outpufile" it gives me an error message
sed: -e Ausdruck #1, Zeichen 55: Unbekannte Option für `s'
Between static text and new static text is some content that changes from time to time so
sed -n '/static text/new static text/,$p' tmp >> source
will work only for as long as the source content hasn't changed.
Any suggestions?
Greetz
shell-script sed
I tried to use sed to edit some part of a specific line in a text file and save the change to the original file. OK I now understand that this is not the way sed directly works.
But how can I use sed in a script to edit the part of a line with variable content in it?
I already used sed to change complete lines or used lines as trigger to cut and insert to a new file. But this is different :)
I think what I need is
sed '/baz/s/foo/bar/g'
sed '/beginning of line/s/static text/new static text/g'
This gives me the right output but how can I save it to the source file?
I understand that sed outputs to stoud but when I add a ">> outpufile" it gives me an error message
sed: -e Ausdruck #1, Zeichen 55: Unbekannte Option für `s'
Between static text and new static text is some content that changes from time to time so
sed -n '/static text/new static text/,$p' tmp >> source
will work only for as long as the source content hasn't changed.
Any suggestions?
Greetz
shell-script sed
shell-script sed
asked 2 hours ago
diggidrediggidre
384
384
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
micha@linux-micha: /tmp
$ echo 'alpha bravo charlie' > blah.txt
micha@linux-micha: /tmp
$ cat blah.txt
alpha bravo charlie
micha@linux-micha: /tmp
$ sed -i 's/bravo/delta/' blah.txt
micha@linux-micha: /tmp
$ cat blah.txt
alpha delta charlie
OK it's a bit late here... I'm confused ^^ Can you explain further please
– diggidre
2 hours ago
Hi, thought you meant in-place editing a file using sed. That's possible with the-ior--in_placeflag. See it in my 3rd command above? Regards.
– Micha
1 hour ago
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%2f506373%2fhow-to-search-and-replace-with-sed-in-a-line-with-variable-content-in-it%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
micha@linux-micha: /tmp
$ echo 'alpha bravo charlie' > blah.txt
micha@linux-micha: /tmp
$ cat blah.txt
alpha bravo charlie
micha@linux-micha: /tmp
$ sed -i 's/bravo/delta/' blah.txt
micha@linux-micha: /tmp
$ cat blah.txt
alpha delta charlie
OK it's a bit late here... I'm confused ^^ Can you explain further please
– diggidre
2 hours ago
Hi, thought you meant in-place editing a file using sed. That's possible with the-ior--in_placeflag. See it in my 3rd command above? Regards.
– Micha
1 hour ago
add a comment |
micha@linux-micha: /tmp
$ echo 'alpha bravo charlie' > blah.txt
micha@linux-micha: /tmp
$ cat blah.txt
alpha bravo charlie
micha@linux-micha: /tmp
$ sed -i 's/bravo/delta/' blah.txt
micha@linux-micha: /tmp
$ cat blah.txt
alpha delta charlie
OK it's a bit late here... I'm confused ^^ Can you explain further please
– diggidre
2 hours ago
Hi, thought you meant in-place editing a file using sed. That's possible with the-ior--in_placeflag. See it in my 3rd command above? Regards.
– Micha
1 hour ago
add a comment |
micha@linux-micha: /tmp
$ echo 'alpha bravo charlie' > blah.txt
micha@linux-micha: /tmp
$ cat blah.txt
alpha bravo charlie
micha@linux-micha: /tmp
$ sed -i 's/bravo/delta/' blah.txt
micha@linux-micha: /tmp
$ cat blah.txt
alpha delta charlie
micha@linux-micha: /tmp
$ echo 'alpha bravo charlie' > blah.txt
micha@linux-micha: /tmp
$ cat blah.txt
alpha bravo charlie
micha@linux-micha: /tmp
$ sed -i 's/bravo/delta/' blah.txt
micha@linux-micha: /tmp
$ cat blah.txt
alpha delta charlie
answered 2 hours ago
MichaMicha
893
893
OK it's a bit late here... I'm confused ^^ Can you explain further please
– diggidre
2 hours ago
Hi, thought you meant in-place editing a file using sed. That's possible with the-ior--in_placeflag. See it in my 3rd command above? Regards.
– Micha
1 hour ago
add a comment |
OK it's a bit late here... I'm confused ^^ Can you explain further please
– diggidre
2 hours ago
Hi, thought you meant in-place editing a file using sed. That's possible with the-ior--in_placeflag. See it in my 3rd command above? Regards.
– Micha
1 hour ago
OK it's a bit late here... I'm confused ^^ Can you explain further please
– diggidre
2 hours ago
OK it's a bit late here... I'm confused ^^ Can you explain further please
– diggidre
2 hours ago
Hi, thought you meant in-place editing a file using sed. That's possible with the
-i or --in_place flag. See it in my 3rd command above? Regards.– Micha
1 hour ago
Hi, thought you meant in-place editing a file using sed. That's possible with the
-i or --in_place flag. See it in my 3rd command above? Regards.– Micha
1 hour ago
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%2f506373%2fhow-to-search-and-replace-with-sed-in-a-line-with-variable-content-in-it%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