Search code examples
sedbsd

BSD sed: extra characters at the end of d command


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

when I run: (BSD sed)

$ sed '3,6 {/This/d}' pets.txt

It show error:

sed: 1: "3,6 {/This/d}": extra characters at the end of d command

what's wrong with it? when I use gsed(GNU sed), it works well.


Solution

  • 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 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'.