在 Solaris 10 中使用 sed 更改文件的内容

发布于 2024-09-02 19:40:37 字数 720 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱桃奶球 2024-09-09 19:40:37

如果只是更改JDK版本的问题,可以尝试以下命令

#!/bin/bash
myvar="1.6.0_20"
sed "s/1\.5\.0_14/$myvar/g" answer_file.1 > answer_file.2

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
晌融 2024-09-09 19:40:37

您的模式搜索“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.

韵柒 2024-09-09 19:40:37

可能这也有助于处理版本

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

编辑:添加了 myvar def

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

你对谁都笑 2024-09-09 19:40:37

您可以使用尾随空格来锚定版本号:

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

You can anchor the version number using the trailing space:

sed "s/jdk1[^ ]* /$myvar /g" answer_file.1 > answer_file.2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文