logging - print a group of the first consecutive lines matching a regex then exit -


i print lines in log file filtered current time.

the setup have right reads entire file , prints lines match. how should stop sed reading entire file while still printing multiple lines.

normally lines need 3 lines deep log , multiple lines long.

currently use:

timestamp=`date +"%y-%m-%d %h:%m:%s"` tac mylogfile | gsed -n "/^$timestamp/p" | tac 

tac bit superfluous in simple demo

example data:

2013-08-19 19:23:42 2013-08-19 19:23:42 2013-08-19 19:23:42 2013-08-19 19:23:42 2013-08-19 19:23:43 2013-08-19 19:23:43 2013-08-19 19:23:43 2013-08-19 19:23:44 2013-08-19 19:23:44 2013-08-19 19:23:44 2013-08-19 19:23:44 2013-08-19 19:23:45 2013-08-19 19:23:45 2013-08-19 19:23:45 2013-08-19 19:23:46 2013-08-19 19:23:46 2013-08-19 19:23:46 2013-08-19 19:23:46 2013-08-19 19:23:46 2013-08-19 19:23:46 2013-08-19 19:23:46 2013-08-19 19:23:46 

sed excellent tool simple substitutions on single line else, use awk:

awk -v ts="$timestamp" '$0 ~ ts{print; f=1; next} f{exit}' file 

Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -