I need to return results for two different matches from a single file.
grep "string1" my.file
correctly returns the single instance of string1 in my.file
grep "string2" my.file
correctly returns the single instance of string2 in my.file
but
grep "string1|string2" my.file
returns nothing
in regex test apps that syntax is correct, so why does it not work for grep in cygwin ?
Using the
|character without escaping it in a basic regular expression will only match the|literal. For instance, if you have a file with contentsUsing
grep "string1|string2" my.filewill only match the last lineIn order to use the alternation operator
|, you could:Use a basic regular expression (just
grep) and escape the|character in the regular expressiongrep "string1\|string2" my.fileUse an extended regular expression with
egreporgrep -E, as Julian already pointed out in his answergrep -E "string1|string2" my.fileIf it is two different patterns that you want to match, you could also specify them separately in
-eoptions:grep -e "string1" -e "string2" my.fileYou might find the following sections of the
grepreference useful:-e