Using sed command in for loop across multiple servers The Next CEO of Stack OverflowHow to strip multiple spaces to one using sed?Text file look-up by columnHow to remove multiple blank lines from a file?Using while loop to ssh to multiple serversCreating multiple input files by using sed in a for loopstream editing tools: output what's scrapedfor loop to evaluate multiple directories and execute commandsed multiple statements within a single command not workingUsing for loop with find commandusing sed in loop
Opposite of a diet
Return the Closest Prime Number
Whats the best way to handle refactoring a big file?
Is HostGator storing my password in plaintext?
How did people program for Consoles with multiple CPUs?
Why doesn't a table tennis ball float on the surface? How do we calculate buoyancy here?
Implement the Thanos sorting algorithm
Robert Sheckley short story about vacation spots being overwhelmed
Why were Madagascar and New Zealand discovered so late?
How to count occurrences of text in a file?
Can a caster that cast Polymorph on themselves stop concentrating at any point even if their Int is low?
Is a stroke of luck acceptable after a series of unfavorable events?
What does this shorthand mean?
Rotate a column
What do "high sea" and "carry" mean in this sentence?
Fastest way to shutdown Ubuntu Mate 18.10
What does "Its cash flow is deeply negative" mean?
Term for the "extreme-extension" version of a straw man fallacy?
Why do professional authors make "consistency" mistakes? And how to avoid them?
Where to find order of arguments for default functions
Science fiction (dystopian) short story set after WWIII
I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin
How to get regions to plot as graphics
Increase performance creating Mandelbrot set in python
Using sed command in for loop across multiple servers
The Next CEO of Stack OverflowHow to strip multiple spaces to one using sed?Text file look-up by columnHow to remove multiple blank lines from a file?Using while loop to ssh to multiple serversCreating multiple input files by using sed in a for loopstream editing tools: output what's scrapedfor loop to evaluate multiple directories and execute commandsed multiple statements within a single command not workingUsing for loop with find commandusing sed in loop
I am trying to manipulate the same text file across multiple hosts. The command I currently have it:
for host in $(cat /etc/hosts | grep text | cut -d' ' -f 1 | sort -u); do
ssh $host
sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo
done
The sed command itself works locally on the host with no problems however when I run it here, I get:
sed: -e expression #1, char 9: unterminated `s' command
What am I doing wrong?
ssh sed for
add a comment |
I am trying to manipulate the same text file across multiple hosts. The command I currently have it:
for host in $(cat /etc/hosts | grep text | cut -d' ' -f 1 | sort -u); do
ssh $host
sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo
done
The sed command itself works locally on the host with no problems however when I run it here, I get:
sed: -e expression #1, char 9: unterminated `s' command
What am I doing wrong?
ssh sed for
Not the issue, but you have a uuoc ;-)
– RoVo
yesterday
@RoVo Old habbits :)
– Matthew Perrott
yesterday
Maybe: stackoverflow.com/questions/305035/…
– RoVo
yesterday
add a comment |
I am trying to manipulate the same text file across multiple hosts. The command I currently have it:
for host in $(cat /etc/hosts | grep text | cut -d' ' -f 1 | sort -u); do
ssh $host
sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo
done
The sed command itself works locally on the host with no problems however when I run it here, I get:
sed: -e expression #1, char 9: unterminated `s' command
What am I doing wrong?
ssh sed for
I am trying to manipulate the same text file across multiple hosts. The command I currently have it:
for host in $(cat /etc/hosts | grep text | cut -d' ' -f 1 | sort -u); do
ssh $host
sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo
done
The sed command itself works locally on the host with no problems however when I run it here, I get:
sed: -e expression #1, char 9: unterminated `s' command
What am I doing wrong?
ssh sed for
ssh sed for
edited yesterday
RoVo
3,442317
3,442317
asked yesterday
Matthew PerrottMatthew Perrott
155
155
Not the issue, but you have a uuoc ;-)
– RoVo
yesterday
@RoVo Old habbits :)
– Matthew Perrott
yesterday
Maybe: stackoverflow.com/questions/305035/…
– RoVo
yesterday
add a comment |
Not the issue, but you have a uuoc ;-)
– RoVo
yesterday
@RoVo Old habbits :)
– Matthew Perrott
yesterday
Maybe: stackoverflow.com/questions/305035/…
– RoVo
yesterday
Not the issue, but you have a uuoc ;-)
– RoVo
yesterday
Not the issue, but you have a uuoc ;-)
– RoVo
yesterday
@RoVo Old habbits :)
– Matthew Perrott
yesterday
@RoVo Old habbits :)
– Matthew Perrott
yesterday
Maybe: stackoverflow.com/questions/305035/…
– RoVo
yesterday
Maybe: stackoverflow.com/questions/305035/…
– RoVo
yesterday
add a comment |
3 Answers
3
active
oldest
votes
Try this,
for host in $(grep test /etc/hosts | cut -d' ' -f 1 | sort -u); do
ssh $host 'sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo'
done
we should wrap around the remote commands with quotes.
1
Pointing out the solution: The extra quotes around the remote commands. Also, would be better with aread
loop (ssh -n
would be needed).
– Kusalananda♦
yesterday
add a comment |
Try to change like this
for host in $(cat /etc/hosts | grep text | cut -d' ' -f 1 | sort -u); do
ssh $host
"sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo"
done
Thanks! msp9011
– JinChin
yesterday
add a comment |
The issue is that the command that is to be executed on the remote host relies on the quoting to be properly handled. The quotes needs to be evaluated as part of the command on the remote side, not on the local host. This means that the command needs an extra level of quotes.
Since your current attempt does not properly quote the command, it is split on whitespaces on the remote host. The sed
command then becomes the equivalent of locally running
sed -i s/enabled = 1/enabled = 0/ /etc/yum.repos.d/testing.repo
which, as the error message says, contains an s
command that is not properly terminated.
Also, looping over a command substitution is inelegant and hazardous. The value that the substitution expands to would be split into words on whitespaces, and the words would then undergo filename generation (globbing). It also requires the command substitution to be fully expanded before the loop could even start its first iteration.
Instead, use a read
loop:
awk '/text/ && !seen[$1]++ print $1 ' |
while IFS= read -r remote; do
ssh -n "$remote"
'sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo'
done
Note the ssh -n
here. It stops ssh
from reading its standard input stream, which would otherwise have it read the hosts that the awk
command outputs. Also, any variable expansion should be double quoted (unless you know in what contexts this is not needed).
Another way to solve this issue would obviously be to, instead of editing the existing file on the various remote hosts, push a new copy of the file to each machine (possibly using scp
or rsync
). This would however require that the file should look the same on each host.
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%2f508911%2fusing-sed-command-in-for-loop-across-multiple-servers%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
Try this,
for host in $(grep test /etc/hosts | cut -d' ' -f 1 | sort -u); do
ssh $host 'sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo'
done
we should wrap around the remote commands with quotes.
1
Pointing out the solution: The extra quotes around the remote commands. Also, would be better with aread
loop (ssh -n
would be needed).
– Kusalananda♦
yesterday
add a comment |
Try this,
for host in $(grep test /etc/hosts | cut -d' ' -f 1 | sort -u); do
ssh $host 'sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo'
done
we should wrap around the remote commands with quotes.
1
Pointing out the solution: The extra quotes around the remote commands. Also, would be better with aread
loop (ssh -n
would be needed).
– Kusalananda♦
yesterday
add a comment |
Try this,
for host in $(grep test /etc/hosts | cut -d' ' -f 1 | sort -u); do
ssh $host 'sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo'
done
we should wrap around the remote commands with quotes.
Try this,
for host in $(grep test /etc/hosts | cut -d' ' -f 1 | sort -u); do
ssh $host 'sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo'
done
we should wrap around the remote commands with quotes.
edited yesterday
answered yesterday
msp9011msp9011
4,54244167
4,54244167
1
Pointing out the solution: The extra quotes around the remote commands. Also, would be better with aread
loop (ssh -n
would be needed).
– Kusalananda♦
yesterday
add a comment |
1
Pointing out the solution: The extra quotes around the remote commands. Also, would be better with aread
loop (ssh -n
would be needed).
– Kusalananda♦
yesterday
1
1
Pointing out the solution: The extra quotes around the remote commands. Also, would be better with a
read
loop (ssh -n
would be needed).– Kusalananda♦
yesterday
Pointing out the solution: The extra quotes around the remote commands. Also, would be better with a
read
loop (ssh -n
would be needed).– Kusalananda♦
yesterday
add a comment |
Try to change like this
for host in $(cat /etc/hosts | grep text | cut -d' ' -f 1 | sort -u); do
ssh $host
"sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo"
done
Thanks! msp9011
– JinChin
yesterday
add a comment |
Try to change like this
for host in $(cat /etc/hosts | grep text | cut -d' ' -f 1 | sort -u); do
ssh $host
"sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo"
done
Thanks! msp9011
– JinChin
yesterday
add a comment |
Try to change like this
for host in $(cat /etc/hosts | grep text | cut -d' ' -f 1 | sort -u); do
ssh $host
"sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo"
done
Try to change like this
for host in $(cat /etc/hosts | grep text | cut -d' ' -f 1 | sort -u); do
ssh $host
"sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo"
done
edited yesterday
msp9011
4,54244167
4,54244167
answered yesterday
JinChinJinChin
213
213
Thanks! msp9011
– JinChin
yesterday
add a comment |
Thanks! msp9011
– JinChin
yesterday
Thanks! msp9011
– JinChin
yesterday
Thanks! msp9011
– JinChin
yesterday
add a comment |
The issue is that the command that is to be executed on the remote host relies on the quoting to be properly handled. The quotes needs to be evaluated as part of the command on the remote side, not on the local host. This means that the command needs an extra level of quotes.
Since your current attempt does not properly quote the command, it is split on whitespaces on the remote host. The sed
command then becomes the equivalent of locally running
sed -i s/enabled = 1/enabled = 0/ /etc/yum.repos.d/testing.repo
which, as the error message says, contains an s
command that is not properly terminated.
Also, looping over a command substitution is inelegant and hazardous. The value that the substitution expands to would be split into words on whitespaces, and the words would then undergo filename generation (globbing). It also requires the command substitution to be fully expanded before the loop could even start its first iteration.
Instead, use a read
loop:
awk '/text/ && !seen[$1]++ print $1 ' |
while IFS= read -r remote; do
ssh -n "$remote"
'sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo'
done
Note the ssh -n
here. It stops ssh
from reading its standard input stream, which would otherwise have it read the hosts that the awk
command outputs. Also, any variable expansion should be double quoted (unless you know in what contexts this is not needed).
Another way to solve this issue would obviously be to, instead of editing the existing file on the various remote hosts, push a new copy of the file to each machine (possibly using scp
or rsync
). This would however require that the file should look the same on each host.
add a comment |
The issue is that the command that is to be executed on the remote host relies on the quoting to be properly handled. The quotes needs to be evaluated as part of the command on the remote side, not on the local host. This means that the command needs an extra level of quotes.
Since your current attempt does not properly quote the command, it is split on whitespaces on the remote host. The sed
command then becomes the equivalent of locally running
sed -i s/enabled = 1/enabled = 0/ /etc/yum.repos.d/testing.repo
which, as the error message says, contains an s
command that is not properly terminated.
Also, looping over a command substitution is inelegant and hazardous. The value that the substitution expands to would be split into words on whitespaces, and the words would then undergo filename generation (globbing). It also requires the command substitution to be fully expanded before the loop could even start its first iteration.
Instead, use a read
loop:
awk '/text/ && !seen[$1]++ print $1 ' |
while IFS= read -r remote; do
ssh -n "$remote"
'sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo'
done
Note the ssh -n
here. It stops ssh
from reading its standard input stream, which would otherwise have it read the hosts that the awk
command outputs. Also, any variable expansion should be double quoted (unless you know in what contexts this is not needed).
Another way to solve this issue would obviously be to, instead of editing the existing file on the various remote hosts, push a new copy of the file to each machine (possibly using scp
or rsync
). This would however require that the file should look the same on each host.
add a comment |
The issue is that the command that is to be executed on the remote host relies on the quoting to be properly handled. The quotes needs to be evaluated as part of the command on the remote side, not on the local host. This means that the command needs an extra level of quotes.
Since your current attempt does not properly quote the command, it is split on whitespaces on the remote host. The sed
command then becomes the equivalent of locally running
sed -i s/enabled = 1/enabled = 0/ /etc/yum.repos.d/testing.repo
which, as the error message says, contains an s
command that is not properly terminated.
Also, looping over a command substitution is inelegant and hazardous. The value that the substitution expands to would be split into words on whitespaces, and the words would then undergo filename generation (globbing). It also requires the command substitution to be fully expanded before the loop could even start its first iteration.
Instead, use a read
loop:
awk '/text/ && !seen[$1]++ print $1 ' |
while IFS= read -r remote; do
ssh -n "$remote"
'sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo'
done
Note the ssh -n
here. It stops ssh
from reading its standard input stream, which would otherwise have it read the hosts that the awk
command outputs. Also, any variable expansion should be double quoted (unless you know in what contexts this is not needed).
Another way to solve this issue would obviously be to, instead of editing the existing file on the various remote hosts, push a new copy of the file to each machine (possibly using scp
or rsync
). This would however require that the file should look the same on each host.
The issue is that the command that is to be executed on the remote host relies on the quoting to be properly handled. The quotes needs to be evaluated as part of the command on the remote side, not on the local host. This means that the command needs an extra level of quotes.
Since your current attempt does not properly quote the command, it is split on whitespaces on the remote host. The sed
command then becomes the equivalent of locally running
sed -i s/enabled = 1/enabled = 0/ /etc/yum.repos.d/testing.repo
which, as the error message says, contains an s
command that is not properly terminated.
Also, looping over a command substitution is inelegant and hazardous. The value that the substitution expands to would be split into words on whitespaces, and the words would then undergo filename generation (globbing). It also requires the command substitution to be fully expanded before the loop could even start its first iteration.
Instead, use a read
loop:
awk '/text/ && !seen[$1]++ print $1 ' |
while IFS= read -r remote; do
ssh -n "$remote"
'sudo sed -i "s/enabled = 1/enabled = 0/" /etc/yum.repos.d/testing.repo'
done
Note the ssh -n
here. It stops ssh
from reading its standard input stream, which would otherwise have it read the hosts that the awk
command outputs. Also, any variable expansion should be double quoted (unless you know in what contexts this is not needed).
Another way to solve this issue would obviously be to, instead of editing the existing file on the various remote hosts, push a new copy of the file to each machine (possibly using scp
or rsync
). This would however require that the file should look the same on each host.
edited yesterday
answered yesterday
Kusalananda♦Kusalananda
138k17258426
138k17258426
add a comment |
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%2f508911%2fusing-sed-command-in-for-loop-across-multiple-servers%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
Not the issue, but you have a uuoc ;-)
– RoVo
yesterday
@RoVo Old habbits :)
– Matthew Perrott
yesterday
Maybe: stackoverflow.com/questions/305035/…
– RoVo
yesterday