It's not a secret anymore!*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 5
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: 5
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: 41062
    
  43
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.


Ping & DNS - my free Android networking tools app
Raj Ms
Greenhorn

Joined: Mar 18, 2014
Posts: 5
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: 41062
    
  43
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.
 
 
subject: Find and replace tokens for a project code using shell script
 
Similar Threads
store the output of "find" command in a variable?
jdk1.3.0_02 PATH????????????
Shell scripts find n replace the word in files
How to detect a config file when using a shell script to start the app
Problem with Installation of Luntbuild