tags:

views:

107

answers:

4

Hello,

I have a bash script that I want to change all occurrences of jdk1.5.0_14 with jdk1.6.0_20 in a file

I have the following piece of code :

#!/bin/bash
myvar="jdk1.6.0_20"
sed "s/jdk1.*/$myvar/g" answer_file.1 > answer_file.2

However I have the following information in answer_file.1 (pasting the relevant part):

JDKSelection.directory.JDK_LIST=/usr/jdk/jdk1.5.0_14 (v. 1.5.0_14 by Sun Microsystems Inc.)
JDKSelection.directory.HIDDEN_JDK=/usr/jdk/jdk1.5.0_14

The code above changes the occurence of jdk1.5.0_14 to jdk1.6.0_20 but also removes the information contained in paranthesis in the first line.

So after the change, I need the answer_file.2 file look like this:

 JDKSelection.directory.JDK_LIST=/usr/jdk/jdk1.6.0_20 (v. 1.6.0_20 by Sun Microsystems Inc.)
 JDKSelection.directory.HIDDEN_JDK=/usr/jdk/jdk1.6.0_20

How can I achieve this?

Thanks for your answers....

+1  A: 

Your pattern searches for "jdk1.*" and thus replaces jdk1 and all that follows up to the end of the line.

You might want to match on version numbers only, like 1\.5\.0_[0-9][0-9], and replace only the numbers.

Make sure to quote the pattern accordingly, so that the backslashes do not get lost.

Ingo
This worked, thanks....
kuti
+2  A: 

If it is just the matter of changing of JDK version, you can try the following commands

#!/bin/bash
myvar="1.6.0_20"
sed "s/1\.5\.0_14/$myvar/g" answer_file.1 > answer_file.2
Kunal
What if I have a version less than 1.5 in the file? The file is not a static file, it changes over time....
kuti
Then replace 5 by [0-9]
Ingo
Or [0-4] for that matter
Ingo
A: 

May be this will help in handling the version as well

myvar="1.6.0_20"

sed "s/\(\(jdk\)\{0,1\}\)[1-9]\.[0-9]\.0_[0-9][0-9]/\1$myvar/g" answer_file.1 > answer_file.2

EDITED: Added myvar def

Kunal
Hmm very helpful but not changing version in parenthesis.
kuti
It does. What happened in your case?
Kunal
Produces the following on Solaris 10JDKSelection.directory.JDK_LIST=/usr/jdk/jdkjdk1.6.0_20 (v. 1.6.0_25 by Sun Microsystems Inc.)JDKSelection.directory.HIDDEN_JDK=/usr/jdk/jdkjdk1.6.0_20I put 1.6.0_25 when doing the testing....
kuti
Did I mention that myvar does not contain "jdk" in string. Just the final version For example, I did the following$ export myvar=1.6.0_20$ sed "s/\(\(jdk\)\{0,1\}\)[1-9]\.[0-9]\.0_[0-9][0-9]/\1$myvar/g" test.tJDKSelection.directory.JDK_LIST=/usr/jdk/jdk1.6.0_20 (v. 1.6.0_20 by Sun Microsystems Inc.)JDKSelection.directory.HIDDEN_JDK=/usr/jdk/jdk1.6.0_20
Kunal
A: 

You can anchor the version number using the trailing space:

sed "s/jdk1[^ ]* /$myvar /g" answer_file.1 > answer_file.2
Dennis Williamson