Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Searching strings with awk
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
contrace...@gmail.com  
View profile  
 More options Dec 8 2008, 2:37 pm
Newsgroups: comp.lang.awk
From: contrace...@gmail.com
Date: Mon, 8 Dec 2008 11:37:46 -0800 (PST)
Local: Mon, Dec 8 2008 2:37 pm
Subject: Searching strings with awk
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Schindler  
View profile  
 More options Dec 10 2008, 10:58 am
Newsgroups: comp.lang.awk
From: Michael Schindler <Michael.Schind...@materna.de>
Date: Wed, 10 Dec 2008 16:58:05 +0100
Local: Wed, Dec 10 2008 10:58 am
Subject: Re: Searching strings with awk

#!/usr/bin/ksh

pattern=$1

shift 1

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

exit


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ed Morton  
View profile  
 More options Dec 10 2008, 1:03 pm
Newsgroups: comp.lang.awk
From: Ed Morton <mortons...@gmail.com>
Date: Wed, 10 Dec 2008 10:03:53 -0800 (PST)
Local: Wed, Dec 10 2008 1:03 pm
Subject: Re: Searching strings with awk
On Dec 10, 9:58 am, Michael Schindler <Michael.Schind...@materna.de>
wrote:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenny McCormack  
View profile  
 More options Dec 10 2008, 1:31 pm
Newsgroups: comp.lang.awk
From: gaze...@shell.xmission.com (Kenny McCormack)
Date: Wed, 10 Dec 2008 18:31:56 +0000 (UTC)
Local: Wed, Dec 10 2008 1:31 pm
Subject: Re: Searching strings with awk
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.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris F.A. Johnson  
View profile  
 More options Dec 10 2008, 1:47 pm
Newsgroups: comp.lang.awk
From: "Chris F.A. Johnson" <cfajohn...@gmail.com>
Date: Wed, 10 Dec 2008 18:47:53 +0000
Local: Wed, Dec 10 2008 1:47 pm
Subject: Re: Searching strings with awk
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"'/ ' "$@"

> 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ed Morton  
View profile  
 More options Dec 10 2008, 2:01 pm
Newsgroups: comp.lang.awk
From: Ed Morton <mortons...@gmail.com>
Date: Wed, 10 Dec 2008 11:01:27 -0800 (PST)
Local: Wed, Dec 10 2008 2:01 pm
Subject: Re: Searching strings with awk
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ed Morton  
View profile  
 More options Dec 10 2008, 3:57 pm
Newsgroups: comp.lang.awk
From: Ed Morton <mortons...@gmail.com>
Date: Wed, 10 Dec 2008 12:57:52 -0800 (PST)
Local: Wed, Dec 10 2008 3:57 pm
Subject: Re: Searching strings with awk
On Dec 10, 12:47 pm, "Chris F.A. Johnson" <cfajohn...@gmail.com>
wrote:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hai Vu  
View profile  
 More options Dec 11 2008, 4:14 pm
Newsgroups: comp.lang.awk
From: Hai Vu <haivu2...@gmail.com>
Date: Thu, 11 Dec 2008 13:14:27 -0800 (PST)
Local: Thurs, Dec 11 2008 4:14 pm
Subject: Re: Searching strings with awk
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

On Dec 8, 11:37 am, contrace...@gmail.com wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Fishman  
View profile  
 More options Dec 13 2008, 1:21 pm
Newsgroups: comp.lang.awk
From: Barry Fishman <barry_fish...@acm.org>
Date: Sat, 13 Dec 2008 13:21:49 -0500
Local: Sat, Dec 13 2008 1:21 pm
Subject: Re: Searching strings with awk

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ed Morton  
View profile  
 More options Dec 14 2008, 10:02 am
Newsgroups: comp.lang.awk
From: Ed Morton <mortons...@gmail.com>
Date: Sun, 14 Dec 2008 07:02:06 -0800 (PST)
Local: Sun, Dec 14 2008 10:02 am
Subject: Re: Searching strings with awk
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »