Operating System - HP-UX
1833744 Members
2910 Online
110063 Solutions
New Discussion

Re: Shell Scripting Help for find and replace

 
SOLVED
Go to solution
Praveen Hari
Advisor

Shell Scripting Help for find and replace

Hi All,
I have a file with list of strings from which I will create http URL's.

I need to replace some special characters with URL encoding before I include them in the URL due to some limitations.
This is my requirement. I have file with following lines:

xxx.yyy++.zzz
aaa.bb+cc.ddd

I want to replace every "." with "%2E" and every "+" with %2B.

How do I do that in shell scripts ?
Thanks
7 REPLIES 7
Rodney Hills
Honored Contributor
Solution

Re: Shell Scripting Help for find and replace

You can use sed-

sed -e 's/\./%2E/g' -e 's/+/%2B/g' outfile

HTH

-- Rod Hills
There be dragons...
Hai Nguyen_1
Honored Contributor

Re: Shell Scripting Help for find and replace



Praveen,

You can try this:

# sed 's/\./%2E/g' original_file | sed 's/+/%2B/g' > new_file

Hai
Praveen Hari
Advisor

Re: Shell Scripting Help for find and replace

If I have to do the same operation on a shell varaiable which has the contenet "xxx.yyy++.zzz" what will be the script ? can I just replace the file name with variable name ?
Hai Nguyen_1
Honored Contributor

Re: Shell Scripting Help for find and replace

for variable:

VAR="xxx.yyy++.zzz"
sed -e 's/\./%2E/g' -e 's/+/%2B/g' $VAR


Hai
Hai Nguyen_1
Honored Contributor

Re: Shell Scripting Help for find and replace

My example should read as follows:

VAR="xxx.yyy++.zzz"
echo $VAR | sed -e 's/\./%2E/g' -e 's/+/%2B/g'


Hai
Rodney Hills
Honored Contributor

Re: Shell Scripting Help for find and replace

newstring=`echo $string | sed -e 's/... '`

Note the backward quotes after the = and at the end. Do not include the input/output filenames.

This will execute what's in the back quotes and return the result. This value is then put into variable "newstring".


HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Shell Scripting Help for find and replace

newstring=`echo $string | sed -e 's/... '`

Note the backward quotes after the = and at the end. Do not include the input/output filenames.

This will execute what's in the back quotes and return the result. This value is then put into variable "newstring".


HTH

-- Rod Hills
There be dragons...