Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find and replace tokens for a project code using shell script

 
Raj Ms
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marshal,

That would be great if you can help me in replacing the .jar files in the same script?.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic