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










0















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?










share|improve this question
























  • Not the issue, but you have a uuoc ;-)

    – RoVo
    yesterday











  • @RoVo Old habbits :)

    – Matthew Perrott
    yesterday











  • Maybe: stackoverflow.com/questions/305035/…

    – RoVo
    yesterday















0















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?










share|improve this question
























  • Not the issue, but you have a uuoc ;-)

    – RoVo
    yesterday











  • @RoVo Old habbits :)

    – Matthew Perrott
    yesterday











  • Maybe: stackoverflow.com/questions/305035/…

    – RoVo
    yesterday













0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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










3 Answers
3






active

oldest

votes


















2














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.






share|improve this answer




















  • 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


















1














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





share|improve this answer

























  • Thanks! msp9011

    – JinChin
    yesterday


















1














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.






share|improve this answer

























    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
    );



    );













    draft saved

    draft discarded


















    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









    2














    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.






    share|improve this answer




















    • 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















    2














    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.






    share|improve this answer




















    • 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













    2












    2








    2







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 a read loop (ssh -n would be needed).

      – Kusalananda
      yesterday












    • 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







    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













    1














    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





    share|improve this answer

























    • Thanks! msp9011

      – JinChin
      yesterday















    1














    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





    share|improve this answer

























    • Thanks! msp9011

      – JinChin
      yesterday













    1












    1








    1







    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





    share|improve this answer















    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday









    msp9011

    4,54244167




    4,54244167










    answered yesterday









    JinChinJinChin

    213




    213












    • Thanks! msp9011

      – JinChin
      yesterday

















    • Thanks! msp9011

      – JinChin
      yesterday
















    Thanks! msp9011

    – JinChin
    yesterday





    Thanks! msp9011

    – JinChin
    yesterday











    1














    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.






    share|improve this answer





























      1














      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.






      share|improve this answer



























        1












        1








        1







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        KusalanandaKusalananda

        138k17258426




        138k17258426



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.