开发者

Changing the contents of a file with sed in Solaris 10

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 occu开发者_StackOverflow社区rence 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....


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


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.


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


You can anchor the version number using the trailing space:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜