LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   search and replace string having multiple special characters (http://www.linuxquestions.org/questions/linux-newbie-8/search-and-replace-string-having-multiple-special-characters-750365/)

say_hi_ravi 08-26-2009 06:55 AM

search and replace string having multiple special characters
 
Hi,

below is extract of my file

$cat outgoing-xfer|grep destination-path
--destination-path= \

What I need is to replace "--destination-path=" with "--destination-path=/home/dest"

i.e. desired output is

----destination-path=/home/dest \

I could achieve it with below command

$cat outgoing-xfer|grep destination-path|perl -pi -e "s/destination-path=/destination-path=\/home\/dest/g"

But the problem is that in this case i just wanted to append "/home/dest" for which I could easily escape "/" with just two "\", but I wonder if i have a long path like "/a/b/c/d/e/f/g/h/i/j" I will have to escape so many /. Is there any other way by which I can avoid escaping forward slash.

I tried following:

$cat outgoing-xfer|grep destination-path|perl -pi -e "s/destination-path=/'destination-path=\/home\/dest'/g"

but receiving follo error

Bareword found where operator expected at -e line 1, near "s/destination-path=/'destination-path=/home"
syntax error at -e line 1, near "s/destination-path=/'destination-path=/home"
Bad name after dest' at -e line 1.

tried with enclosing in double quotes as well but in vain :(

pixellany 08-26-2009 07:09 AM

I don't know about perl, but in SED, you can use a different delimiter in the "s" command---this avoids having the "escape" the "/".

I don't understand the double-quotes with single quotes inside....

This works:
Code:

root@Ath:~# echo "--destination-path=\\"|sed 's%\(--destination-path=\)\\%\1/home/dest\\%'
--destination-path=/home/dest\
root@Ath:~#


pixellany 08-26-2009 07:11 AM

PS:
Note that you have to escape the escape ("\") to have it be read as a normal character.

centosboy 08-26-2009 07:12 AM

Quote:

Originally Posted by say_hi_ravi (Post 3658086)
Hi,

below is extract of my file

$cat outgoing-xfer|grep destination-path
--destination-path= \

What I need is to replace "--destination-path=" with "--destination-path=/home/dest"

i.e. desired output is

----destination-path=/home/dest \

I could achieve it with below command

$cat outgoing-xfer|grep destination-path|perl -pi -e "s/destination-path=/destination-path=\/home\/dest/g"

But the problem is that in this case i just wanted to append "/home/dest" for which I could easily escape "/" with just two "\", but I wonder if i have a long path like "/a/b/c/d/e/f/g/h/i/j" I will have to escape so many /. Is there any other way by which I can avoid escaping forward slash.

I tried following:

$cat outgoing-xfer|grep destination-path|perl -pi -e "s/destination-path=/'destination-path=\/home\/dest'/g"

but receiving follo error

Bareword found where operator expected at -e line 1, near "s/destination-path=/'destination-path=/home"
syntax error at -e line 1, near "s/destination-path=/'destination-path=/home"
Bad name after dest' at -e line 1.

tried with enclosing in double quotes as well but in vain :(



it actually works for me

Code:

cat outgoing-xfer | grep destination-path |perl -pi -e's#destination-path=#destination-path=/home/dest/test/this/is/#'
--destination-path=/home/dest/test/this/is/ \


or

Code:

cat outgoing-xfer | grep destination-path |perl -pi -e's#destination-path=#destination-path=/home/dest/#'
--destination-path=/home/dest/ \


this isnt the problem, but do you need the extra grep command in there?? as the perl section is already searching for the string

say_hi_ravi 08-26-2009 07:43 AM

Hey guys

That was such a quick reply!!

Both the solution worked! Many thanks mates..God bless you!


All times are GMT -5. The time now is 09:32 AM.