Computing.Net > Forums > Unix > contextual GREP in Unix

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

contextual GREP in Unix

Reply to Message Icon

Name: gpkarthik
Date: February 24, 2004 at 07:56:28 Pacific
OS: solaris/unix
CPU/Ram: 800Mhz
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: aigles
Date: February 24, 2004 at 10:47:20 Pacific
Reply:


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 -f

function 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.


0

Response Number 2
Name: aigles
Date: February 24, 2004 at 23:31:53 Pacific
Reply:

more readable :


#!/usr/bin/awk -f

function 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.


0

Response Number 3
Name: Dlonra
Date: February 25, 2004 at 06:17:11 Pacific
Reply:

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,.+5p

A problem with this is that if there are not 10 lines before "pattern", ex silently fails.


0

Response Number 4
Name: aigles
Date: February 25, 2004 at 08:20:31 Pacific
Reply:

Typing error :
g/pattern/.-10,.p


There is some problems with your solution

With the following input file :
1 SUCCES
2 SUCCES
3 ERROR
4 SUCCES
5 SUCCES
6 ERROR
7 ERROR
8 SUCCES

The 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^[[?25h

1) There are not wanted display (escape seq, file name )
2) Some lines are displayed many times

The correct result is :
1 SUCCES
2 SUCCES
3 ERROR
4 SUCCES
5 SUCCES
6 ERROR
7 ERROR

Jean-Pierre.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Shell Scripting -DateTime... Display Java swings on HP...



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: contextual GREP in Unix

grep for proximity? www.computing.net/answers/unix/grep-for-proximity/6037.html

How 2 find java script used in UNIX www.computing.net/answers/unix/how-2-find-java-script-used-in-unix/8124.html

Need drivers for DFE-530TX in UNIX! www.computing.net/answers/unix/need-drivers-for-dfe530tx-in-unix/2251.html