LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-05-2008, 04:43 AM   #1
tikit
Member
 
Registered: Feb 2008
Posts: 84

Rep: Reputation: 16
How to replace string containing / in a text file


Hi,

I am trying to write a script, that replaces string in a text file.

I used
Code:
sed 's/find/replace/g' oldfile > newfile
It works fine, but if the string contains '/' character I get this
Code:
sed 's/tpoWYH/.CrDts/newstring/g' oldfile > newfile
sed: -e expression #1, char 17: unknown option to `s'
Is there any way how to replace this string?

Thanks for your help

tikit
 
Old 09-05-2008, 04:49 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
You can either change the separator:
Code:
sed 's#tpoWYH/.CrDts#newstring#g' oldfile > newfile
or escape the offending character:
Code:
sed 's/tpoWYH\/.CrDts/newstring/g' oldfile > newfile
Yves.
 
Old 09-05-2008, 09:38 AM   #3
tikit
Member
 
Registered: Feb 2008
Posts: 84

Original Poster
Rep: Reputation: 16
Thanks for help. I have another problem. If I use

Code:
sed 's:$old_string:$new_string:g' oldfile > newfile
where $old_string and $new_string are variables containing the string, it does not work. The both variables contain the right string, I checked it using
Code:
echo $old_string $new_string.
. I think sed does not deal with $old_string and $new_string as being variables, bacause it worked if I replaced the variables with the real strings.

Thanks for help

tikit
 
Old 09-05-2008, 09:45 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The sed command is embedded in single quotes: this prevents the shell variable expansion and the items $old_string and $new_string are passed literally. Try to run without quotes:
Code:
sed s:$old_string:$new_string:g oldfile > newfile
the usage of single quotes is not really needed, unless you have something you want to protect from shell expansion.
 
Old 09-05-2008, 09:48 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
double quotes works for me:

sed -i -e "s:$a:$b:" filename

single quotes prevent variable interpolation/substitution
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to replace a string in a text file jpan Linux - General 3 10-14-2012 07:17 PM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 07:56 AM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 07:02 AM
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 10:22 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 10:33 PM


All times are GMT -5. The time now is 12:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration