print all between patterns with special chars

Hi, I'm having trouble with awk print all characters between 2 patterns. I tried more then one solution found on this forum but with no success.
Probably my mistakes are due to the special characters "[" and "]"in the search patterns.

Well, have a log file like this:

logfile.txt

[0]abcdef[0]patt1[0] abcdef patt2[0]
abcdefabcdefabc[0]defabcde[0]fabcdef
abcdef abcdefabcdefabcdefabcdefabcdef patt1[0]patt1 I WANT READ THIS patt2[0]patt2 abcdefabcdefabcdefabcdefabcdef abcdefabcdefabcdefabcdefabcdef
abcdefabcdefabcdefabcdefabcdef
abcdef abcdefabcdefabcdefabcdefabcdef patt1[0]patt1 I WANT READ ALSO THIS patt2[0]patt2 abcdefabcdefabcdefabcdefabcdef abcdefabcdefabcdefabcdefabcdef
[0]abcdef[0]patt1[0] abcdef patt2[0]
abcdefabcdefabc[0]defabcde[0]fabcdef
[0]abcdef[0]patt1[0] abcdef patt2[0]
abcdefabcdefabc[0]defabcde[0]fabcdef
abcdef abcdefabcdefabcdefabcdefabcdef patt1[0]patt1 I WANT READ THIS TOO patt2[0]patt2 abcdefabcdefabcdefabcdefabcdef abcdefabcdefabcdefabcdefabcdef
abcdefabcdefabcdefabcdefabcdef
[0]abcdef[0]patt1[0] abcdef patt2[0]
abcdef abcdefabcdefabcdefabcdefabcdef patt1[0]patt1 I WANT READ AGAIN patt2[0]patt2 abcdefabcdefabcdefabcdefabcdef abcdefabcdefabcdefabcdefabcdef

I would like to print all chars between the patterns "patt1[0]patt1" and "patt2[0]patt2" so that my expected output should be like this:

I WANT READ THIS
I WANT READ ALSO THIS
I WANT READ THIS TOO 
I WANT READ AGAIN

I tried using "" and also \ before [ and ] but with no good results.

By the way, my final step should be to pipe this to a tail -f logfile.txt ore something else to have the above output in real time.
Tnx for your help :slight_smile:

Could this help you ?

perl -nle 'print $1 if /patt1\[0\]patt1(.+?)patt2\[0\]patt2/' filename
1 Like
 
/home/ravi>awk '/patt1\[0\]patt1/ && /patt2\[0\]patt2/' input_file | sed 's!.*patt1\[0\]patt1\(.*\)patt2\[0\]patt2.*!\1!'
1 Like

did not expect so fast reply ..TNX A LOT
:D:D:D:D:D:D:D:D:D:D