Grep cannot find all matches for pattern "\ [\ [\ [\ ["

I am having problems using grep with a pipe. The script is as follows: I am running a python script that prints (using print) to the screen debugging messages. I use ./prog | grep "\[\[\[\["to catch strings using "[[[["] in them. It returns several matching results, but not others (another observation: the results found by grep are presented before the results were not found by grep in the file). I ran ./progwithout pipe and grep and printed all the lines using "[[[[[" "pattern.

+3
source share
2 answers

The problem is that the left square bracket is a special character in regular expressions. "grep" is not just a string match. Regular expressions are an involved language that allows you to describe text patterns. Grep tries to interpret it [[[[as a regular expression, not just a string.

As your question indicates, you can avoid special characters with backslashes. So the following may work:

./prog | grep '\[\[\[\['

You can also “avoid” square brackets by placing them in square brackets. This way, [[][[][[][[]or [[]{4}if your version of grep processes it.

, ./prog " " " ". stderr :

./proc 2>&1 | egrep '[[]{4}'

UPDATE:

[ghoti@pc ~]$ printf '[[[[\n[[[\n[[[[\n[[[[[\n[[\n' | grep '\[\[\[\['
[[[[
[[[[
[[[[[
[ghoti@pc ~]$ printf '[[[[\n[[[\n[[[[\n[[[[[\n[[\n' | egrep '[[]{4}'
[[[[
[[[[
[[[[[
[ghoti@pc ~]$  

, . , , .

+3

stderr, stdout; stdout. ( " " .) stderr stdout :

./prog 2>&1 | grep '\[\[\[\['
+2

All Articles