find a word and print n lines before and after the match

how to find a word and print n lines before and after the match until a blank line is encounterd

Homework ?? What is your try ..

i googled and got some info..but cant put all together to make it work.. grep -n will give the line number of the word i am searching for .. and i took that line number in awk '{$value+1}' but this didnt work..

then saw getline utility in awk but it prints only one line..

If not GNU grep ..

Before 5 lines ..

sed -n '/pattern/q;p' infile | tail -5

After 5 lines ..

nawk 'c-->0;/pattern/{print;c=5}' infile

Which OS?

try grep -A and grep -B if those are available for you.

solaris 10

grep -A and -B are for gnu tool right?

what i exactly want is..

G
M

A
B
C
D

E
F

Assume the above is the file content. i want to find for the letter 'B' and print lines above
and below it until a blank line is encountered.

so my result should be

A
B
C
D

on solaris, grep -A and grep -B should work.

try

/usr/xpg4/bin/grep 

it says illegal option -A

Hi.

The standard grep and xpg4/ grep do not admit -A -B options, at least on:

OS, ker|rel, machine: SunOS, 5.10, i86pc
Distribution        : Solaris 10 10/08 s10x_u6wos_07b X86

In addition, the OP appears to looking for bounding lines that are not the result of a count, but rather a pattern -- a blank line in this case.

One solution is cgrep, but one needs to obtain the source and compile it. I did that and keep it in a local directory. Here is a sample showing how it works on Linux:

#!/usr/bin/env bash

# @(#) s1	Demonstrate extract blank-line-bounded block, cgrep.
# http://sourceforge.net/projects/cgrep/

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
cgrep -D +-w "^$" -e "B" $FILE

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39

-----
 Input data file data1:
G
M

A
B
C
D

E
F

X
B
Z




-----
 Results:

A
B
C
D


X
B
Z

If you could get by with -A and -B, there are alternatives:

ack - grep-like text finder, a perl code
http://betterthangrep.com/
glark - Search text files for complex regular expressions, a ruby code
http://www.incava.org/projects/glark/

Best wishes ... cheers, drl

1 Like

is there any other way.. like awk or perl .. i was hoping this can be done with a oneliner :frowning:

look into this thread.

1 Like

That works but in my case the creteria is blank line.. the extraction of lines both above and below the pattern should end when it encounters blank line..

given myFile:

G
M

A
B
C
D

E
F

nawk '/B/' RS= myFile produces:

A
B
C
D
2 Likes

awesome!!! i nvr thought the one liner would be so simple.. thanks a ton :smiley:
so here the record separator is blank line thats why you have give RS=

is that so ?

correct.

1 Like