Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

Could you please help me around with.

command file (sedcommands)

/DOG,29-APR-2015/d

test1.txt

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

command

sed -f sedcommands test1.txt > test2.txt
share|improve this question

4 Answers 4

up vote 4 down vote accepted

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.

share|improve this answer
2  
Note that your command requires GNU sed. You need sed '/DOG/{/29-APR-2015/d;}' for POSIX. –  cuonglm 18 hours ago
    
Thanks very much it worked. –  Gopal 18 hours ago

/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.

share|improve this answer

POSIXly:

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

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

or:

sed '/DOG/{/29-APR-2015/d;}' file
share|improve this answer

The command file should contain:

/DOG *29-APR-2015/d

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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