Computing.Net > Forums > Unix > Greb -A or -B

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.

Greb -A or -B

Reply to Message Icon

Name: aasemota
Date: October 10, 2005 at 10:59:52 Pacific
OS: AIX
CPU/Ram: xxx
Comment:

The current version of AIX i have does not have the grep -A or -B

I was told there are grep sharewares out there that has this fuctionality. I am using the grep -E Error|Message filename

But I need to be able to capture at least 2 lines before and after.

Can anyone help me please?.

Thank you.



Sponsored Link
Ads by Google

Response Number 1
Name: aasemota
Date: October 10, 2005 at 11:01:13 Pacific
Reply:

Sorry i meant Grep -A or -B


0

Response Number 2
Name: nails
Date: October 10, 2005 at 11:45:38 Pacific
Reply:

This:

grep -A -B

is generally associated with the GNU version of grep, i.e. Linux. You should be able to obtain it at http://www.gnu.org

Here's an earlier thread that discussed using this version of grep:

http://www.computing.net/unix/wwwboard/forum/7114.html


0

Response Number 3
Name: Jim Boothe
Date: October 11, 2005 at 14:45:17 Pacific
Reply:

Here is an awk script that I call cgrep (context grep).  It was originally posted by aigles, and I converted it as noted in the script comments.

Thanks to aigles for posting the original script (I think in Feb 2004).

If context areas overlap, lines will not be duplicated.

# !/usr/bin/ksh
# Script: cgrep
# Author: aigles (Jean-Pierre) from COMPUTING.NET
#
# This script was originally posted by aigles as an #!/usr/bin/awk script.
# I converted to #!/usr/bin/ksh to allow parameters to be entered in the
# form of -B3 instead of -v before=3.
#
# Usage:
# cgrep [ -Bn ] [ -An ] [ -Cn ] pattern file1 file2 ...
#   -Bn       : n lines to display before target line
#   -An       : n lines to display after target line
#   -Cn       : n lines to display before/after target line
#   pattern   : regular expression
#   file1 ... : input file(s)
#
# Example:
# cgrep -C3 'ERROR' input_file

B=0
A=0
C=0

while true
do

if [[ $1 = -B* ]] ; then
   B=${1#-B}
   shift
   continue
fi

if [[ $1 = -A* ]] ; then
   A=${1#-A}
   shift
   continue
fi

if [[ $1 = -C* ]] ; then
   C=${1#-C}
   shift
   continue
fi

break

done

awk -v before=$B -v after=$A -v context=$C '\

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++
}' $*

exit 0


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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: Greb -A or -B

grep -A or -B in Sun os 9 www.computing.net/answers/unix/grep-a-or-b-in-sun-os-9/6468.html

Script to prune a file www.computing.net/answers/unix/script-to-prune-a-file/6823.html

grep command www.computing.net/answers/unix/grep-command/6034.html