Mahesh Rajendran Messages: 9793 Registered: March 2002 Location: oracleDocoVille
Senior Member
Depends on the complexity of search.
A simple one-liner with awk is a an old standard.
Any more complex, the real solution is with perl.
Kaapi:ora magvivek$ cat somelog.log
first line
second line
third line
fourth line
fifth line
sixth line above duplicate second line
duplicate second line
Kaapi:ora magvivek$
##author: not me. Shamelessly ripped from an old logRotate.
##b=number of lines before
##a=number of lines after
##s=value
Kaapi:ora magvivek$ awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=1 a=0 s="second" somelog.log
first line
second line
fifth line
sixth line above duplicate second line
sixth line above duplicate second line
duplicate second line