1747980 Members
4022 Online
108756 Solutions
New Discussion юеВ

grep and |

 
SOLVED
Go to solution
Markus P├╢stinger
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
2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

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]$"
Markus P├╢stinger
Occasional Contributor

Re: grep and |

Thank you very much. :)