Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi!
I want to know if there is any way to substitute the contextual grep (-C) function of Linux in UNIX.
My problem is that I need the 10 lines preceiding my grep search in a log file?
Any suggestions?
Thanks!
kart

The following script search file(s) for a string (ereg specification) and print context lines (before and after).Usage:
grep_with_context [ -v before=before_lines ] [ -v after=after_lines ][ -v context=context_lines ] ereg file...
before_lines : number of lines to display after line containing ereg
after_lines : number of lines to display after line containing ereg
context-lines: number of lines to display before and after line containing ereg
ereg : searched string as a regular expression
file.. : input file(s)Example:
grep_with_context -v before=10 'ERROR' input_file
------ grep_with_context ------
#!/usr/bin/awk -ffunction PrintBeforeContext ( lindex, lfrom, lto) {
lfrom = before_index - before;
if (lfrom < 0) lfrom = 0 ;
lto = before_index - 1 ;
for ( lindex = lfrom ; lindex <= lto ; lindex++ ) {
print before_context[lindex % before] ;
}
before_index = 0 ;
}BEGIN {
context = context + 0;
before = before + 0 ;
after = after + 0 ;
if (context > 0) {
before = context;
after = context;
}
before_index = 0 ;
before_context[0] = "";
after_index = 0 ;
pattern = ARGV[1];
ARGV[1] = "";
if (ARGC <= 2) ARGV[ARGC++] = "-";
}$0 ~ pattern { PrintBeforeContext() ;
print $0 ;
after_index = after ;
next ;
}after_index > 0 {
print $0 ;
after_index-- ;
next ;
}before > 0 {
before_context[before_index % before] = $0 ;
before_index++ ;
}
------ grep_with_context ------
Jean-Pierre.

more readable :
#!/usr/bin/awk -ffunction PrintBeforeContext ( lindex, lfrom, lto) {
lfrom = before_index - before;
if (lfrom < 0) lfrom = 0 ;
lto = before_index - 1 ;
for ( lindex = lfrom ; lindex <= lto ; lindex++ ) {
print before_context[lindex % before] ;
}
before_index = 0 ;
}BEGIN {
context = context + 0;
before = before + 0 ;
after = after + 0 ;
if (context > 0) {
before = context;
after = context;
}
before_index = 0 ;
before_context[0] = "";
after_index = 0 ;
pattern = ARGV[1];
ARGV[1] = "";
if (ARGC <= 2) ARGV[ARGC++] = "-";
}$0 ~ pattern { PrintBeforeContext() ;
print $0 ;
after_index = after ;
next ;
}after_index > 0 {
print $0 ;
after_index-- ;
next ;
}before > 0 {
before_context[before_index % before] = $0 ;
before_index++ ;
}Jean-Pierre.

As I noted in response to this posting in the Linux Forum:
how about ex:
ex <<! file
g/pattern/.,.-10p
q
!This corrects the Linux posting which had ".+10p"
To get 5 before and 5 after g/pattern/.,.-5,.+5pA problem with this is that if there are not 10 lines before "pattern", ex silently fails.

Typing error :
g/pattern/.-10,.p
There is some problems with your solutionWith the following input file :
1 SUCCES
2 SUCCES
3 ERROR
4 SUCCES
5 SUCCES
6 ERROR
7 ERROR
8 SUCCESThe result for g/ERROR/.-2,.p is :
^[[24;1H"u.txt" 8 lines, 109 characters
1 SUCCES
2 SUCCES
3 ERROR
4 SUCCES
5 SUCCES
6 ERROR
5 SUCCES
6 ERROR
7 ERROR
^[[J^[[?25h1) There are not wanted display (escape seq, file name )
2) Some lines are displayed many timesThe correct result is :
1 SUCCES
2 SUCCES
3 ERROR
4 SUCCES
5 SUCCES
6 ERROR
7 ERRORJean-Pierre.

![]() |
Shell Scripting -DateTime...
|
Display Java swings on HP...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |