Re: grep and | (128 Views)
Reply
Occasional Contributor
Posts: 8
Registered: ‎01-09-2007
Message 1 of 3 (128 Views)
Accepted Solution

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
Acclaimed Contributor
Posts: 25,717
Registered: ‎03-06-2006
Message 2 of 3 (128 Views)

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
Posts: 8
Registered: ‎01-09-2007
Message 3 of 3 (128 Views)

Re: grep and |

Thank you very much. :)
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.