LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-05-2013, 11:50 PM   #1
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 43

Rep: Reputation: Disabled
Lightbulb How to replace the symbol special characters using find command?


Hi guys,

I need to way to replace special characters using find command, usually im using following two command, but when there is special characters such as "#,&,<,>,<br> it not replacing, sometimes it add to existing one. See the below command im using, please give me a solution for it.

find /rezsystem/jboss/jboss_2/server/default/deploy/SERVERNAME.ear -name "*.properties" -type f -exec sed -i 's/<REPLACE THIS KEY>/<WITH THIS KEY>/g' {} \;

AND if there is "//" im using this

find /home/uploads/chathu/eventinfocus.ear -name "*.properties" -type f -exec sed -i 's$http://<REPLACE THIS KEY.COM>$http://<WITH THIS KEY>COM>$' {} \;

above two command not working for some special character, please give me idea to how to do it if there are special characters.

Thnks.
 
Old 03-06-2013, 12:58 AM   #2
fortran
Member
 
Registered: Nov 2011
Location: Cairo, Egypt
Distribution: CentOS, RHEL, Fedora
Posts: 300
Blog Entries: 2

Rep: Reputation: 50
you can use
Code:
find /root/Desktop -name "properties*" -type f -print | xargs sed -i 's/replace this/with this/g'
I have used properties* if filenames like properties100, properties1023, properties1569 etc
you can use REGEXP according to your requirement.
 
Old 03-06-2013, 07:35 AM   #3
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
Thank you very much pavi_kanetkar. IS this possible to replace 100 of files at a time?
 
Old 03-06-2013, 07:54 AM   #4
fortran
Member
 
Registered: Nov 2011
Location: Cairo, Egypt
Distribution: CentOS, RHEL, Fedora
Posts: 300
Blog Entries: 2

Rep: Reputation: 50
That's why I have used xargs for multiple files.
This command is used when you want to pass a lot of arguments to one command. xargs will combine the single line output of find and run commands with multiple arguments.
 
Old 03-10-2013, 11:43 PM   #5
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
Hi friend,
I have run following command to change it. bu i got following error.
Command I have run=
find /home/uploads/DntUseThis_ear/abcd.ear -name "*.properties" -type f -print |xargs sed -i 's/webres.label.cancellation.hotels.amend.phone1 = <b>+971 4 3434747</b>/webres.label.cancellation.hotels.amend.phone1 = <b>+971 4 3434747</b>,1234567 hello we edit/g'

I got following error

sed: couldn't open file ebres.label.cancellation.hotels.amend.phone1 = <b>+971 4 3434747</b>,1234567 hello we edit/g: No such file or directory

Please advice me.
 
Old 03-11-2013, 01:31 AM   #6
fortran
Member
 
Registered: Nov 2011
Location: Cairo, Egypt
Distribution: CentOS, RHEL, Fedora
Posts: 300
Blog Entries: 2

Rep: Reputation: 50
First I want to tell you If you want to change a string like this
inform to informant.
You do not have only this option.
Code:
sed 's/inform/informant/g' /path/og/file
You can use this too
Code:
sed 's/inform/&ant/g' /path/og/file
where '&' is the symbol of string what do you want to replace.

You are getting the error because you have not used backslash(\) before dot (.).
Linux is taking dot as special characters and it is using it as a regular expression. So when you have special character like dot or slash, you have to put backslash before it. It works as a escape character.
Code:
find  /home/uploads/DntUseThis_ear/abcd.ear -name "*.properties" -type f -print |xargs sed -i 's/webres\.label\.cancellation\.hotels\.amend\.phone1 = <b>+971 4 3434747<\/b>/&1234567 hello we edit/g'
Now above command will replace "webres.label.cancellation.hotels.amend.phone1 = <b>+971 4 3434747</b>" with "webres.label.cancellation.hotels.amend.phone1 = <b>+971 4 3434747</b>1234567 hello we edit".
I haven't done it practically, I am just giving you hint. If you get any error, paste here.
 
Old 03-19-2013, 07:00 AM   #7
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
HI,

Still im facing to above problems. it not replace when i run following command.

find /home/uploads/DntUseThis_ear/lamarholidays.ear -name "*.properties" -type f -exec sed -i 's/webres\.label\.cancellation\.hotels\.amend\.phone1 = \<\b\>\+971 4 3434747<\/b\>/webres\.label\.cancellation\.hotels\.amend\.phone1 = \<b\>\+971 5 3396361\<\/b\>/g' {} \;


Old= webres.label.cancellation.hotels.amend.phone1 = <b>+971 4 3434747</b>
NEW= webres.label.cancellation.hotels.amend.phone1 = <b>+971 5 3396361</b>


please tell me whether is correct or wrong.
 
Old 03-20-2013, 01:29 AM   #8
fortran
Member
 
Registered: Nov 2011
Location: Cairo, Egypt
Distribution: CentOS, RHEL, Fedora
Posts: 300
Blog Entries: 2

Rep: Reputation: 50
If you want to change webres.label.cancellation.hotels.amend.phone1 = <b>+971 4 3434747</b> into webres.label.cancellation.hotels.amend.phone1 = <b>+971 5 3396361</b>


Try this
Code:
find  /home/uploads/DntUseThis_ear/lamarholidays.ear -name "*.properties" -type f -exec sed -i 's/webres\.label\.cancellation\.hotels\.amend\.phone1 = <b>+971 4 3434747<\/b>/webres\.label\.cancellation\.hotels\.amend\.phone1 = <b>+971 5 3396361<\/b>/g' {} \;
In your code you have used webres\.label\.cancellation\.hotels\.amend\.phone1 = \<\b\>\+971 4 3434747<\/b\> for old string, if you see this carefully, you have used \<\b\> for initial bold syntax, you should not give backspace before b, it is not able to find it that's why it is not able to replace it.
Second do not use backspace before <, >, & +. These are not used as escape characters just like you have done with =, you haven't used backspace for = and it is correct.

Last edited by fortran; 03-20-2013 at 01:32 AM.
 
Old 03-20-2013, 11:23 AM   #9
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
Hi friend,

It is working, you are genius friend. thank you very much.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use sed to find lines with special characters bryanwdly Linux - General 2 02-07-2012 12:54 PM
Using sed to replace special characters in a file wskibum Linux - Software 2 03-24-2011 09:47 PM
[SOLVED] replace new line with <br /> & escape special characters ted_chou12 Linux - Newbie 5 02-07-2011 07:24 PM
search and replace string having multiple special characters say_hi_ravi Linux - Newbie 4 08-26-2009 07:43 AM
How do I replace special characters in a string within a bash script? rhaup0317 Linux - Newbie 2 06-03-2008 11:56 AM


All times are GMT -5. The time now is 06:37 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration