Operating System - HP-UX
1833784 Members
2497 Online
110063 Solutions
New Discussion

grep last time "rndc reload" messages plus the next 50 lines

 
SOLVED
Go to solution
Ho_5
Advisor

grep last time "rndc reload" messages plus the next 50 lines

Hi,

The /var/adm/syslog/syslog.log captures several times "rndc reload", but I just want to see the last time rndc reload messages plus the next 20 lines after it.
Do you know how can I do that? with grep command it doesn't perform what I want.

Please give me a tips.

//john
2 REPLIES 2
KapilRaj
Honored Contributor
Solution

Re: grep last time "rndc reload" messages plus the next 50 lines

This is a simple shell script which will do the task. Anyway you may get a onliner from one of our perl experts.

==========================
a=0
cp /var/adm/syslog/syslog.log /tmp/syslog.log
cat /tmp/syslog.log|while read line
do
a=`echo "$a + 1" |bc`
echo $a $line >>/tmp/output$$
done
LASTLINE=`cat /tmp/output$$ |grep 'rndc reload' |tail -1|cut -f 1 -d " "`
TOTALLEN=`wc -l /tmp/syslog.log`
FORTAIL=`echo "$TOTALLINE - $LASTLINE " |bc`
cat /tmp/syslog.log |tail -$FORTAIL|head -20
==========================

Regds,

Kaps
Nothing is impossible
Hein van den Heuvel
Honored Contributor

Re: grep last time "rndc reload" messages plus the next 50 lines


Not a one liner, but an easy to read perl solution below:

- loop through the file
- if you see the target string, forget the array with lines remembered earlier and setup a count of lines to remember.
- if there is a count of lines left to remember, then push lines ($_) into an array
- when no more data (end while) print each of the latests remembered lines from the array.

----------------- x.pl ---------------
while (<>) {
if (/rndc reload/) {
undef @lines;
$more = 50;
}
if ($more) {
push (@lines, $_);
$more--;
}
}
foreach $_ (@lines) {print};
---------------------------------------
perl x.pl /var/adm/syslog/syslog.log