Exact String match - awk

This is a discussion on Exact String match - awk ; To locate all instances of "as" in a text file, I tried the following lines: /as/ {print NR, $0} --- "messages", "has" etc are also getting listed /^as$/ {print NR, $0} -- nothing is listed. Am I doing something wrong. ...

+ Reply to Thread
Results 1 to 6 of 6

Exact String match

  1. Default Exact String match

    To locate all instances of "as" in a text file, I tried the following
    lines:

    /as/ {print NR, $0} --- "messages", "has" etc are also getting
    listed

    /^as$/ {print NR, $0} -- nothing is listed.

    Am I doing something wrong. I am using gawk.

    Thanks in advance for the help.


  2. Default Re: Exact String match

    On Oct 12, 10:33 am, Raj <rsp...@gmail.com> wrote:
    > To locate all instances of "as" in a text file, I tried the following
    > lines:
    >
    > /as/ {print NR, $0} --- "messages", "has" etc are also getting
    > listed
    >
    > /^as$/ {print NR, $0} -- nothing is listed.
    >
    > Am I doing something wrong. I am using gawk.
    >
    > Thanks in advance for the help.


    I am sorry about the examples above. "messages" is incorrect.
    "ascending" , "casual" , "has" etc. are getting listed


  3. Default Re: Exact String match

    On 12 Okt., 09:02, Raj <rsp...@gmail.com> wrote:
    > On Oct 12, 10:33 am, Raj <rsp...@gmail.com> wrote:
    >
    > > To locate all instances of "as" in a text file, I tried the following
    > > lines:

    >
    > > /as/ {print NR, $0} --- "messages", "has" etc are also getting
    > > listed


    Use the word delimiters \< ... \> to get what you need...

    awk '/\<as\>/{print NR, $0}'

    > > /^as$/ {print NR, $0} -- nothing is listed.


    Here you match against complete records; only where "as" appears alone
    in a record that will be printed. You can of course redefine the
    record definition by writing

    awk 'BEGIN{RS=FS}/^as$/{print NR, $0}'

    The result will not show a line number but the word number in this
    case.

    Janis

    >
    > > Am I doing something wrong. I am using gawk.

    >
    > > Thanks in advance for the help.

    >
    > I am sorry about the examples above. "messages" is incorrect.
    > "ascending" , "casual" , "has" etc. are getting listed




  4. Default Re: Exact String match

    On Oct 12, 12:51 pm, Janis <janis_papanag...@hotmail.com> wrote:
    > On 12 Okt., 09:02, Raj <rsp...@gmail.com> wrote:
    >
    > > On Oct 12, 10:33 am, Raj <rsp...@gmail.com> wrote:

    >
    > > > To locate all instances of "as" in a text file, I tried the following
    > > > lines:

    >
    > > > /as/ {print NR, $0} --- "messages", "has" etc are also getting
    > > > listed

    >
    > Use the word delimiters \< ... \> to get what you need...
    >
    > awk '/\<as\>/{print NR, $0}'
    >
    > > > /^as$/ {print NR, $0} -- nothing is listed.

    >
    > Here you match against complete records; only where "as" appears alone
    > in a record that will be printed. You can of course redefine the
    > record definition by writing
    >
    > awk 'BEGIN{RS=FS}/^as$/{print NR, $0}'
    >
    > The result will not show a line number but the word number in this
    > case.
    >
    > Janis
    >
    >
    >
    >
    >
    > > > Am I doing something wrong. I am using gawk.

    >
    > > > Thanks in advance for the help.

    >
    > > I am sorry about the examples above. "messages" is incorrect.
    > > "ascending" , "casual" , "has" etc. are getting listed- Hide quoted text -

    >
    > - Show quoted text -


    Thanks. It worked.

    Raj


  5. Default Re: Exact String match

    Janis wrote:

    > On 12 Okt., 09:02, Raj <rsp...@gmail.com> wrote:
    >
    >>On Oct 12, 10:33 am, Raj <rsp...@gmail.com> wrote:
    >>
    >>
    >>>To locate all instances of "as" in a text file, I tried the following
    >>>lines:

    >>
    >>>/as/ {print NR, $0} --- "messages", "has" etc are also getting
    >>>listed

    >
    >
    > Use the word delimiters \< ... \> to get what you need...
    >
    > awk '/\<as\>/{print NR, $0}'


    I know the OP said he's using gawk, but it's still probably worth
    mentioning that that's gawk specific.

    >
    >>>/^as$/ {print NR, $0} -- nothing is listed.

    >
    >
    > Here you match against complete records; only where "as" appears alone
    > in a record that will be printed. You can of course redefine the
    > record definition by writing
    >
    > awk 'BEGIN{RS=FS}/^as$/{print NR, $0}'


    That won't cause RS to behave like FS (i.e. treat every sequence of
    blank characters as a single separator) which I think is what you're
    trying to do. Instead it'll set RS to a single space character " ". To
    get RS to behave like FS would if it were set to the default " ", you'd
    need to do something like (untested and not thought about much):

    awk 'BEGIN{RS="[[:space:]]+"}....

    > The result will not show a line number but the word number in this
    > case.
    >
    > Janis
    >
    >
    >>>Am I doing something wrong. I am using gawk.

    >>
    >>>Thanks in advance for the help.

    >>
    >>I am sorry about the examples above. "messages" is incorrect.
    >>"ascending" , "casual" , "has" etc. are getting listed


    What exactly do you NEED printed out - the line number and line or
    something else?

    Ed.

  6. Default Re: Exact String match

    On 12 Okt., 14:40, Ed Morton <mor...@lsupcaemnt.com> wrote:
    > Janis wrote:
    > > awk 'BEGIN{RS=FS}/^as$/{print NR, $0}'

    >
    > That won't cause RS to behave like FS (i.e. treat every sequence of
    > blank characters as a single separator) which I think is what you're


    Yes, you're right. I don't know what I was thinking at the moment I
    wrote that.

    Janis


+ Reply to Thread

Similar Threads

  1. Find exact Match
    By Application Development in forum ADO DAO RDO RDS
    Replies: 2
    Last Post: 11-21-2007, 07:54 PM
  2. Exact Match and Identity conversion
    By Application Development in forum c++
    Replies: 17
    Last Post: 08-14-2007, 06:49 PM
  3. some problems about exact match in the overload resolution.
    By Application Development in forum c++
    Replies: 2
    Last Post: 07-29-2007, 02:22 PM
  4. Exact match with start of field
    By Application Development in forum awk
    Replies: 4
    Last Post: 08-09-2006, 08:04 AM
  5. Do a search for an Exact Match
    By Application Development in forum Inetserver
    Replies: 3
    Last Post: 04-07-2004, 12:35 AM