Showing results for 
Search instead for 
Do you mean 

grep and |

SOLVED
Go to Solution
Occasional Contributor

grep and |

Hi there,

I need some help with the grep-Command in combination with the logical OR '|'. The following expression does not work correctly. It should recognize all correct dates in the form MMYYYY between 012000 and 122019.

grep "^\(1[0-2]\)\|\(0[1-9]\)20[0-1][0-9]$"

The problem is definitely the |. I tried it with or without the \, but it does not make any difference. I am using it in a script. The corresponding lines are

#!/bin/sh
[.........]
grepErg=`echo $datum | grep "^\(1[0-2]\)\|\(0[1-9]\)20[0-1][0-9]$"`

Hope you can help me.

Greetings,
Markus
1 ACCEPTED SOLUTIONS
Highlighted
Acclaimed Contributor Acclaimed Contributor

Re: grep and |

grep doesn't take a "|", only egrep.
I recommend you not use grep -E if you can use grep -e xxx -e yyy.
But since you are using tricky "()", you need -E.
I got your query to work with this pattern:
grep -E "^(1[0-2])|(0[1-9])20[0-1][0-9]$"
2 REPLIES
Highlighted
Acclaimed Contributor Acclaimed Contributor

Re: grep and |

grep doesn't take a "|", only egrep.
I recommend you not use grep -E if you can use grep -e xxx -e yyy.
But since you are using tricky "()", you need -E.
I got your query to work with this pattern:
grep -E "^(1[0-2])|(0[1-9])20[0-1][0-9]$"
Occasional Contributor

Re: grep and |

Thank you very much. :)