Validate zip code in Shell-Script

0

I am entering this world of regular expressions, I am with a doubt, I read about the metacharacters, etc. Well, I'm trying to validate a zip code and I'm using the following command and I'm not getting it ... I've already replaced \ d with [0-9] and I succeeded, but I can not do that:

cat arquivo.txt | grep -E '^\d{5}[-]\d{3}'

The .txt file has the line: 02954-852

Thank you.

    
asked by anonymous 17.06.2015 / 15:22

3 answers

2

Thanks for replying, but the simplest solution was because \ d is not recognized by the -E parameter but by -P (Perl), the code looks like this:

cat arquivo.txt | grep -P '^\d{5}-\d{3}$'

But thank you very much for your attention.

    
17.06.2015 / 16:04
1

This site has good examples below.

link

echo 'Informe o CEP';
read CEP
echo $CEP | egrep '^[0-9]{5}[-][0-9]{3}$' && echo -e '3[01;32m Número válido! 3[0m' || echo -e '3[01;31m NÃO é válido esse número.3[0m'
    
17.06.2015 / 15:54
1

You may not have closed the dollar sign: Example:

^\d{5}[-]\d{3}$
    
17.06.2015 / 15:59