BSD sed:d 命令末尾的额外字符 [英] BSD sed: extra characters at the end of d command
问题描述
我在 mac OS 上使用 sed 命令,下面是正文.
I use sed command on mac OS, following is the text.
$ cat pets.txt
This is my cat
my cat's name is betty
This is your dog
your dog's name is frank
This is your fish
your fish's name is george
This is my goat
my goat's name is adam
当我运行时:(BSD sed)
when I run: (BSD sed)
$ sed '3,6 {/This/d}' pets.txt
显示错误:
sed: 1: "3,6 {/This/d}": extra characters at the end of d command
这是怎么回事?当我使用 gsed(GNU sed)
时,效果很好.
what's wrong with it? when I use gsed(GNU sed)
, it works well.
推荐答案
最简单的解决方案是在 d
后面加一个分号(显示的空格是可选的):
The simplest solution is to put a semi-colon after the d
(the spaces shown are optional):
sed -e '3,6 { /This/d; }' pets.txt
BSD sed
在布局方面比 GNU sed
更加挑剔.有许多 GNU 扩展不是 BSD sed
的一部分,这就是其中之一.分号不是问题的通用解决方案,尽管它确实解决了许多问题.您还可以将命令分布在多行中,或者将它们放在多个 -e
选项中,或者各种其他技巧".
BSD sed
is fussier about the layout than GNU sed
. There are a number of GNU extensions that are not part of BSD sed
, and this is one. The semi-colon isn't the universal solution to problems, though it does solve many of them. You could also spread the commands out over multiple lines, or put them in multiple -e
options, or various other 'tricks'.
这篇关于BSD sed:d 命令末尾的额外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!