I have a file test.txt
which contains
ERROR [x]
If I try
grep -Eo "ERROR \[[^p]\]" test.txt
it matches ERROR [x]
.
But if I do:
grep -Eo "ERROR \[[^\]]\]" test.txt
which is trying to match ERROR [
followed by any character expect literal ]
, it does not match.
It should, since x
is not a ]
.
What am I missing ?
You don't need to escape the ]
string in the match group. The correct expression is the following:
ERROR \[[^]]\]
Test:
echo "ERROR []]" | grep -Eo "ERROR \[[^]]\]" # no match
echo "ERROR [x]" | grep -Eo "ERROR \[[^]]\]" # match
echo "ERROR [a]" | grep -Eo "ERROR \[[^]]\]" # match
echo "ERROR [abcdef]" | grep -Eo "ERROR \[[^]]\]" # no match
Tested on macOS 15.5.