aspose file tools*
The moose likes Linux / UNIX and the fly likes Find and replace tokens for a project code using shell script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Find and replace tokens for a project code using shell script" Watch "Find and replace tokens for a project code using shell script" New topic
Author

Find and replace tokens for a project code using shell script

Raj Ms
Greenhorn

Joined: Mar 18, 2014
Posts: 6
I am trying to write a shell script that finds and replaces the tokens with the required values:

rename.properties will contain:

@server@=hostname
@port@=7001
Directory=/tmp/code

rename.sh should be able to find these tokens in the folders and subfolders under the directory mentioned in the rename.properties.

Please suggest.
Raj Ms
Greenhorn

Joined: Mar 18, 2014
Posts: 6
Here is what i have worked out :
***************************************************************************
. /home/oracle/devapp.properties
serverold=$serverold
servernew=$servernew
portold=$portold
portnew=$portnew


grep -rl "$serverold" ./ | xargs sed -i "s/$serverold/$servernew/g"
grep -rl "$portold" ./ | xargs sed -i "s/$portold/$portnew/g"
***************************************************************************

But the concern is i am unable to replace when hostnames are under .jar.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
Welcome to JavaRanch.

i am unable to replace when hostnames are under .jar.


Does that mean you want to alter the contents of jar files? An approach like this will only work for text files - you would need to extract the jar file (and any other archive and compression formats like tar, zip gzip etc.), run the script recursively over the contents, and then jar (tar, zip, gzip, etc.) the file back together.
Raj Ms
Greenhorn

Joined: Mar 18, 2014
Posts: 6
Hi Marshal,

That would be great if you can help me in replacing the .jar files in the same script?.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
As I said, you need to unjar them, run the replacement command, and then jar them back together. Type "jar" at the command line to learn about all the jar tool options.
 
Consider Paul's rocket mass heater.
 
subject: Find and replace tokens for a project code using shell script