macos 在 OS X 上使用 sed 进行就地编辑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7573368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:28:41  来源:igfitidea点击:

In-place edits with sed on OS X

macosfile-iosedin-place

提问by SundayMonday

I'd like edit a file with sed on OS X. I'm using the following command:

我想在 OS X 上用 sed 编辑一个文件。我使用以下命令:

sed 's/oldword/newword/' file.txt

The output is sent to the terminal. file.txtis not modified. The changes are saved to file2.txtwith this command:

输出被发送到终端。 file.txt未修改。使用以下命令将更改保存到file2.txt

sed 's/oldword/newword/' file1.txt > file2.txt

However I don't want another file. I just want to edit file1.txt. How can I do this?

但是我不想要另一个文件。我只想编辑file1.txt。我怎样才能做到这一点?

I've tried the -i flag. This results in the following error:

我试过 -i 标志。这会导致以下错误:

sed: 1: "file1.txt": invalid command code f

回答by whittle

You can use the -iflag correctly by providing it with a suffix to add to the backed-up file. Extending your example:

您可以-i通过为其提供后缀以添加到备份文件中来正确使用该标志。扩展您的示例:

sed -i.bu 's/oldword/newword/' file1.txt

Will give you two files: one with the name file1.txtthat contains the substitution, and one with the name file1.txt.buthat has the original content.

将为您提供两个文件:一个名称file1.txt包含替换内容,另一个名称包含file1.txt.bu原始内容。

Mildly dangerous

轻度危险

If you want to destructively overwrite the original file, use something like:

如果要破坏性地覆盖原始文件,请使用以下内容:

sed -i '' 's/oldword/newword/' file1.txt
      ^ note the space

Because of the way the line gets parsed, a space is required between the option flag and its argument because the argument is zero-length.

由于该行的解析方式,选项标志与其参数之间需要一个空格,因为该参数的长度为零。

Other than possibly trashing your original, I'm not aware of any further dangers of tricking sed this way. It should be noted, however, that if this invocation of sedis part of a script, The Unix Way? would (IMHO) be to use sednon-destructively, test that it exited cleanly, and only then remove the extraneous file.

除了可能会破坏您的原件之外,我不知道以这种方式欺骗 sed 有任何进一步的危险。然而,应该注意的是,如果这个调用sed是脚本的一部分,Unix 方式?将(恕我直言)以sed非破坏性方式使用,测试它是否干净地退出,然后才删除无关文件。

回答by ksnt

I've similar problem with MacOS

我在 MacOS 上有类似的问题

sed -i '' 's/oldword/newword/' file1.txt

doesn't works, but

不起作用,但是

sed -i"any_symbol" 's/oldword/newword/' file1.txt

works well.

效果很好。

回答by jazzed

sed -i -- "s/https/http/g" file.txt

回答by kenorb

You can use -i''(--in-place) for sedas already suggested. See: The -iin-place argument, however note that -ioption is non-standard FreeBSD extensions and may not be available on other operating systems. Secondly sedis a Stream EDitor, not a file editor.

您可以按照已经建议的方式使用-i''( --in-place) sed。见:-i就地说法,但是请注意,-i选项是非标准的FreeBSD扩展和可能并不适用于其他操作系统。其次sed小号tream EDitor,而不是一个文件编辑器



Alternative way is to use built-in substitution in Vim Ex mode, like:

另一种方法是在 Vim Ex 模式下使用内置替换,例如:

$ ex +%s/foo/bar/g -scwq file.txt

and for multiple-files:

对于多个文件:

$ ex +'bufdo!%s/foo/bar/g' -scxa *.*

To edit all files recursively you can use **/*.*if shell supports that (enable by shopt -s globstar).

要以递归方式编辑所有文件,您可以使用**/*.*如果 shell 支持(通过 启用shopt -s globstar)。



Another way is to use gawkand its new "inplace" extensionsuch as:

另一种方法是使用gawk它的新“就地”扩展,例如:

$ gawk -i inplace '{ gsub(/foo/, "bar") }; { print }' file1

回答by JustADude

This creates backup files. E.g. sed -i -e 's/hello/hello world/' testfilefor me, creates a backup file, testfile-e, in the same dir.

这将创建备份文件。例如,sed -i -e 's/hello/hello world/' testfile对我来说,在同一个目录中创建一个备份文件 testfile-e。

回答by Vincent Lal

You can use:

您可以使用:

sed -i -e 's/<string-to-find>/<string-to-replace>/' <your-file-path>

Example:

例子:

sed -i -e 's/Hello/Bye/' file.txt

This works flawless in Mac.

这在Mac 中完美无缺。