Skip to main content
added 355 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60
$ 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.

With any awk script if you'd like to see it with different formatting and less reliance on defaults to make it a bit less brief and more clear, you can pretty-print it using GNU awk (note: it must be gawk, not some other awk variant) as awk -o- ...:

$ awk -o- -v t=34897 '/^\[/{f=($4=="T("t")")} f' file /^\[/ { f = ($4 == "T(" t ")") } f { print } 
$ 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.

$ 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.

With any awk script if you'd like to see it with different formatting and less reliance on defaults to make it a bit less brief and more clear, you can pretty-print it using GNU awk (note: it must be gawk, not some other awk variant) as awk -o- ...:

$ awk -o- -v t=34897 '/^\[/{f=($4=="T("t")")} f' file /^\[/ { f = ($4 == "T(" t ")") } f { print } 
added 51 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60
$ 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 ff is set to true (1), otherwise false (0). When every line is read if f is true theat that time then that current line is printed.

$ 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, otherwise false. When f is true the current line is printed.

$ 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.

Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

$ 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, otherwise false. When f is true the current line is printed.