LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2016, 02:13 PM   #1
keif
Member
 
Registered: Apr 2013
Posts: 107

Rep: Reputation: Disabled
Replacing line in xml on remote linux unit over ssh


Hello everyone,

I'm having trouble figuring out how to replace a line of xml in an xml file over ssh, due to all the special characters.

I spent a lot of time just trying to do a simple sed command and escape all the special characters but had no success.

I thought it might be easier to create a local variable and export it like so:

Code:
export OLD='<prop name=\"portSettings\" type=\"String\" value=\"\" /\>'
export NEW='<prop name=\"portSettings\" type=\"String\" value=\"9100\" /\>'
ssh root@10.201.7.102 'bash -s' << EOF
sed -i 's/$OLD/$NEW/g' jpos.xml
EOF
As you can see the only real change that needs to be made is the 9100 value.

When I run this I get the following error:

sed: -e expression #1, char 63: unknown option to `s'

I have tried to escape the 's' and that doesn't seem to work either.


I suppose I can resort to pulling the file, editing it locally and then sending it back. But I know there must be a way to do this by executing a one-liner via ssh. I just can't seem to figure it out. Any help would be greatly appreciated.

Last edited by keif; 02-04-2016 at 02:47 PM. Reason: added sign off
 
Old 02-04-2016, 03:14 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
Your escaping was a smidge off at the end of the strings (/\ vs \/), and you don't have to use 'bash -s', though it should also work.

Code:
export OLD='<prop name=\"portSettings\" type=\"String\" value=\"\" \/>'
export NEW='<prop name=\"portSettings\" type=\"String\" value=\"9100\" \/>'
ssh user@host << EOF
sed -i 's/$OLD/$NEW/g' jpos.xml
EOF
 
1 members found this post helpful.
Old 02-05-2016, 02:29 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I would have gone a slightly different way with your sed:
Code:
sed -i '/portSettings/s/""/"9100"/' jpos.xml
 
1 members found this post helpful.
Old 02-05-2016, 06:59 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
The problem that you face ... and it is a very, very significant(!) one ... is that you will corrupt the remote XML file and thereby render it inoperable.

A wiser approach would be:
  1. Don't attempt to use sed to update an XML file. Instead, use a "real" programming language that can, using libxml2.so of course, parse the file, change its content, and then rewrite it. (Or, using another tool, safely update it in place.
  2. Use rsync -rsh=ssh to replace the entire file on the remote system.
  3. Use version-control, locally, (e.g. git) to maintain up-to-date copies of present and past "known-good" copies of this file for each remote (on your local system), so that any of them can be restored, anywhere, at will.
 
1 members found this post helpful.
Old 02-09-2016, 10:25 AM   #5
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by thesnow View Post
Your escaping was a smidge off at the end of the strings (/\ vs \/), and you don't have to use 'bash -s', though it should also work.

Code:
export OLD='<prop name=\"portSettings\" type=\"String\" value=\"\" \/>'
export NEW='<prop name=\"portSettings\" type=\"String\" value=\"9100\" \/>'
ssh user@host << EOF
sed -i 's/$OLD/$NEW/g' jpos.xml
EOF
I ended up doing this:

Code:
ssh root@$i  'bash -s' << EOF
sed -i '/portSettings/s/"9100"/""/' jpos.xml
grep portSettings jpos.xml
EOF
This worked great. Thank you so much.
 
Old 02-09-2016, 10:27 AM   #6
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
The problem that you face ... and it is a very, very significant(!) one ... is that you will corrupt the remote XML file and thereby render it inoperable.

A wiser approach would be:
  1. Don't attempt to use sed to update an XML file. Instead, use a "real" programming language that can, using libxml2.so of course, parse the file, change its content, and then rewrite it. (Or, using another tool, safely update it in place.
  2. Use rsync -rsh=ssh to replace the entire file on the remote system.
  3. Use version-control, locally, (e.g. git) to maintain up-to-date copies of present and past "known-good" copies of this file for each remote (on your local system), so that any of them can be restored, anywhere, at will.
sundialsvcs you are so right. This scenario is a true case to try out Ansible or something like that (I say Ansible because we don't have any Ruby guys on our staff :-)

This is something I have been bringing up in my department. Thank you for your suggestions, they are a great help. Keith
 
  


Reply



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
Work on a remote project with Eclipse via SSH and SVN+SSH (2 remote Linux) alwaystudent Linux - Software 5 08-12-2015 11:40 AM
VLC remote access with SSH and command line trox Linux - Networking 5 08-30-2014 08:18 AM
Remote view for CCTV unit Entropy1024 Linux - Software 1 12-19-2012 03:34 PM
[SOLVED] Copy and replacing specific line from file1 to file2 line by line vjramana Programming 10 03-28-2011 07:49 AM
[SOLVED] How can I use the command line to split a single-lined XML into a multi-line XML xexers Linux - Software 3 12-09-2010 07:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:28 PM.

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