sed 插入行命令 OSX [英] sed insert line command OSX

查看:21
本文介绍了sed 插入行命令 OSX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 sed 将文本插入文件的第三行,我在其他论坛上发现的语法是:

I'm trying to insert text to the third line in a file using sed, and the syntax I've found on other forums is:

sed -i '' "3i text to insert" file

然而,当我使用它时,我收到一个错误:

When I use this however, I get an error:

sed: 1: "3i text to insert": extra characters after  at the end of i command

我似乎无法弄清楚是什么导致了问题.我正在使用 OSX,这就是为什么我有一个空的 ' ' 作为我的扩展名.

I can't seem to figure out what is causing the problem. I'm using OSX, which is why I have an empty ' ' as my extension.

谢谢!

推荐答案

你应该在 后直接放置一个换行符:

You should put a newline directly after the :

sed '3i
text to insert' file

这实际上是 POSIX 规范定义的行为.GNU sed 允许您指定要在同一行插入的文本这一事实是一个扩展.

This is actually the behaviour defined by the POSIX specification. The fact that GNU sed allows you to specify the text to be inserted on the same line is an extension.

如果由于某种原因你需要在 sed 命令周围使用双引号,那么你必须在第一行的末尾转义反斜杠:

If for some reason you need to use double quotes around the sed command, then you must escape the backslash at the end of the first line:

sed "3i\
text to insert" file

这是因为shell首先处理了一个双引号字符串,然后删除了后跟一个换行符:

This is because a double-quoted string is processed first by the shell, and followed by a newline is removed:

$ echo "abc
def"
abcdef

这篇关于sed 插入行命令 OSX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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