在 Solaris 上替代 `sed -i` [英] Alternative to `sed -i` on Solaris

查看:86
本文介绍了在 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 -i`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆