Using “sed” to append to end of a filewriting a command transcript to fileCreate a file as a different user and groupAppend at the end of line using sedsed - calling a variable from a file with multilineStandard Out Append to File Size LimitationsWhere should I put ownerless log file-w flag in bash if statement not working
Why are Starfleet vessels designed with nacelles so far away from the hull?
If the music alphabet had more than 7 letters would octaves still sound like the same note?
How can demon technology be prevented from surpassing humans?
How to remind myself to lock my doors
An idiomatic word for "very little" in this context?
Is data science mathematically interesting?
How did Ron get five hundred Chocolate Frog cards?
Glacial, Magnetic and Mossy Lures; what Pokémon do they attract?
Does wall of stone need support or not?
How to execute a project with two resources where you need three resources?
Where is a "fat Cantor staircase" differentiable?
How should I tell a professor the answer to something he doesn't know?
Why is it so hard to land on the Moon?
Drawing Super Mario Bros.....in LaTeX
Giving a character trauma but not "diagnosing" her?
How can I attach a set of five panniers?
I didn't do any exit passport control when leaving Japan. What should I do?
Google can't fetch large sitemap with 50k URLs, nor will browsers render it
Why does 1.1.1.1 not resolve archive.is?
Match the blocks
Would removing the "total cover" part of a Paladin's Divine Sense unbalance the feature?
My professor says my digit summing code is flawed. Is he right?
'Kukhtarev's model' or 'THE Kukhtarev's model'?
Did smallpox emerge in 1580?
Using “sed” to append to end of a file
writing a command transcript to fileCreate a file as a different user and groupAppend at the end of line using sedsed - calling a variable from a file with multilineStandard Out Append to File Size LimitationsWhere should I put ownerless log file-w flag in bash if statement not working
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I am currently using sed
to write to an apache configuration file from stdin
. I am using sed
in this script to get around the bash script limition where the calling user does not have privilegess to write to the file, so I can't simply echo "..." >> outputfile.conf
Here is what I have for writing to the file:
echo "<VirtualHost *:80>
...
</VirtualHost>" | sudo sed -n "w/etc/apache2/sites-available/000-default.conf"
How can I later append to this same file?
if $enable_ssl; then
echo "<VirtualHost *:443>
...
</VirtualHost>" | sudo sed <opions-to-sed-here>
fi
bash shell-script sed
New contributor
add a comment
|
I am currently using sed
to write to an apache configuration file from stdin
. I am using sed
in this script to get around the bash script limition where the calling user does not have privilegess to write to the file, so I can't simply echo "..." >> outputfile.conf
Here is what I have for writing to the file:
echo "<VirtualHost *:80>
...
</VirtualHost>" | sudo sed -n "w/etc/apache2/sites-available/000-default.conf"
How can I later append to this same file?
if $enable_ssl; then
echo "<VirtualHost *:443>
...
</VirtualHost>" | sudo sed <opions-to-sed-here>
fi
bash shell-script sed
New contributor
2
Could you simply usetee -a
?
– Arkadiusz Drabczyk
8 hours ago
add a comment
|
I am currently using sed
to write to an apache configuration file from stdin
. I am using sed
in this script to get around the bash script limition where the calling user does not have privilegess to write to the file, so I can't simply echo "..." >> outputfile.conf
Here is what I have for writing to the file:
echo "<VirtualHost *:80>
...
</VirtualHost>" | sudo sed -n "w/etc/apache2/sites-available/000-default.conf"
How can I later append to this same file?
if $enable_ssl; then
echo "<VirtualHost *:443>
...
</VirtualHost>" | sudo sed <opions-to-sed-here>
fi
bash shell-script sed
New contributor
I am currently using sed
to write to an apache configuration file from stdin
. I am using sed
in this script to get around the bash script limition where the calling user does not have privilegess to write to the file, so I can't simply echo "..." >> outputfile.conf
Here is what I have for writing to the file:
echo "<VirtualHost *:80>
...
</VirtualHost>" | sudo sed -n "w/etc/apache2/sites-available/000-default.conf"
How can I later append to this same file?
if $enable_ssl; then
echo "<VirtualHost *:443>
...
</VirtualHost>" | sudo sed <opions-to-sed-here>
fi
bash shell-script sed
bash shell-script sed
New contributor
New contributor
edited 8 hours ago
Maverik Minett
New contributor
asked 8 hours ago
Maverik MinettMaverik Minett
112 bronze badges
112 bronze badges
New contributor
New contributor
2
Could you simply usetee -a
?
– Arkadiusz Drabczyk
8 hours ago
add a comment
|
2
Could you simply usetee -a
?
– Arkadiusz Drabczyk
8 hours ago
2
2
Could you simply use
tee -a
?– Arkadiusz Drabczyk
8 hours ago
Could you simply use
tee -a
?– Arkadiusz Drabczyk
8 hours ago
add a comment
|
3 Answers
3
active
oldest
votes
The usual replacement for shell >
with higher/different privileges is:
echo "replace file content with this line" | sudo tee protectedFile >/dev/null
And if you want to append, use -a
:
echo "append this line to file" | sudo tee -a protectedFile >/dev/null
add a comment
|
Whithout echo
:
string=$'"<VirtualHost *:443>
...
</VirtualHost>"'
sudo sed -i '$a'"$string"'' file
The -i
option tells sed
to process a files in-place (optionally adding a suffix to the original version).
The sed script:
$
match last linea
append text after matching line
It won't work on an empty file though (since there won't be any lines of input to match the last line).
I'm not sure why you have the double single quote at the end of the sed command?
– Grump
5 hours ago
add a comment
|
For completeness, if you have ed
at hand:
echo "$
a
<VirtualHost *:80>
...
</VirtualHost>
.
w" | sudo ed protectedFile
$
go to last linea
append, followed by data you want to append terminated by a line containing a single dotw
stands for write (file)
The first two commands can be concatenated (yielding $a
), yet this won't work on empty file – separately it will, since it means "go to last line" (which is a no-op), followed by "append" rather than "append to last line", which doesn't really exist.
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/4.0/"u003ecc by-sa 4.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
);
);
Maverik Minett is a new contributor. Be nice, and check out our Code of Conduct.
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%2f545149%2fusing-sed-to-append-to-end-of-a-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The usual replacement for shell >
with higher/different privileges is:
echo "replace file content with this line" | sudo tee protectedFile >/dev/null
And if you want to append, use -a
:
echo "append this line to file" | sudo tee -a protectedFile >/dev/null
add a comment
|
The usual replacement for shell >
with higher/different privileges is:
echo "replace file content with this line" | sudo tee protectedFile >/dev/null
And if you want to append, use -a
:
echo "append this line to file" | sudo tee -a protectedFile >/dev/null
add a comment
|
The usual replacement for shell >
with higher/different privileges is:
echo "replace file content with this line" | sudo tee protectedFile >/dev/null
And if you want to append, use -a
:
echo "append this line to file" | sudo tee -a protectedFile >/dev/null
The usual replacement for shell >
with higher/different privileges is:
echo "replace file content with this line" | sudo tee protectedFile >/dev/null
And if you want to append, use -a
:
echo "append this line to file" | sudo tee -a protectedFile >/dev/null
answered 5 hours ago
IsaacIsaac
15.1k1 gold badge25 silver badges64 bronze badges
15.1k1 gold badge25 silver badges64 bronze badges
add a comment
|
add a comment
|
Whithout echo
:
string=$'"<VirtualHost *:443>
...
</VirtualHost>"'
sudo sed -i '$a'"$string"'' file
The -i
option tells sed
to process a files in-place (optionally adding a suffix to the original version).
The sed script:
$
match last linea
append text after matching line
It won't work on an empty file though (since there won't be any lines of input to match the last line).
I'm not sure why you have the double single quote at the end of the sed command?
– Grump
5 hours ago
add a comment
|
Whithout echo
:
string=$'"<VirtualHost *:443>
...
</VirtualHost>"'
sudo sed -i '$a'"$string"'' file
The -i
option tells sed
to process a files in-place (optionally adding a suffix to the original version).
The sed script:
$
match last linea
append text after matching line
It won't work on an empty file though (since there won't be any lines of input to match the last line).
I'm not sure why you have the double single quote at the end of the sed command?
– Grump
5 hours ago
add a comment
|
Whithout echo
:
string=$'"<VirtualHost *:443>
...
</VirtualHost>"'
sudo sed -i '$a'"$string"'' file
The -i
option tells sed
to process a files in-place (optionally adding a suffix to the original version).
The sed script:
$
match last linea
append text after matching line
It won't work on an empty file though (since there won't be any lines of input to match the last line).
Whithout echo
:
string=$'"<VirtualHost *:443>
...
</VirtualHost>"'
sudo sed -i '$a'"$string"'' file
The -i
option tells sed
to process a files in-place (optionally adding a suffix to the original version).
The sed script:
$
match last linea
append text after matching line
It won't work on an empty file though (since there won't be any lines of input to match the last line).
edited 5 hours ago
peterph
24.5k2 gold badges49 silver badges63 bronze badges
24.5k2 gold badges49 silver badges63 bronze badges
answered 6 hours ago
guillermo chamorroguillermo chamorro
9391 silver badge14 bronze badges
9391 silver badge14 bronze badges
I'm not sure why you have the double single quote at the end of the sed command?
– Grump
5 hours ago
add a comment
|
I'm not sure why you have the double single quote at the end of the sed command?
– Grump
5 hours ago
I'm not sure why you have the double single quote at the end of the sed command?
– Grump
5 hours ago
I'm not sure why you have the double single quote at the end of the sed command?
– Grump
5 hours ago
add a comment
|
For completeness, if you have ed
at hand:
echo "$
a
<VirtualHost *:80>
...
</VirtualHost>
.
w" | sudo ed protectedFile
$
go to last linea
append, followed by data you want to append terminated by a line containing a single dotw
stands for write (file)
The first two commands can be concatenated (yielding $a
), yet this won't work on empty file – separately it will, since it means "go to last line" (which is a no-op), followed by "append" rather than "append to last line", which doesn't really exist.
add a comment
|
For completeness, if you have ed
at hand:
echo "$
a
<VirtualHost *:80>
...
</VirtualHost>
.
w" | sudo ed protectedFile
$
go to last linea
append, followed by data you want to append terminated by a line containing a single dotw
stands for write (file)
The first two commands can be concatenated (yielding $a
), yet this won't work on empty file – separately it will, since it means "go to last line" (which is a no-op), followed by "append" rather than "append to last line", which doesn't really exist.
add a comment
|
For completeness, if you have ed
at hand:
echo "$
a
<VirtualHost *:80>
...
</VirtualHost>
.
w" | sudo ed protectedFile
$
go to last linea
append, followed by data you want to append terminated by a line containing a single dotw
stands for write (file)
The first two commands can be concatenated (yielding $a
), yet this won't work on empty file – separately it will, since it means "go to last line" (which is a no-op), followed by "append" rather than "append to last line", which doesn't really exist.
For completeness, if you have ed
at hand:
echo "$
a
<VirtualHost *:80>
...
</VirtualHost>
.
w" | sudo ed protectedFile
$
go to last linea
append, followed by data you want to append terminated by a line containing a single dotw
stands for write (file)
The first two commands can be concatenated (yielding $a
), yet this won't work on empty file – separately it will, since it means "go to last line" (which is a no-op), followed by "append" rather than "append to last line", which doesn't really exist.
answered 4 hours ago
peterphpeterph
24.5k2 gold badges49 silver badges63 bronze badges
24.5k2 gold badges49 silver badges63 bronze badges
add a comment
|
add a comment
|
Maverik Minett is a new contributor. Be nice, and check out our Code of Conduct.
Maverik Minett is a new contributor. Be nice, and check out our Code of Conduct.
Maverik Minett is a new contributor. Be nice, and check out our Code of Conduct.
Maverik Minett is a new contributor. Be nice, and check out our Code of Conduct.
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%2f545149%2fusing-sed-to-append-to-end-of-a-file%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
2
Could you simply use
tee -a
?– Arkadiusz Drabczyk
8 hours ago