Using awk or sed to find a pattern that has lines before and after it

Dear gurus,
Please help this beginner to write and understand the required script. I am looking for useing awk for sed.

I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings.

  1. Find a string, say user1. Then hash the line containing the string along with 2 lines before and 1 line after.
  2. The script should not hast other lines like user10,11,12 etc.

The file looks like as below.

        start {
                UID 5001
                NAME  user1
                }
        start {
                UID 5002
                NAME  user2
                }
        start {
                UID 5003
                NAME  user3
                }
        start {
                UID 5004
                NAME  user4
                }
        start {
                UID 5005
                NAME  user5
                }
        start {
                UID 5006
                NAME  user6
                }
        start {
                UID 5007
                NAME  user7
                }
        start {
                UID 5008
                NAME  user8
                }
        start {
                UID 5009
                NAME  user9
                }
        start {
                UID 5010
                NAME  user10
                }
        start {
                UID 5011
                NAME  user11
                }
        start {
                UID 5012
                NAME  user12
                }
        start {
                UID 5013
                NAME  user13
                }
        start {
                UID 5014
                NAME  user14
                }
        start {
                UID 5015
                NAME  user15
                }
        start {
                UID 5016
                NAME  user16
                }
        start {
                UID 5017
                NAME  user17
                }
        start {
                UID 5018
                NAME  user18
                }
        start {
                UID 5019
                NAME  user19
                }
        start {
                UID 5020
                NAME  user20
                }

Thanks in advance
Randy

Is this a homework assignment? If not, please show your efforts so far.

for i in user1 user4 usern
do
grep -B2 "$i" file | sed 's/^/#/g'
grep "$i" file | sed 's/^/#/g'
grep -A1 "$i" file | sed 's/^/#/g'
done

Try:

printf '%s\n' user11 user1 user4 | awk '
NR == FNR {
	list[$1]
	next
}
$6 in list {
	printf("%s}\n", substr($0, 1 + (FNR > 1)))
}' - RS='}' file

which, with your sample input file, produces the output:

        start {
                UID 5001
                NAME  user1
                }
        start {
                UID 5004
                NAME  user4
                }
        start {
                UID 5011
                NAME  user11
                }

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

Thanks to all for reply and my sincere apology to Scott for not showing the work.
As I mentioned you the file is pretty big and I am trying to update the source file at the same time too. I did some search and understand that sed buffer option can handle it but frankly could not understand.

Thanks
Randy

perl -ne 'BEGIN{$/="}\n"} /user1$/m and print' example.data

Output:

        start {
                UID 5001
                NAME  user1
                }

Substitute the red for any other you want. If you want several users like user1 and user4, change to /user(1|4)$/m

It works also with UID as well:

perl -ne 'BEGIN{$/="}\n"} /5002$/m and print' example.data

Output:

        start {
                UID 5002
                NAME  user2
                }

You never answered Scott's main question. Is this a homework assignment? If this is not homework, why are we restricted to "useing(sic) awk for(sic) sed"?

And, until now, you never said anything about wanting to update the file. You said you wanted to hash certain lines and did not want to hast(?) other lines. If you want to update certain strings in your file, what replacement text do you want to use? And, why do you care about "hash"ing lines you don't want to change?

How is your script supposed to determine which strings are to be replaced and what replacement text is to be used? Do you intend to hard code values into your script and edit your script every time before you run it? Will you be passing in pairs of command-line arguments specifying old and new text? Will you supply an input file that specifies old and new values? (If so, what will be used as the separator between old and new values in that file? And, what will be the name of that file (or will the name of that file be given as an operand)?)

What shell are you using?

What operating system are you using?