LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-02-2021, 02:25 AM   #1
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Rep: Reputation: 0
sed command with /


I am trying to use the sed command like this
Code:
  sed -i 's/cd /usr/bin/prp/prpclient-1/#cd /usr/bin/prp/prpclient-1/g' /home/dingo/myscript.sh"
to change cd /usr/bin/prp/prpclient-1 to #cd /usr/bin/prp/prpclient-1

I get this error

Quote:
sed: -e expression #1, char 11: unknown option to `s'
I am not sure if it is the # or the / that is the problem. I am not a programmer and at 71 can't think as well as I used to.

I also want to use the command with pssh to change the file in multiple machines
Code:
  parallel-ssh -h hosts_file.txt -i "sed -i 's/cd /usr/bin/prp/prpclient-1/#cd /usr/bin/prp/prpclient-1/g' /home/dingo/myscript.sh"
but I also get an error.
Quote:
Exited with error code 1
Stderr: sed: -e expression #1, char 11: unknown option to `s'
Which us probably the same error.


Any help is always appreciated.

Last edited by gilesaj001; 09-02-2021 at 02:27 AM.
 
Old 09-02-2021, 02:28 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,335
Blog Entries: 3

Rep: Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731
The marker in the substitution command can be any character but has to be the same all the way through:

Code:
s/old/new/
s#old#new#
s|old|new|
s!old!new!
saoldanewa
And so on.

Also, the -i option can and probably should be used with an extension.
 
Old 09-02-2021, 08:12 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
Quote:
Originally Posted by Turbocapitalist View Post
Also, the -i option can and probably should be used with an extension.
I prefer a more explicit non-Sed backup, since it's easy to run a command with (e.g) -i.bak twice and wipe out the original.

 
Old 09-02-2021, 10:39 PM   #4
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
I have tried using the ! as the separator and it works on it's own like
Code:
 sed -i 's!#cd /usr/bin/prp/prpclient-1!cd /usr/bin/prp/prpclient-1!' /home/dingo/myscript.sh
BUT whwn I try and use it with pssh like this


Code:
 parallel-ssh -h hosts_file.txt -i sed -i 's!#cd /usr/bin/prp/prpclient-1!cd /usr/bin/prp/prpclient-1!g' /home/dingo/myscript.sh
I get the error:

Quote:
[1] 23:37:39 [FAILURE] "IP ADDRESS" Exited with error code 1
Stderr: sed: -e expression #1, char 5: unterminated `s' command
The format for pssh is OK when I run this

Code:
root@dingo4:~# parallel-ssh -h hosts_file.txt -i "sed -i 's/work_buf_min_days>0.000000/work_buf_min_days>4.000000/g' /usr/bin/global_prefs_override.xml"
[1] 23:51:17 [SUCCESS] 158.69.
[2] 23:51:17 [SUCCESS] 158.69
[3] 23:51:17 [SUCCESS] 158.69.
[4] 23:51:17 [SUCCESS] 158.69.
I tried it with the "" like the one that works but I get another error:
Code:
parallel-ssh -h hosts_file.txt -i "sed -i 's!#cd /usr/bin/prp/prpclient-1!cd /usr/bin/prp/prpclient-1!g' /home/dingo/myscript.sh"
Quote:
-bash: !': event not found

Last edited by gilesaj001; 09-02-2021 at 10:58 PM.
 
Old 09-02-2021, 11:19 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752
For the bash shell '!' is special char. For example you can recall/rerun a prev cmd by giving it's cmd num from bash history eg
Code:
!996
will re-run cmd num 996 in your bash history...

Try ':' instead.
 
Old 09-02-2021, 11:43 PM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by boughtonp View Post
it's easy to run a command with (e.g) -i.bak twice and wipe out the original.
This has happened to me nore times than I care to admit.

Evo2.
 
Old 09-03-2021, 12:02 AM   #7
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
For the bash shell '!' is special char. For example you can recall/rerun a prev cmd by giving it's cmd num from bash history eg
Code:
!996
will re-run cmd num 996 in your bash history...

Try ':' instead.
I tried : but get the same error
Code:
parallel-ssh -h hosts_file.txt -i "sed -i 's:#etc/init.d/boinc start:/etc/init.d/boinc start:g' /usr/bin/global_prefs_override.xml"
But still get the same error

Quote:
[1] 01:00:48 [FAILURE] 158.69.1**.** Exited with error code 1
Stderr: sed: -e expression #1, char 5: unterminated `s' command
[2] 01:00:49 [FAILURE] 158.69.1**.** Exited with error code 1
Stderr: sed: -e expression #1, char 5: unterminated `s' command
[3] 01:00:49 [FAILURE] 158.69.**.** Exited with error code 1
Stderr: sed: -e expression #1, char 5: unterminated `s' command
[4] 01:00:49 [FAILURE] 158.69.1**.*** Exited with error code 1

Last edited by gilesaj001; 09-03-2021 at 12:17 AM. Reason: format error
 
Old 09-03-2021, 02:04 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,980

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
# is a special char too, so I would try escape that too.
Code:
parallel-ssh -h hosts_file.txt -i "\"sed -i 's:#etc/init.d/boinc start:/etc/init.d/boinc start:g' /usr/bin/global_prefs_override.xml\""
probably works.
 
Old 09-04-2021, 12:49 AM   #9
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
# is a special char too, so I would try escape that too.
Code:
parallel-ssh -h hosts_file.txt -i "\"sed -i 's:#etc/init.d/boinc start:/etc/init.d/boinc start:g' /usr/bin/global_prefs_override.xml\""
probably works.
I think we are closer but I get the error

[4] 01:46:11 [FAILURE] 158.69.1**.*** Exited with error code 127
Stderr: bash: sed -i 's:#etc/init.d/boinc start:/etc/init.d/boinc start:g' /usr/bin/global_prefs_override.xml: No such file or directory
 
Old 09-04-2021, 01:14 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,335
Blog Entries: 3

Rep: Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731
Is it GNU sed on all the other machines? Or do you need something fully portable? If GNU sed is present on each, then you might see if it is the pound sign causing the trouble and write it as octal instead:

Code:
parallel-ssh -h hosts_file.txt -i "sed -i 's:\o043etc/init.d/boinc start:/etc/init.d/boinc start:g' /usr/bin/global_prefs_override.xml"
Just a random guess.
 
1 members found this post helpful.
Old 09-05-2021, 12:53 AM   #11
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
Is it GNU sed on all the other machines? Or do you need something fully portable? If GNU sed is present on each, then you might see if it is the pound sign causing the trouble and write it as octal instead:

Code:
parallel-ssh -h hosts_file.txt -i "sed -i 's:\o043etc/init.d/boinc start:/etc/init.d/boinc start:g' /usr/bin/global_prefs_override.xml"
Just a random guess.

Thank you that works, I knew it was the # as the error changed when it moved in the script but I did not know how to do it.
 
Old 09-05-2021, 12:57 AM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,335
Blog Entries: 3

Rep: Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731
It'll work on systems with GNU sed but no others.

I wonder if the problem isn't parallel-ssh there. Looking at the manual page for it, I don't see much anything standing out in the way of advantages over the normal SSH client launched with a while loop. Are you using any special features of Parallel-SSH or just the ability to read targets from a list?
 
Old 09-05-2021, 04:04 AM   #13
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
I use it to send commands from one machine to a bunch of other machines. I have eight dedicated servers that I run BOINC on and 16 Pi 4's that I also use for BOINC. with pssh I can send one command from one machine and get it applied on all machines that I have in the hosts_file.txt.

If there is a simpler way then I am open to suggestions but I am not a programmer so it would have to be easy to follow..
 
Old 09-05-2021, 05:02 AM   #14
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,335
Blog Entries: 3

Rep: Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731
Quote:
Originally Posted by gilesaj001 View Post
I use it to send commands from one machine to a bunch of other machines. I have eight dedicated servers that I run BOINC on and 16 Pi 4's that I also use for BOINC. with pssh I can send one command from one machine and get it applied on all machines that I have in the hosts_file.txt.

If there is a simpler way then I am open to suggestions but I am not a programmer so it would have to be easy to follow..
Thanks.

Although there has been a bit of drift, all GNU/Linux systems are at their heart IDEs. So they are built around programming and scripting and, in reality, all interaction through the shell interpreter is actually scripting. So with that caveat:

Code:
while read host; do 
        ssh $host "sed -i.bak 's:#etc/init.d/boinc start:/etc/init.d/boinc start:g' /usr/bin/global_prefs_override.xml"
done < hosts_file.txt
So while on the surface that looks like it adds two lines, underneath it allows use of the normal SSH client and re-uses pre-existing shell skills. Knowledge of shell scripting is a highly useful skill even with a small investment of time.

Maybe parallel-ssh is more appropriate, especially if you have hundreds or thousands of machines. However, the option for normal shell work is always.
 
Old 09-06-2021, 02:17 PM   #15
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,816

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
Attention, ssh can read from the stdin, competing with the read!
Have
ssh -n ...
or
</dev/null ssh ...

An alternative is to pass a local script through ssh's stdin to the remote host.
Code:
while read host
do
  ssh $host /bin/bash -s <<"_EOT"
    sed -i.bak 's:#etc/init.d/boinc start:/etc/init.d/boinc start:g' /usr/bin/global_prefs_override.xml
_EOT
done < hosts_file.txt
 
1 members found this post helpful.
  


Reply

Tags
bash, sed bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed substitution gives error "sed: command garbled" gsai0205 Linux - Newbie 3 09-13-2013 09:01 AM
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 05:31 PM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration