从sed的“S / C / D /'MYFILE到MyFile的输出重定向 [英] Redirect output from sed 's/c/d/' myFile to myFile

查看:109
本文介绍了从sed的“S / C / D /'MYFILE到MyFile的输出重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的sed脚本做替代,我想有替换的文件覆盖该文件。通常情况下,我认为你会使用这样的:

I am using sed in a script to do a replace and I want to have the replaced file overwrite the file. Normally I think that you would use this:

% sed -i 's/cat/dog/' manipulate
sed: illegal option -- i

不过,你可以看到我的SED没有该命令。

However as you can see my sed does not have that command.

我试过这样:

% sed 's/cat/dog/' manipulate > manipulate

但是,这只是变成操纵到一个空文件(有意义)。

But this just turns manipulate into an empty file (makes sense).

本作品:

% sed 's/cat/dog/' manipulate > tmp; mv tmp manipulate

但我想知道是否有输出重定向到该输入是取自同一文件的标准方法。

But I was wondering if there was a standard way to redirect output into the same file that input was taken from.

推荐答案

我通常使用的3号路,但有一个重要的变化

I commonly use the 3rd way, but with an important change:

$ SED的/猫/狗/'操纵> TMP和放大器;&安培; MV TMP操纵

即。变化; &放大器;&安培; 因此只有在发生的sed,此举是成功的;否则你会为你在你的sed语法做一个印刷工一旦失去了你的原始文件。

I.e. change ; to && so the move only happens if sed is successful; otherwise you'll lose your original file as soon as you make a typo in your sed syntax.

这篇关于从sed的“S / C / D /'MYFILE到MyFile的输出重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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