PDA

View Full Version : replace character in a string



dunryc
November 28th, 2007, 08:03 PM
Hi all hope some one can help me ive writng a bash script scrape data from web site (from the html source) ive got most of the sript running fine but have a small prob one of the sites i need to seach replaces space dharacters in the seach string with "%20" not including the quotation marks the string is stored in a variable search wich ive got from the user using the read command , hope some one can help or at least understand what im not very elequently tring to say !!

regards dunryc

volanin
November 28th, 2007, 08:21 PM
hope some one can help or at least understand what im not very elequently tring to say !!

Your english is very good.
It seems that you are just lazy to type correctly.
It would be much easier to get your point across with correct punctuation, believe me!
:)

Try this:

echo $VARIABLE | sed 's/%20/ /g'

Or this to modify the file directly:

sed -i 's/%20/ /g' index.html

Wybiral
November 28th, 2007, 08:21 PM
I can't help you with BASH, but if you could afford to do it in Python it's pretty simple:



print "Hello%20World!".replace("%20", " ")

geirha
November 28th, 2007, 08:46 PM
If you want a really, really dirty way of doing it, you can try

line="Hello%20%3A%29"
eval `echo echo -e $line | sed 's/%\([A-F0-9]\{2\}\)/"\\\0"\`echo "ibase=16;obase=8;\1"|bc\`/g'`
which will replace any %## with its respective ascii symbol.

Beautiful isn't it?

dunryc
November 28th, 2007, 09:37 PM
wow thanks for the quick responces, I tried all of the above , but settled with the following. :-


VARIABLE1=$(echo $$VARIABLE2 | sed 's/ /30%/g')_

Which seems to work perfectly (until i break it) leaving the output of the sed command in $VARIABLE1.

Once again thanks for the heads up guys :)

Keith Hedger
November 28th, 2007, 09:48 PM
Easier:
var1="AAA%20BBB%20CCC"
var1=${var1//'%20'/" "}
echo $var1
=
AAA BBB CCC

dunryc
November 28th, 2007, 09:56 PM
thanks keith that works great and makes it asy for me and others to understand

easoukenka
August 25th, 2008, 06:03 AM
Thanks Keith you really helped me with this one believe it or not I was googling forever for this for some reason no export trick ever worked for me your solution was the simplest and only one that worked for me!