imadirtycunit
imadirtycunit

Reputation: 125

How can I grep for multiple patterns at once?

I am looking for two separate patterns each having a specific constraints within some large compressed files. I can't for the life of me figure out how to grep for both cases at once.

For an example, a file daily records that add up over time:

myfile

AnimalID-Type-Mood

Animal456-Dog-happy-day1
Animal453-Elephant-happy-day1
Animal896-Dog-sad-day1
Animal405-Dog-angry-day1
Animal443-Goat-angry-day1
Animal453-Dog-sad-day1
Animal473-Cat-sad-day1
Animal452-cat-happy-day1
Animal456-Dog-angry-day2
Animal453-Elephant-sad-day2
Animal896-Dog-happy-day2
Animal405-Dog-angry-day2
Animal443-Goat-happy-day2
Animal453-Dog-happy-day2
Animal473-Cat-happy-day2
Animal452-cat-happy-day2

So far I have tried

zcat.myfile | grep -e  'Dog|happy' | grep -e 'Cat|happy'

Essentially I am trying to find how many days out of the year, by AnimalID, that a dog is happy and a cat is happy. I can do counts and sorts, I just can't figure out how to run that

Upvotes: 5

Views: 1998

Answers (4)

glenn jackman
glenn jackman

Reputation: 247210

You can specify multiple pattern with the -e option

$ seq 10 | grep -e 5 -e 6
5
6

Upvotes: 4

anubhava
anubhava

Reputation: 786101

You can use zgrep here:

zgrep -iE '(Dog|Cat)-happy' file

Upvotes: 4

hek2mgl
hek2mgl

Reputation: 158250

You can do it with a basic regex:

grep -i '\(Dog\|Cat\)-happy' input

Upvotes: 2

Jens
Jens

Reputation: 72756

It's egrep or grep -E for extended regular expression with alteration using (a|b) for a or b. Note capital E.

Upvotes: 2

Related Questions