LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-05-2008, 03:43 AM   #1
tikit
Member
 
Registered: Feb 2008
Posts: 85

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, 03:49 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
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, 08:38 AM   #3
tikit
Member
 
Registered: Feb 2008
Posts: 85

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, 08:45 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 08:48 AM   #5
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
double quotes works for me:

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

single quotes prevent variable interpolation/substitution
 
  


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

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

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