I would like to print 3 lines after a regular expression is found in the logfile. I'm using the following code:
grep -n "$reg_exp" file.txt |while read LINE ;do i=$(echo $LINE |cut -d':' -f1 ) ;sed -n "$i,$(($i+3))p" file.txt ;done
The above code things works fine,but sometimes gives erroneous results. In my case i'm searching for the expression X104 in the logfile. Sometimes X104 will contain only 1 line to print and after printing that .It also prints the next 3 lines after that, e.g:
(i)11/02/09 02:21:15 X104
Text : Unable to unlink/erase file
System Error Number: 13 (d) - The data is invalid.
(ii) Text : X104 disabled.
(iii) 11/03/09 01:01:03 web200
Text : Freespace on C: is at a critical level (694.55MB).
Text : X104 enabled per scheduled_reboot.
NSLOOKUP was successful. Please reload your
configuration.
(i) : I get the desired results, regexp is found and the next 3 lines are printed
(ii) : regexp is matched (contains only 1 line) and printed, but I receive the next 3 lines (including the newline) which I find not useful for me.
Can anyone help me out