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 file fileA.txt

Batman.plist
Green Arrow.plist
Hawkgirl.plist
EOPrototypes.plist
Person.plist
EOPrototypes.plist
EOJavellinPrototypes.plist
Sinestro
Slomon Grundy.plist
Batman Beyond.plist
EORedRobin
EORavenPrototypes.plist

Now if I want to get all the lines that end with plist and does not contain the word Prototype. So far I have

grep -v "Prototype" fileA.txt | grep -E "*plist$"

And the output is

Batman.plist
Green Arrow.plist
Hawkgirl.plist
Person.plist
Slomon Grundy.plist
Batman Beyond.plist

Which is exactly what I want,

But is there a better way to do this?

share|improve this question
    
The second half should just be grep 'plist$'. * isn't a shell glob in a regex, and it's unnecessary, and there's nothing there that needs an ERE. –  Kevin Jun 20 at 14:53
    
What do you mean by ERE ? –  Unknown Jun 20 at 14:54
    
Extended Regular Expression, which is why you would use the -E flag. –  Kevin Jun 20 at 14:55
    
I knew Extended Regular Expression, never used ERE before :$ But now I know what it stands for ;) –  Unknown Jun 20 at 14:56

3 Answers 3

up vote 7 down vote accepted
grep -v Prototype | grep 'plist$'

is probably as good as it gets. You could do it with one command with sed or awk (or with non-standard extensions to grep as others have already shown):

sed '/Prototype/d;/plist$/!d'

Or

awk '/plist$/ && ! /Prototype/'

But that's not necessarily going to be more efficient.

share|improve this answer

Try this

grep -P '^(?!.*Prototype).*plist$' fileA.txt
share|improve this answer
    
Yes. You are right. I missed a Prototype.plist in testing set. –  dchirikov Jun 20 at 16:31

If the Prototypes string always exactly prefixes the .plist string as shown in your example, and your platform's version of grep supports PCRE mode, you could use a perl-style negative lookbehind such as grep -P '(?<!Prototypes)\.plist$' e.g.

$ grep -P '(?<!Prototypes)\.plist$' fileA.txt
Batman.plist
Green Arrow.plist
Hawkgirl.plist
Person.plist
Slomon Grundy.plist
Batman Beyond.plist
share|improve this answer
    
N.B. grep on OSX >= 10.8 doesn't include -P. –  Kevin Jun 20 at 14:56

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.