I have this file:
---------------------------
Police name: ora_rac_zax01
OS: solaris
xxxx
yyy
hhhhh
zzz
-------------------------
I want this:
./search_script yyy
Police name: ora_rac_zax01
OS: solaris
xxxx
yyy
hhhhh
zzz
Thanks
#!/usr/bin/ksh
pattern=$1
shift 1
gawk ' BEGIN { RS="---*" }
/'$pattern'/ ' $*
exit
No, that's not how you pass the value of shell variables to an awk
script. See question 24 in the comp.unix.shell FAQ (http://
cfaj.freeshell.org/shell/cus-faq-2.html#24).
Ed.
No, that *is* how he passes shell variables to an AWK script.
It also happens to be how I (usually) do it.
It may not be how you (Ed) do it, but then again, it takes all kinds to
make a world. Diversity is a good thing (both theoretically and
practically/genetically/biologically).
And your script would fail if $1 contains spaces. I would hope that
you actually do this:
gawk ' BEGIN { RS="---*" }
/'"$pattern"'/ ' "$@"
> It may not be how you (Ed) do it, but then again, it takes all kinds to
> make a world. Diversity is a good thing (both theoretically and
> practically/genetically/biologically).
Indeed.
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Then you're leaving yourself open to all sorts of nastiness based on
possible values of that variable.
> It may not be how you (Ed) do it, but then again, it takes all kinds to
> make a world. Diversity is a good thing (both theoretically and
> practically/genetically/biologically).
Diversity is a good thing, but if you see someone hitting a nail with
the claw side of a hammer, they MIGHT appreciate you suggesting they
turn it around rather than quietly applauding their diversity.
Ed.
and then it'd still fail with obscure error messages for some other
values of pattern, e.g.:
$ pattern="foo
bar"
$ gawk ' BEGIN { RS="---*" }
/'"$pattern"'/ ' "$@" file
gawk: cmd. line:1: /foo
gawk: cmd. line:1: ^ unterminated regexp
gawk: cmd. line:3: bar/
gawk: cmd. line:3: ^ unexpected newline or end of string
$ pattern="\\"
$ gawk ' BEGIN { RS="---*" }
/'"$pattern"'/ ' "$@" file
gawk: cmd. line:1: /\/
gawk: cmd. line:1: ^ unterminated regexp
$ pattern="12/25/2008"
$ gawk ' BEGIN { RS="---*" }
/'"$pattern"'/ ' "$@" file
gawk: cmd. line:2: /12/25/2008/
gawk: cmd. line:2: ^ unexpected newline or end of
string
Regards,
Ed.
#!/usr/bin/env awk -f
BEGIN {
searchPattern = ARGV[1]; # First argument is the search pattern
ARGV[1] = ""; # Discard it
count = -1; # Skip processing until the first dash
line
}
/----/ {
# We found the pattern, so print out the information between the
# dash lines.
if (count > 0 && found) {
for (i = 0; i < count; i++) {
print buffer[i];
}
}
# Reset every time we see the dash line
count = 0;
found = 0;
next;
}
count >= 0 {
# Save the lines between the dash lines to the buffer
buffer[count++] = $0;
# We found the pattern we want, flag it
if (index($0, searchPattern) != 0) found = 1;
}
# end of file
While:
$ gawk -v pat="$pattern" ' BEGIN { RS="---*" } $0 ~ pat' "$@"
just returns without an error message, in spite of a bad pattern.
Is that better?
--
Barry Fishman
Yes, see the comp.unix.shell FAQ I referenced earlier in the thread.
Ed.