在 Solaris 上替代 `sed

编程入门 行业动态 更新时间:2024-10-07 21:40:38
本文介绍了在 Solaris 上替代 `sed -i`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 Linux 上 sed -i 将修改输入文件.不过,它在 Solaris 上不起作用.

On Linux sed -i will modify the input files in place. It doesn't work on Solaris, though.

sed -i '$ s/OLD/NEW/g' test sed: illegal option -- i

我可以用什么来代替 Solaris 上的 sed -i?

What can I use in place of sed -i on Solaris?

推荐答案

您需要自己复制 -i 的行为,方法是将结果存储在临时文件中,然后替换原始文件与临时文件.这可能看起来不雅,但这就是 sed -i 在幕后所做的一切.

You'll need to replicate -i's behavior yourself by storing the results in a temp file and then replacing the original file with the temp file. This may seem inelegant but that's all sed -i is doing under the covers.

sed '$ s/OLD/NEW/g' test > test.tmp && cat test.tmp > test && rm test.tmp

如果您愿意,可以使用 mktemp 使其更加健壮:

If you care you could make it a bit more robust by using mktemp:

tmp=$(mktemp test.XXXXXX) sed '$ s/OLD/NEW/g' test > "$tmp" && cat "$tmp" > test && rm "$tmp"

更多推荐

在 Solaris 上替代 `sed

本文发布于:2023-11-24 14:09:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625531.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Solaris   sed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!