在 Solaris 10 中使用 sed 更改文件的内容
我有一个 bash 脚本,我想将文件中所有出现的 jdk1.5.0_14 更改为 jdk1.6.0_20
我有以下代码:
#!/bin/bash
myvar="jdk1.6.0_20"
sed "s/jdk1.*/$myvar/g" answer_file.1 > answer_file.2
但是我在answer_file.1中有以下信息(粘贴相关部分):
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
上面的代码将 jdk1.5.0_14 的出现更改为 jdk1.6.0_20,但也删除了第一行括号中包含的信息。
因此,更改后,我需要 answer_file.2 文件如下所示:
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
如何实现此目的?
感谢您的回答....
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....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果只是更改JDK版本的问题,可以尝试以下命令
If it is just the matter of changing of JDK version, you can try the following commands
您的模式搜索“jdk1.*”,从而替换 jdk1 以及该行末尾之前的所有内容。
您可能只想匹配版本号,例如
1\.5\.0_[0-9][0-9
],并仅替换数字。确保相应地引用模式,以免反斜杠丢失。
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.
可能这也有助于处理版本
编辑:添加了 myvar def
May be this will help in handling the version as well
EDITED: Added myvar def
您可以使用尾随空格来锚定版本号:
You can anchor the version number using the trailing space: