I have a text file, let say test1.txt, I need to remove the line containing dog and date with "29-APR-2015"

Using the below command to accomplish it but it is not deleting the lines.

Where as if i mention just with /DOG/d it is deleting the lines containing DOG.

command file (sedcommands)



DOG          29-APR-2015          
DOG          29-APR-2015          
DOG          30-APR-2015          
CAT          29-APR-2015          
CAT          29-APR-2015          


sed -f sedcommands test1.txt > test2.txt

With GNU sed:

sed '/DOG/{/29-APR-2015/d}' test1

This method allows for any order. ie. DOG can either be before or after the date.

  • @cuonglm - sed --posix '/DOG/{/29-APR-2015/d}' works fine in GNU sed version 4.2.1 (with all GNU extensions disabled). – Peter.O Apr 30 '15 at 4:11
  • @Peter.O: No, POSIX require terminator between the command anh }. At least your command won't work in BSD sed. – cuonglm Apr 30 '15 at 4:28
  • @Peter.O, GNU sed's --posix is to tell it to be more POSIX conformant. It's not telling it to report an error on any non-POSIX code you throw at it. It would not make GNU sed more POSIX conformant to report an error when you give it that non-standard {xxx} code. – Stéphane Chazelas May 4 '15 at 16:25


sed -e '/DOG/!b' -e '/29-APR-2015/!b' -e d file

branch to the end if you don't provide any label.


sed '/DOG/{/29-APR-2015/d
}' file

Modern sed implementations also support {command;} form, this's an accepted extension by POSIX, but not required:

sed '/DOG/{/29-APR-2015/d;}' file

/DOG,29-APR-2015/d will not work because there is no comma between DOG and 9-APR-2015. Try this;

$ sed -e '/DOG[[:space:]]*29-APR-2015/d' test1.txt 
     DOG          30-APR-2015          
     CAT          29-APR-2015          
     CAT          29-APR-2015   

[[:space:]]* allows for zero or more white space characters between DOG and 9-APR-2015. The character class [:space:] allows both ordinary spaces and tabs and is safe to use with unicode fonts.


The command file should contain:

/DOG *29-APR-2015/d

That is, DOG followed by 0 or more spaces, followed by specified date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.