LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2015, 09:08 AM   #1
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 89

Rep: Reputation: Disabled
sed search and replace help please...


Hello

I want to replace

this
<emulator>/usr/bin/qemu-kvm</emulator>

with this
<emulator>/usr/sbin/qemu-system-x86_64</emulator>

in *.xml files.

How do i do this?

I tried this.. but not working.
Code:
# sed -i "s/\<emulator\>\/usr\/bin\/qemu-kvm\<\/emulator\>/\<emulator\>\/usr\/sbin\/qemu-system-x86_64\<\/emulator\>/" *.xml
I know it can be made simple by removing <emulator> part, but for learning sake, i want to know how I can search and replace strings that contain special chars, spaces etc.

Please help. Thanks
 
Old 09-26-2015, 09:19 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Hi there!

Well, the easiest approach is to simply replace the chars "kvm" with "system-x86_64".

Like so:
Code:
$ echo "<emulator>/usr/bin/qemu-kvm</emulator>" | sed 's/kvm/system-x86_64/'
<emulator>/usr/bin/qemu-system-x86_64</emulator>
Best regards,
HMW
 
Old 09-26-2015, 09:28 AM   #3
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 89

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by HMW View Post
Hi there!

Well, the easiest approach is to simply replace the chars "kvm" with "system-x86_64".

Like so:
Code:
$ echo "<emulator>/usr/bin/qemu-kvm</emulator>" | sed 's/kvm/system-x86_64/'
<emulator>/usr/bin/qemu-system-x86_64</emulator>
Best regards,
HMW
Thanks for the quick reply. But you missed bin/sbin part.

I already made it working with
Code:
# sed -i "s/\/usr\/bin\/qemu-kvm/\/usr\/sbin\/qemu-system-x86_64/" *.xml
I was looking for any easy method of search and replace like bash's single quote method, so that i can avoid '\' for every special character.

Thanks
 
Old 09-26-2015, 09:31 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by mangya View Post
Hello

I want to replace

this
<emulator>/usr/bin/qemu-kvm</emulator>

with this
<emulator>/usr/sbin/qemu-system-x86_64</emulator>

in *.xml files.

How do i do this?

I tried this.. but not working.
Code:
# sed -i "s/\<emulator\>\/usr\/bin\/qemu-kvm\<\/emulator\>/\<emulator\>\/usr\/sbin\/qemu-system-x86_64\<\/emulator\>/" *.xml
I know it can be made simple by removing <emulator> part, but for learning sake, i want to know how I can search and replace strings that contain special chars, spaces etc.

Please help. Thanks

First of all, please note that you can replace / with other charactres to make it more readable (if there are forward slashes in your search string) to avoid the need to escape special characters.
Code:
sed 's@bin/qemu-kvm@sbin/qemu-system-x86_64@' *.xml
Secondly, <> are not special characters for sed so must not be escaped with \.

Code:
sed "s/\<emulator\>\/usr\/bin\/qemu-kvm\<\/emulator\>/\<emulator\>\/usr\/sbin\/qemu-system-x86_64\<\/emulator\>/" file

Last edited by sycamorex; 09-26-2015 at 09:33 AM.
 
1 members found this post helpful.
Old 09-26-2015, 09:39 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by mangya View Post
Thanks for the quick reply. But you missed bin/sbin part.
Hepp! Yes, you're right. Sorry about that

Glad you were able to solve it. You can shorten your sed into something like this if you want:
Code:
sed 's/bin.*kvm/sbin\/qemu-system-x86_64/'
Code:
$ echo "<emulator>/usr/bin/qemu-kvm</emulator>" | sed 's/bin.*kvm/sbin\/qemu-system-x86_64/'
<emulator>/usr/sbin/qemu-system-x86_64</emulator>
Best regards,
HMW
 
1 members found this post helpful.
Old 09-26-2015, 09:57 AM   #6
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 89

Original Poster
Rep: Reputation: Disabled
Thanks @sycamorex and @HMW for help.

Quote:
First of all, please note that you can replace / with other charactres to make it more readable (if there are forward slashes in your search string) to avoid the need to escape special characters.

Secondly, <> are not special characters for sed so must not be escaped with \.
Didn't know that, thanks @sycamorex.
 
Old 09-27-2015, 10:06 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You might want to have a read through the http://www.grymoire.com/Unix/Sed.html although it is a bit old.
 
  


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
[SOLVED] Search and replace with SED jamesdunn72 Programming 9 07-22-2014 02:05 PM
sed search and replace question the_rhino Linux - General 3 12-10-2012 03:25 AM
sed command search and replace zulkifal Linux - Newbie 8 11-26-2012 10:56 AM
Using sed - search and replace seebee Programming 5 06-07-2011 09:47 PM
sed search replace tomerbd1 Linux - General 9 04-10-2008 04:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:07 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