Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I grep for multiple patterns at once?

Tags:

regex

grep

bash

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

like image 677
imadirtycunit Avatar asked Jan 07 '15 21:01

imadirtycunit


2 Answers

You can use zgrep here:

zgrep -iE '(Dog|Cat)-happy' file
like image 173
anubhava Avatar answered Sep 29 '22 00:09

anubhava


You can specify multiple pattern with the -e option

$ seq 10 | grep -e 5 -e 6
5
6
like image 40
glenn jackman Avatar answered Sep 29 '22 02:09

glenn jackman