Tuesday, February 1, 2011

Grep AND/OR pattern matching

Simple once you understand it, lots of people ask about it:

#Return instances of lines containing string1 AND string2
grep string1.*string2 filename
What is happeneing here is that you are searching for string1, zero or more (*) of any characters (.) and then string2. NOTICE: it will only return lines that have string1 before string2

#Return instances of lines containing string1 OR string2
grep "string1|string2"
Classical OR operator, the pipe. This will test for lines containing either string1 or string2 and return those lines.