LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-26-2009, 06:55 AM   #1
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Rep: Reputation: 15
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
 
Old 08-26-2009, 07:09 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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:~#
 
Old 08-26-2009, 07:11 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
PS:
Note that you have to escape the escape ("\") to have it be read as a normal character.
 
Old 08-26-2009, 07:12 AM   #4
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by say_hi_ravi View Post
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
 
Old 08-26-2009, 07:43 AM   #5
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Original Poster
Rep: Reputation: 15
Hey guys

That was such a quick reply!!

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


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 special character by any word in a string. mksc Linux - Newbie 1 08-21-2008 02:33 AM
How do I replace special characters in a string within a bash script? rhaup0317 Linux - Newbie 2 06-03-2008 11:56 AM
bash command for removing special characters from string kkpal Linux - Newbie 5 05-26-2008 08:14 AM
search unknown special characters on a textfile bad_jaye Linux - Newbie 6 05-02-2008 09:55 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:40 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration