Highlighting a character with close brackets in a grep pad

How to avoid closing parenthesis character inside character class? According to the manual, to match the literal] it should be placed first on the list, but I need to match the complement of the class consisting of a square bracket.

Most metacharacters lose their special meaning within parenthesized expressions. To include a literal] put it first on the list. Similarly, to include the literal ^ place it anywhere, but first. Finally, to include a literal, put it last.

+4
source share
3 answers

How to avoid closing parenthesis character inside character class?

A ] POSIX, .

a ] ( ), , .

echo "foo [22-Jun-2014 04:11:37 UTC] bar" | grep -o '\[[^]]*\]'
# [22-Jun-2014 04:11:37 UTC]
+7

.

  • grep -P [][] char, , .
  • , , : [\]] [\[]

"[^] []" ?

+1

, zx81 . , :

[22-Jun-2014 04:11:37 UTC]

grep -o '.[^[\]]*' 

.

That the job was not at all in the closing bracket:

grep '^[[][^]]*]'

It seems that grep still considers the closing bracket first, even if it is preceded by a carriage.

+1
source

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


All Articles