How to Grep of by section?

I have a script that outputs this as a file

John Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
memberOf: example4;sampletest;test
A Member of 4 Groups
 
Sally Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
A Member of 3 Groups
 
Toby Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
memberOf: example4;sampletest;test
memberOf: example5;sampletest;test
A Member of 5 Groups

I want to make it so that if some one uses (ex. grep Toby Smith) on the file, than they will get the name aswell as their memberships. Also, I can only use grep, so is there anything i can do in the script to allow grep to gather all the information for the each member.

Smash the group of lines into single lines and post process the grep to put it back.

Consider (assumes a Unix/Linux line terminated format in my case in a file called groups.txt):

sed -e 's/^$/^B/' <groups.txt |  tr '\012\002' '\011\012' | sed 's/^\o011//' | grep '^Sally Smith' | tr '\011' '\012'

You'll want the data to end with an empty line.

(oh and ^B is a Ctrl-B, I should have been more consistent in my approach)

Check this ,it may help you

awk  'BEGIN{RS="----------------"}/John/{print;getline;print}'  file 

Hi.

An option in perl, -00, allows processing by paragraphs, provided they are separated by empty lines:

#!/usr/bin/env bash

# @(#) s1	Demonstrate processing paragraphs of lines, perl.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
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 perl

PATTERN=${1-Sally}
FILE=data1

pl " Input data file $FILE, looking for \"$PATTERN\":"
cat $FILE

pl " Results:"
perl -00 -wn -e " print if m{$PATTERN}" $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 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
perl 5.10.0

-----
 Input data file data1, looking for "Sally":
John Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
memberOf: example4;sampletest;test
A Member of 4 Groups

Sally Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
A Member of 3 Groups

Toby Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
memberOf: example4;sampletest;test
memberOf: example5;sampletest;test
A Member of 5 Groups

-----
 Results:
Sally Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
A Member of 3 Groups

This also allows constructs such as:

$ ./s1 'John|Toby'

producing:

 Results:
John Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
memberOf: example4;sampletest;test
A Member of 4 Groups

Toby Smith
----------------
memberOf: example1;sampletest;test
memberOf: example2;sampletest;test
memberOf: example3;sampletest;test
memberOf: example4;sampletest;test
memberOf: example5;sampletest;test
A Member of 5 Groups

Best wishes ... cheers, drl

How come that you can only use grep? Unfortunately, grep by itself does not have the functionality you are hoping for.

You make reference to a script. Could you post what you have been working on?