Avoiding square brackets inside brackets in grep

I want to match characters other than square brackets in a regular expression.

Exactly I want to match some special characters and some others not, so I want to specify them

# grep $'[^a-zA-Z0-9#\\/:!<>{},=?. ()["_+;*\'&|$-]' file

This is missing ], I tried to screen using \], \\]etc. I read that people do this outside [^], but I need it inside!

+4
source share
1 answer

Place ]immediately after ^.

Here is an example. The input file "foo" contains:

foo
[
bar
]
baz
quux

We execute the command:

grep '[^][]' foo

Output:

foo
bar
baz
quux

POSIX:

(']') , ( ('^'), ).

:

'.', '*', '[' '\' (, , ) .

+8

Source: https://habr.com/ru/post/1525837/


All Articles