#!/bin/sh
while read NAME1 NAME2 NAME3
do
case "$NAME2" in
dining )
read A
read B
read C
echo $A $B $C
;;
plus )
read A
read B
read C
echo $A $B $C
;;
* )
exit 1
;;
esac
done <data.file
The GNU grep can easily do this. However, AT&T offers cgrep which also can do "context" printing. One might want to use this approach for files that are large enough so that shell scripts might be too slow, or where you need additional features:
#!/bin/bash --posix
# @(#) s1 Demonstrate AT&T free cgrep for grep with context.
set -o nounset
echo
debug=":"
debug="echo"
## Use local command version for the commands in this demonstration.
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash cgrep
set -o posix
set -o | grep -i posix
echo
FILE=${1-data1}
cgrep +3 "dining" $FILE
exit 0
Producing on your data in file "data1":
% ./s1
(Versions displayed with local utility "version")
GNU bash 2.05b.0
cgrep (local) - no version provided.
posix on
acct dining mem
open 0 50
dep 50 0
close 255 0
The AT&T source for cgrep is available at: cgrep home page -- I had no trouble downloading and compiling it on GNU/Linux, but I cannot vouch for HPUX.
It is a very useful command that can take on jobs that GNU grep normally does, plus a lot more. Best wishes ... cheers, drl
Yuk! That won't work at all. It assumes that the search term "string" is found only once in the datafile. If it is found more than once, then $val will have a multi-line and cause any 'expr $val' to fail with a syntax error.
acct dining mem
open 0 50
dep 50 0
close 255 0
acct plus mem
open 100 100
dep 50 0
close 323 0
acct dining mem
open 0 50
dep 50 0
close 255 0
acct plus mem
open 100 100
dep 50 0
close 323 0
output:
open 0 50
dep 50 0
close 255 0
open 0 50
dep 50 0
close 255 0
code:
awk '
{
if ($2=="dining")
n=NR
if (NR>=n+1 && NR<=n+3)
print
}' a > c
cat c