【发布时间】:2020-04-29 15:47:59
【问题描述】:
我搜索了一下,在这个link找到了答案
但是当我尝试执行此命令sed -i '5ianything' file1.txt 时,它不起作用并显示错误消息:
sed: 1: "file1.txt": invalid command code f
当我使用另一个命令sed -i '5i\anything' file1.txt时也会出现同样的错误信息
我不知道为什么它对我不起作用我错过了什么吗? 有没有其他方法可以做到这一点?
编辑
我已经尝试了 cmets 中提供的解决方案并运行了这个命令,但不幸的是,即使这样也行不通
sed -i.bu '5i\anything' file1.txt
sed: 1: "5i\anything": extra characters after \ at the end of i command
这是我的 file1.txt :
1. alloha I want some cheese
2.
3. this is just a test and you know it
4.
5. this line will change sooooo sooooooon
6.
7. daaaamn, it changed I mean that line above me daaaaamn
编辑
在我在 sed 的 OSX 手册页中搜索了一下之后,我发现了这个:
-i extension
Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no backup will be saved. It is not recommended
to give a zero-length extension when in-place editing files, as you risk corruption or partial content in situations where disk space is exhausted, etc.
【问题讨论】:
-
您使用的是最新版本的 gnu sed 吗?
sed -V说什么? -
@match 实际上甚至 sed -V 在我的终端中也不起作用,并且我收到错误消息“sed:非法选项 -- V”我在 Mac 中工作
-
this 是您要找的吗?
-
@Chipster 不,即使那对我也不起作用
-
您需要在 \ 之后换行
标签: bash macos shell sed command-line