Replace userinput with forward sladh (/)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace userinput with forward sladh (/)
# 1  
Old 11-05-2007
Replace userinput with forward sladh (/)

Hi All,

If I wanted to find and replace a line with a user input value that has forward slash, then how to do it?

For example I have a script called ./configure.sh which will ask for user to input some path (let's say WPS_HOME). I can read that user input and want to put this value to a configuration file by searching for a string and replace that entire line with user input value. Using following command I can get this working with hard coded value,

sed "s/WPS_HOME.*/WPS_HOME=\/opt\/WebSphere/g" $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh > $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh_Temp
mv $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh_Temp $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh

However if the user input value is stored into a variable called WPS_HOME and then use this variable to replace....

sed "s/WPS_HOME.*/WPS_HOME='"$WPS_HOME"'/g" $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh > $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh_Temp
mv $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh_Temp $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh

This does not work. It will throw error

sed: 0602-404 Function s/WPS_HOME.*//opt/websphere/ cannot be parsed.

Can anyone suggest solving this problem? I cannot use perl here, any solution using sed or awk is fine.

Thanks,
Rijesh.
# 2  
Old 11-05-2007
Code:
a=add
b=mul
echo "addsub" | sed "s/$a/$b/"

# 3  
Old 11-05-2007
Thanks for the reply.

Yes, it will work, but what will be in case if the variable is taking from user which is a valid UNIX path that contains / and need to put that variable by replacing the corresponding value to other file or value? Any idea?
# 4  
Old 11-05-2007
From sed man page :
Quote:
(2)s/pattern/replacement/flags

Substitutes the replacement string for the first occurrence of the pattern
parameter in the pattern space. Any character that is displayed after the s
subcommand can substitute for the / (slash) separator except for the space or
new-line character.
You can do something like that:
Code:
sed "s§WPS_HOME.*§WPS_HOME='$WPS_HOME'§g" $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh > $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh_Temp
mv $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh_Temp $SPLFRAMEWORK_HOME/environment/splsetupCmdLine.sh

Jean-Pierre.

Last edited by aigles; 11-05-2007 at 10:43 AM..
# 5  
Old 11-05-2007
This works fine, thank you very much. Could you please tell me what is "§" character and what is the significance of it?

Thanks,
Rijesh.
# 6  
Old 11-06-2007
Quote:
Originally Posted by rijeshpp
Thanks for the reply.

Yes, it will work, but what will be in case if the variable is taking from user which is a valid UNIX path that contains / and need to put that variable by replacing the corresponding value to other file or value? Any idea?
Code:
echo "addsub" | sed "s/$a/$b/"

Delimiter separating ( / ) search pattern and replace pattern need not be ' / ' always, if there is ' / ' character in the input, you could change the delimiter

something like

Code:
echo "addsub" | sed "s_$a_$b_"

# 7  
Old 11-06-2007
Quote:
Originally Posted by matrixmadhan
Delimiter separating ( / ) search pattern and replace pattern need not be ' / ' always, if there is ' / ' character in the input, you could change the delimiter
Exactly.

The use of "/" to delimit regular expressions is not a rule, just a convention. You could use any other character as long as you are consistent to yourself: "s/x/y/g" is the same as "s:x:y:g", but "s/x:y:g" won't work because once you have used a delimiting character in a regexp you have to stick with it.

Anyways, it is a good idea to escape certain characters anyways, regardless of having a workaround with other delimiting chars or not. Therefor it is advisable to have a separate sed-script modify the input before feeding it to further sed-scripts:

"s/[/\.]/\\&/g"

will "escape" any of the characters "\", "/" and "." by putting a backslash ("\") in front of them. Notice, though, that some backslashes are interpreted by the shell. Play around a bit by trying ("<spc>" is ablank character):

print - "\<spc>" | sed 's/[/\.]/\\&/g'

and modify the string enclosed in the double quotes in the "print -"-statement. Notice, how it changes the behaviour if you remove the space and find out why.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace string until 3Rd occurance of forward slash(/)

I have a file abc.txt which has records like 456 /home/fgg/abdc.txt 3567 /home/fdss/vfgb.txt 23 /home/asd/dfght.txt I WANT TO REMOVE STRING UNTIL 3RD OCCURANCE OF FORWARD SLASH Output should be like abdc.txt vfgb.txt dfght.txt (5 Replies)
Discussion started by: himanshupant
5 Replies

2. Shell Programming and Scripting

AWK or SED to replace forward slash

hi hope somebody can help, there seems to be bit on the net about this, but still cant make it work the way i need. i have a file live this mm dd ff /dev/name1 mm dd ff /dev/name2 mm dd ff /dev/name3 mm dd ff /dev/name4 i need to update /dev/name1 etc to /newdev/new/name1 etc so... (5 Replies)
Discussion started by: dshakey
5 Replies

3. Ubuntu

Iptables forward traffic to forward chain!!!

Hi, I am new to linux stuff. I want to use linux iptables to configure rule so that all my incoming traffic with protocol "tcp" is forwarded to the "FORWARD CHAIN". The traffic i am dealing with has destination addresss of my machine but i want to block it from coming to input chain and somehow... (0 Replies)
Discussion started by: arsipk
0 Replies

4. IP Networking

IP tables - ip forward to another ip

Hi all, Now my need is: This should forward each client to 1.11 and 1.12 as per each request. I mean : First request should go to : http://192.168.1.10:8080/MySite Second request should go to : http://192.168.1.11:8081/MySite Third request should go to ... (1 Reply)
Discussion started by: linuxadmin
1 Replies

5. UNIX for Dummies Questions & Answers

Replace Forward Slash with sed

i need to replace '/' forward slash with \/(backward slash follwed by a forward slash) using sed command when the forward slash occurs as a first character in a file.. Tried something like this but doesn't seem to work. find $1 -print0 | xargs -0 sed -i -e 's/^\//\\\//g' Can someone... (19 Replies)
Discussion started by: depakjan
19 Replies

6. UNIX for Dummies Questions & Answers

[Help]Shellscript -Userinput string replacement with ....

Hi Respeceted , What i want to do is take input from user-input and replace 1st with last &last with 1st character.. eg: world>>dorlw how to do dat?plz help me:o (2 Replies)
Discussion started by: uni9
2 Replies

7. What is on Your Mind?

Who's looking forward to Ironman 2?

I can't wait for this movie to come out. I loved the first one and I look for a lot more action in the second one. Plus Scarlett Johannson as a red head.. I mean.. how can it be bad? :) (5 Replies)
Discussion started by: dday
5 Replies

8. UNIX for Advanced & Expert Users

Forward Script

Here is wat iam looking for , I need a forward script which sends out a mail to a particular server say (B-server) as soon as it receives a mail from differnt server say A-server. Lets say abc@xyz.com is sending a mail from server A to Server B then the script should automatically send a mail to... (2 Replies)
Discussion started by: sriharan
2 Replies

9. UNIX for Dummies Questions & Answers

.forward

We have unix faxing software that e-mails the fax results to our users unix mail. We want to forward this e-mail to their desktop internet mail. Originally we setup .forward files in each users id to eliminate unwanted unix mail from the fax. Now I want to modify the forward. We are on... (3 Replies)
Discussion started by: MsGail
3 Replies
Login or Register to Ask a Question