Using the |
character without escaping it in a basic regular expression will only match the |
literal. For instance, if you have a file with contents
string1
string2
string1|string2
Using grep "string1|string2" my.file
will only match the last line
$ grep "string1|string2" my.file
string1|string2
In 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.file
-
Use an extended regular expression with
egrep
orgrep -E
, as Julian already pointed out in his answergrep -E "string1|string2" my.file
-
If it is two different patterns that you want to match, you could also specify them separately in
-e
options:grep -e "string1" -e "string2" my.file
You might find the following sections of the grep
reference useful:
- Basic vs Extended Regular Expressions
- Matching Control, where it explains
-e