我试图找出是否可以在单个sed命令中编辑一个文件,而无需手动将编辑的内容流到一个新文件中,然后将新文件重命名为原始文件名。

我尝试了-i选项,但是Solaris系统说-i是非法选项。还有别的办法吗?


当前回答

就像《007:大破天幕杀机》中Moneypenny说的:“有时候旧的方法是最好的。” 金凯德后来也说过类似的话。

$ printf ',s/false/true/g\nw\n' | ed {YourFileHere}

快乐编辑到位。 增加了'\nw\n'来写入文件。很抱歉没有及时回复请求。

其他回答

需要注意的一点是,sed不能自己写文件,因为sed的唯一目的是充当“流”(即stdin、stdout、stderr和其他>&n缓冲区、套接字等的管道)上的编辑器。考虑到这一点,您可以使用另一个命令tee将输出写回文件。另一种选择是创建一个补丁,从管道的内容到diff。

三通法

sed '/regex/' <file> | tee <file>

修补方法

sed '/regex/' <file> | diff -p <file> /dev/stdin | patch

更新:

另外,请注意patch将从diff输出的第一行获取要更改的文件:

Patch不需要知道要访问哪个文件,因为这在diff输出的第一行中找到:

$ echo foobar | tee fubar

$ sed 's/oo/u/' fubar | diff -p fubar /dev/stdin
*** fubar   2014-03-15 18:06:09.000000000 -0500
--- /dev/stdin  2014-03-15 18:06:41.000000000 -0500
***************
*** 1 ****
! foobar
--- 1 ----
! fubar

$ sed 's/oo/u/' fubar | diff -p fubar /dev/stdin | patch
patching file fubar

下面的程序在我的mac上运行良好

sed -i.bak 's/foo/bar/g' sample

在示例文件中,我们用bar替换foo。原始文件的备份将保存在sample.bak中

要编辑内联而不备份,请使用以下命令

sed -i'' 's/foo/bar/g' sample

为了在Mac上解决这个问题,我不得不在core-utils中添加一些unix函数。

brew install grep
==> Caveats
All commands have been installed with the prefix "g".
If you need to use these commands with their normal names, you
can add a "gnubin" directory to your PATH from your bashrc like:
  PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"

使用gsed而不是sed调用。mac默认不喜欢grep -rl显示带有。/前缀的文件名。

~/my-dir/configs$ grep -rl Promise . | xargs sed -i 's/Promise/Bluebird/g'

sed: 1: "./test_config.js": invalid command code .

我还必须使用xargs -I{} sed -I 's/Promise/Bluebird/g'{}来处理名称中有空格的文件。

Sed支持就地编辑。来自男人sed:

-i[SUFFIX], --in-place[=SUFFIX]

    edit files in place (makes backup if extension supplied)

例子:

假设你有一个文件hello。图文短信:

hello world!

如果你想保留旧文件的备份,使用:

sed -i.bak 's/hello/bonjour' hello.txt

你会得到两个文件:hello.txt,内容如下:

bonjour world!

还有hello。txt。bak的旧内容。

如果不想保留副本,只需不要传递扩展参数。

请注意,在OS X上运行此命令时,您可能会遇到奇怪的错误,如“无效命令代码”或其他奇怪的错误。要修复此问题,请尝试

sed -i '' -e "s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g" <file>

这是因为在OSX版本的sed上,-i选项需要一个扩展参数,因此您的命令实际上被解析为扩展参数,文件路径被解释为命令代码。来源:https://stackoverflow.com/a/19457213