-   Linux - General (
-   -   How to replace the symbol special characters using find command? (

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.


fortran 03-06-2013 01:58 AM

you can use

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/ = <b>+971 4 3434747</b>/ = <b>+971 4 3434747</b>,1234567 hello we edit/g'

I got following error

sed: couldn't open file = <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.

sed 's/inform/informant/g' /path/og/file
You can use this too

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.

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 " = <b>+971 4 3434747</b>" with " = <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


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= = <b>+971 4 3434747</b>
NEW= = <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 = <b>+971 4 3434747</b> into = <b>+971 5 3396361</b>

Try this

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 07:17 AM.