LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 06-03-2008, 09:13 AM   #1
rhaup0317
LQ Newbie
 
Registered: Jun 2008
Posts: 2

Rep: Reputation: 0
How do I replace special characters in a string within a bash script?


I have this bash script that is basically a search/replace in a text file.

I am running into issues when a user sends in a string that has special characters (quotes, spaces, =s, etc). So I think that I want to put a \ in front of each special character. Can I do it in one command or does each character needs its own command?

Or I am going about this in the wrong way?

Heres the code.
Code:
#!/bin/bash
echo "Hello!"
echo "Enter a string to be replaced:"
read old
echo "Enter a string that will replace the above string:"
read new
echo "Enter the name of a file where you want this to take place:"
read filename
#Checks to see if the file exists in the current directory
if [ -e $filename ]
then
        #saves a backup of the original file
        oldfile=$filename"_old_"`date '+%m%d%Y'`
        cat $filename > $oldfile
        #makes sure the quotes are read in correctly
        old=${old//"\""/"\\\""}
        new=${new//"\""/"\\\""}
        #performs the replacement
        sed -i 's/'$old'/'$new'/g' $filename 
        echo "Success! Your file has been updated"
else
        echo "The filename that you typed in does not exist."
fi
 
Old 06-03-2008, 10:18 AM   #2
Uxinn
Member
 
Registered: May 2008
Location: Iceland
Distribution: Ubuntu Hardy
Posts: 47

Rep: Reputation: 16
Try this
here I'm using sed "s# instead of 's/
only problematic characters now are "\" and "#" and possibly "^" at begining of line.

Code:
#!/bin/bash


echo "Hello!"
echo "Enter a string to be replaced:"
read old
echo "Enter a string that will replace the above string:"
read new
echo "Enter the name of a file where you want this to take place:"
read filename

if [ -e $filename ]
then
        #saves a backup of the original file
        oldfile=$filename"_old_"`date '+%m%d%Y'`
        cat $filename > $oldfile
        #makes sure the quotes are read in correctly
#        old=${old//"\""/"\\\""}
#        new=${new//"\""/"\\\""}
        #performs the replacement
        sed -i "s#${old}#${new}#g" $filename 
        echo "Success! Your file has been updated"
else
        echo "The filename that you typed in does not exist."
fi

Last edited by Uxinn; 06-03-2008 at 10:20 AM.
 
Old 06-03-2008, 11:56 AM   #3
rhaup0317
LQ Newbie
 
Registered: Jun 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Who rock da party? Uxinn!!!!!!!

Thanks dude! It works.

-Ryan
 
  


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 string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
bash script to find out more than 1 continuous special characters in a file. kkpal Linux - Newbie 1 06-02-2008 04:56 AM
bash command for removing special characters from string kkpal Linux - Newbie 5 05-26-2008 08:14 AM
inserting special characters into mysql with bash script ihopeto Linux - Newbie 1 12-05-2006 12:46 PM
bash and filenames with special characters CoolAJ86 Programming 2 03-09-2005 02:50 PM


All times are GMT -5. The time now is 02:29 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration