$ awk -v t=34897 '/^\[/{f=($4=="T("t")")} f' file [12] 03/31/21 08:33:30.080851 T(34897) _DBG message y 1 [12] 03/31/21 08:33:31.457612 T(34897) _DBG message y 2 test message line 2 test message line 3 test message line 4 [12] 03/31/21 08:33:32.56341 T(34897) _DBG message y 3 [12] 03/31/21 08:33:33.78123 T(34897) _DBG message y 3 test message line 2 [12] 03/31/21 08:33:34.56712 T(34897) _DBG message y 4 The above sets a "found" flag f every time it sees a line starting with [. If the 4th field on that line is T(<target value>) then f is set to true (1), otherwise false (0). When every line is read if f is true at that time then that current line is printed.