LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to replace the symbol special characters using find command? (http://www.linuxquestions.org/questions/linux-general-1/how-to-replace-the-symbol-special-characters-using-find-command-4175452895/)

lahirushanaka 03-06-2013 12:50 AM

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.

fortran 03-06-2013 01:58 AM

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.

lahirushanaka 03-06-2013 08:35 AM

Thank you very much pavi_kanetkar. IS this possible to replace 100 of files at a time?

fortran 03-06-2013 08:54 AM

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.

lahirushanaka 03-11-2013 12:43 AM

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.

fortran 03-11-2013 02:31 AM

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.

lahirushanaka 03-19-2013 08:00 AM

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.

fortran 03-20-2013 02:29 AM

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.

lahirushanaka 03-20-2013 12:23 PM

Hi friend,

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


All times are GMT -5. The time now is 08:08 PM.