[comp.unix.shell] help me do grep

cshort@nmsu.edu (Spmg*d, Lord of Potted Meat Product) (02/26/91)

hi

i have some large protien files that i am trying to search
for keywords and when they are found, print specific
areas of text around it.

let me be more clear:
this is what my text looks like:

///
entry 
title
date
accession
reference
superfamily
sequence
///

if i get a hit on accession, i would like to print the whole
record.

the reason i am not running this through a database is the file
is about 19megs

thanks


 |----------------------|----------------------                     | 
 |the world is spam and |    Chris Short                            |
 | we are but the key   |          Computing Research Labatory      |
 |--------|-------------|          Box 30001/3 CRL                  |      
          |                        New Mexico State University      |
          |                        Las Cruces, NM 88003-0001        | 
          |      Email: Cshort@nmsu.edu  Fax:505 646 6218|-------------------|
          |      Voice: 505 646 6216                     |SpamKey productions| 
          |                     -------------------------|-------------------|

jik@athena.mit.edu (Jonathan I. Kamens) (02/26/91)

You could do something like this:

#!/bin/sh

if [ $# -ne 2 ]; then
	echo "Usage: $0 pattern filename"
	exit 1
fi

AWK='BEGIN { doprint = 0
	record = ""
}
/\/\/\// { if (doprint) { printf("%s", record) }
	record = ""
	doprint = 0
	next
}
/'$1'/ { doprint = 1 }
{ record = record $0 "\n" }'

awk "$AWK" $2

If you only want to compare your pattern to a certain line of the entry, then
you can count entry lines as you go along and only set doprint to true if the
pattern matches *and* the current line number is the line of the entry against
which you want to compare.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710