Reputation: 345
Im trying to search for a pattern in a file as follows:
SRC_ERROR_CODE=105
SRC_ERROR_CODE=106
...
To achieve this following is the grep statement used:
grep -io "[a-z]*_error_code=[0-9]*" events.log
However i was wondering if instead of using the "*" which fetches 0 to n occurrences of the preceding matched character, the "+" should fetch the same results as well as below:
grep -io "[a-z]+_error_code=[0-9]+" events.log
But, this doesn't seem to work. Could you please guide as to why it doesn't.
Thanks
Upvotes: 0
Views: 72
Reputation: 8402
The +
in regex matches one or more character in Extended Regular Expression (ERE - this is egrep)
In Basic regular expression (BRE) you have to escape the +
sign.
Since you are using grep
, you have to escape the +
. Therefore, use \+
instead of +
.
If you are using egrep
, you can use the unescaped +
Upvotes: 0
Reputation: 123650
In POSIX Basic Regular Expressions (BRE), the default regex dialect used by grep
, +
just matches itself.
In POSIX Extended Regular Expressions (ERE) and Perl Compatible RegEx (PCRE), +
matches 1 or more of the preceding atom.
You can ask grep
to use ERE with the -E
option:
$ echo "foo baaar" | grep -o -E 'a+'
aaa
Upvotes: 2