Searched for a solution on SO, Ask and Unix/Linux but I am lost trying to figure out how to write a conditional with a grep range:
Go through:
1-20_something
100-200_something
2-100_something
11-333_something
code works but only on 1-9_something
:
if grep -q '[0-9]-[0-9]_something' "$foobar"; then
echo "Additional Code"
fi
doesn't work:
if grep -q '\d{1,3}-\d{1,3}_something' "$foobar"; then
echo "Additional Code"
fi
and this doesn't work:
if grep -q '[0-9]{1,3}-[0-9]{1,3}_something' "$foobar"; then
echo "Additional Code"
fi
What is the best way to range through {1,3}
?
EDIT:
To help the next person that may run across this I did a lot of searching and after the answers given I was able to result:
Curly brackets
{
}
are treated as literals in BRE (Basic Regular Expression). Fromman grep
:So you need either
or enable Extended Regular Expression mode with the
-E
switchYour second attempt should work if you enabled PCRE (Perl-compatible regular expressions) using
-P
:\d
is not in BRE or ERE or in the list of extra backslash expressions that GNU Grep has, but it is a Perl thing.Your third attempt should work if you enabled ERE (extended regular expressions):
<exp>{m,n}
is not a BRE (basic regular expression) construct.