Greetings.
So simple a problem has me stumped. People here are so helpful.
I am trying to match a string containing some fixed text and random digits.
echo blah blah abc123 | grep -o abc
abc
echo blah blah abc123 | grep -o abc[0-9]
abc1
echo blah blah abc123 | grep -o abc[0-9]+
echo blah blah abc123 | grep -o "abc[0-9]+"
echo blah blah abc123 | grep -o "abc[0-9]*"
abc123
echo blah blah abc123 | grep -o abc[0-9]{3}
echo blah blah abc123 | grep -o "abc[0-9]{3}"
The * operator (matches zero or more times) is the only one that works as I would expect.
Why does the + operator (match 1 or more times) not match?
Why does the specific repetition count operator {3} not match?
I am running these examples in a bash shell under Ubuntu 10.10 if it makes a difference.
Thanks so much.
They both work when you escape the special characters:
Unescaped, the regex is looking for a literal
+or{, as you have probably deduced.As to exactly why you have to keep a
*unescaped but you have to escape a+, I’m not sure.