Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Searching strings with awk

42 views
Skip to first unread message

contr...@gmail.com

unread,
Dec 8, 2008, 2:37:46 PM12/8/08
to
Hi:
Is there any way to make a grep using awk
to find a specified string between "-------------" and
"--------------" lines ?

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

Michael Schindler

unread,
Dec 10, 2008, 10:58:05 AM12/10/08
to
contr...@gmail.com wrote:

#!/usr/bin/ksh

pattern=$1

shift 1

gawk ' BEGIN { RS="---*" }
/'$pattern'/ ' $*

exit

Ed Morton

unread,
Dec 10, 2008, 1:03:53 PM12/10/08
to
On Dec 10, 9:58 am, Michael Schindler <Michael.Schind...@materna.de>
wrote:
> exit- Hide quoted text -
>
> - Show quoted text -

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.

Kenny McCormack

unread,
Dec 10, 2008, 1:31:56 PM12/10/08
to
In article <a41c5c09-c8f8-4958...@m22g2000vbl.googlegroups.com>,
Ed Morton <morto...@gmail.com> wrote:
...

>No, that's not how you pass the value of shell variables to an awk
>script. Blah, blah, blah.

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).

Chris F.A. Johnson

unread,
Dec 10, 2008, 1:47:53 PM12/10/08
to
On 2008-12-10, Kenny McCormack wrote:
> In article <a41c5c09-c8f8-4958...@m22g2000vbl.googlegroups.com>,
> Ed Morton <morto...@gmail.com> wrote:
> ...
>>No, that's not how you pass the value of shell variables to an awk
>>script. Blah, blah, blah.
>
> No, that *is* how he passes shell variables to an AWK script.
> It also happens to be how I (usually) do it.

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

Ed Morton

unread,
Dec 10, 2008, 2:01:27 PM12/10/08
to
On Dec 10, 12:31 pm, gaze...@shell.xmission.com (Kenny McCormack)
wrote:
> In article <a41c5c09-c8f8-4958-a502-fa99f9051...@m22g2000vbl.googlegroups.com>,

> Ed Morton  <mortons...@gmail.com> wrote:
> ...
>
> >No, that's not how you pass the value of shell variables to an awk
> >script. Blah, blah, blah.
>
> No, that *is* how he passes shell variables to an AWK script.
> It also happens to be how I (usually) do it.

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.

Ed Morton

unread,
Dec 10, 2008, 3:57:52 PM12/10/08
to
On Dec 10, 12:47 pm, "Chris F.A. Johnson" <cfajohn...@gmail.com>
wrote:

> On 2008-12-10, Kenny McCormack wrote:
>
> > In article <a41c5c09-c8f8-4958-a502-fa99f9051...@m22g2000vbl.googlegroups.com>,

> > Ed Morton  <mortons...@gmail.com> wrote:
> > ...
> >>No, that's not how you pass the value of shell variables to an awk
> >>script. Blah, blah, blah.
>
> > No, that *is* how he passes shell variables to an AWK script.
> > It also happens to be how I (usually) do it.
>
>    And your script would fail if $1 contains spaces. I would hope that
>    you actually do this:
>
> gawk ' BEGIN { RS="---*" }
>        /'"$pattern"'/ ' "$@"

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.

Hai Vu

unread,
Dec 11, 2008, 4:14:27 PM12/11/08
to
1. Save the lines below to a file call search_script
2. chmod +x search_script
3. Assume that your data is in a file call "data1"
4. Assume that you want to ignore any data prior to the first dash
line. If you want to include the data prior to the first dash line in
the search, delete the line that set count = -1
5. Issue this command:
./search_script yyy data1
Please let me know if it works for you.

#!/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

Barry Fishman

unread,
Dec 13, 2008, 1:21:49 PM12/13/08
to

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

Ed Morton

unread,
Dec 14, 2008, 10:02:06 AM12/14/08
to
On Dec 13, 12:21 pm, Barry Fishman <barry_fish...@acm.org> wrote:

Yes, see the comp.unix.shell FAQ I referenced earlier in the thread.

Ed.

0 new messages