Pergunta

I have a file

cat /tmp/b
A
Quick
Brown
Fox
Killed
2 Hens 

If I match on [0-9] then I get the expected

cat /tmp/b | grep "[0-9]"
2 Hens

But if I try to match on the digit character class I get the unexpected

cat /tmp/b | grep "\d"
Killed

This continues irrespective of what combination of things that I think might work I try

cat /tmp/b | grep "\\d"
Killed
cat /tmp/b | grep "\\d"
Killed
cat /tmp/b | grep "\\\\d"
{No matches}
cat /tmp/b | grep "\\\d"
{No matches}
cat /tmp/b | grep "[\\d]"
Killed
cat /tmp/b | grep "[d]"
Killed
cat /tmp/b | grep "[\\\\d]"
Killed   

Does anyone know what I am doing wrong and how to get Character Classed working in cygwin sed

Update

cat /tmp/b | grep [:digit]
Quick
Killed

Update 2

cat /tmp/b | grep [[:digit:]]
2 Hens
Foi útil?

Solução

You are specifying the character class incorrectly. The manual would tell you:

  • A bracket expression is a list of characters enclosed by [ and ]
  • Certain named classes of characters are predefined within bracket expressions:
    • [:digit:] Digits: 0 1 2 3 4 5 6 7 8 9.

This would imply that in order to match a [:digit:] you need to say [[:digit:]].

You'll find:

$ cat input | grep -o '[[:digit:]]'
2

Outras dicas

I'm pretty sure that cygwin uses the GNU grep regex, which doesn't support the \d abbreviation, but does support [:digit:]. As an aside, I can't explain why you're matching the d in the case where you have multiple backslashes preceding the d outside of the character class brackets.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top