文件中有两个相同的字符串,但需要替换第二个字符串中的单词
我的文件中包含以下字符串。
ALTER DATABASE RENAME FILE '/home/oracle/oradata/undotbs01.dbf' TO '/home/oracle/oradata/undotbs01.dbf';
使用 bash 方法,我想将第二个“undotbs01.dbf”替换为另一个名称。当我使用 grep 和 sed 执行此操作时,它将文件中的所有 undotbs01 单词更改为新值。但我只需要更改第二个undotbs(位于“TO”字之后)。
I have file which contain below string in file.
ALTER DATABASE RENAME FILE '/home/oracle/oradata/undotbs01.dbf' TO '/home/oracle/oradata/undotbs01.dbf';
With bash methods I want to replace second "undotbs01.dbf" to another name. When I did it with grep and sed it changes all undotbs01 words in file, to the new value. But I need only second undotbs (which located after "TO" word) to be changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
sed
Using
sed
对于 sed 反向引用,这就是我的建议:
这使用第一个
.*
模式的贪婪性(即它希望尽可能多地匹配)。With
sed
back-references this is what I suggest:This uses the greedyness of the first
.*
pattern (i.e. it wants to match as much as possible).建议
awk
脚本:awk脚本:
结果:
指令:
将替换字符串放入
str =“ value”
或变量名称$ varstr =“ $ var”
说明
fs =“'”
setawk
field saparator to'
ofs =''''“”
setawk
输出字段分隔符至'
sub(“。字段-1)带有
str
值。最后一个字段$ fn
是终止;
Suggesting an
awk
script:awk script:
results in:
instructions:
Put replacement string in
str="value"
or variable name $varstr="$var"
Explanation
FS="'"
setawk
field separator to'
OFS="'"
setawk
output field separator to'
sub(".*",str,$(NF-1))
substitute/replace value of (last field - 1) withstr
value. The last field$FN
is the termination;