why awk don't display the results? 
Author Message
 why awk don't display the results?

Hi,

I am new to awk programming so I did some tests to learn more about it.

BEGIN { count = 0 }
/HELLO/ { for (i=1; i <= NF; i++)
          { if ($i == "HELLO") count++ }
        }
END { print ("HELLO occurs", count, "times.") }

I ran it like that: awk -f my.awk data.don

I thought it was suppose to display all the lines containing HELLO, and
the sentence HELLO occurs... but only the sentence was displayed.
Anybody knows why?

Thank you!
--
Josianne Marsan
Logistec Stevedoring Inc.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 19 Jul 2002 03:00:00 GMT  
 why awk don't display the results?
Try...

/HELLO/ {count++;print}
END {print "HELLO occurs", count, "times."}

Quote:
>I thought it was suppose to display all the lines containing HELLO, and
>the sentence HELLO occurs... but only the sentence was displayed.
>Anybody knows why

You need to "print" in the "HELLO" pattern match.

Regards,
   =Adrian=



Fri, 19 Jul 2002 03:00:00 GMT  
 why awk don't display the results?
OOPS!! I didn't read the question properly.

Try...

/HELLO/ {print;count+=gsub(/HELLO/,"")}
END {print "HELLO occurs", count, "times."

Regards,
   =Adrian=



Fri, 19 Jul 2002 03:00:00 GMT  
 why awk don't display the results?

Quote:

>OOPS!! I didn't read the question properly.
>Try...
>/HELLO/ {print;count+=gsub(/HELLO/,"")}
>END {print "HELLO occurs", count, "times."

..

This just happens to work in this case because there aren't
any English words containing the substring 'HELLO' except
for the word HELLO. If the word were 'ROT', this technique
would fail on words like 'ROTTEN' and 'CARROT'. Also, this
differs from the original posting in which only the count
of fields containing HELLO were counted. While 'HELLO-
HELLO' may be pathological, should it be double counted?

Keep it closer to the original posting. First block only.

/HELLO/ {
  initcount = count
  for (i = 1; i <= NF; ++i) count += ($i == "HELLO")
  if (count > initcount) print

Quote:
}

* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful


Fri, 19 Jul 2002 03:00:00 GMT  
 why awk don't display the results?


Quote:


>>OOPS!! I didn't read the question properly.
>>Try...
>>/HELLO/ {print;count+=gsub(/HELLO/,"")}
>>END {print "HELLO occurs", count, "times."
>..

>This just happens to work in this case because there aren't
>any English words containing the substring 'HELLO' except
>for the word HELLO. If the word were 'ROT', this technique
>would fail on words like 'ROTTEN' and 'CARROT'. Also, this
>differs from the original posting in which only the count
>of fields containing HELLO were counted. While 'HELLO-
>HELLO' may be pathological, should it be double counted?

>Keep it closer to the original posting. First block only.

>/HELLO/ {
>  initcount = count
>  for (i = 1; i <= NF; ++i) count += ($i == "HELLO")
>  if (count > initcount) print
>}

But this fails to perform properly for lines like these:

HELLO, CAN I HELP YOU?
She shouted, "HELLO Paul!"

note that the punctuation is included in the field, so $i may
include "HELLO" but not be equal to it.  

Parsing text for words is not just a question of fields.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Fri, 19 Jul 2002 03:00:00 GMT  
 why awk don't display the results?

Quote:

>In article


..
>>/HELLO/ {
>>  initcount = count
>>  for (i = 1; i <= NF; ++i) count += ($i == "HELLO")
>>  if (count > initcount) print
>>}

>But this fails to perform properly for lines like these:

>HELLO, CAN I HELP YOU?
>She shouted, "HELLO Paul!"

>note that the punctuation is included in the field, so $i
>may include "HELLO" but not be equal to it. Parsing text
>for words is not just a question of fields.

Good point. I had debated whether or not to use

/(^|[^A-Za-z])HELLO([^A-Za-z]|$)/

Perhaps this or some more elaborate variation is necessary.
Only the original poster knows for sure.

* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful



Fri, 19 Jul 2002 03:00:00 GMT  
 why awk don't display the results?


Quote:


> >In article


> ..
> >>/HELLO/ {
> >>  initcount = count
> >>  for (i = 1; i <= NF; ++i) count += ($i == "HELLO")
> >>  if (count > initcount) print
> >>}

> >But this fails to perform properly for lines like these:

> >HELLO, CAN I HELP YOU?
> >She shouted, "HELLO Paul!"

> >note that the punctuation is included in the field, so $i
> >may include "HELLO" but not be equal to it. Parsing text
> >for words is not just a question of fields.

> Good point. I had debated whether or not to use

> /(^|[^A-Za-z])HELLO([^A-Za-z]|$)/

> Perhaps this or some more elaborate variation is necessary.
> Only the original poster knows for sure.

I am the original poster and I am new to AWK. Does this:

/(^|[^A-Za-z])HELLO([^A-Za-z]|$)/

mean a record that contains the word HELLO, or any character not in A-
Za-z and HELLO, or any character not in A-Za-z and HELLO and any
character not in A-Za-z?

Thank you!

--
Josianne Marsan
Logistec Stevedoring Inc.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 21 Jul 2002 03:00:00 GMT  
 why awk don't display the results?


Quote:




>> >In article


>> ..
>> >>/HELLO/ {
>> >>  initcount = count
>> >>  for (i = 1; i <= NF; ++i) count += ($i == "HELLO")
>> >>  if (count > initcount) print
>> >>}

>> >But this fails to perform properly for lines like these:

>> >HELLO, CAN I HELP YOU?
>> >She shouted, "HELLO Paul!"

>> >note that the punctuation is included in the field, so $i
>> >may include "HELLO" but not be equal to it. Parsing text
>> >for words is not just a question of fields.

>> Good point. I had debated whether or not to use

>> /(^|[^A-Za-z])HELLO([^A-Za-z]|$)/

>> Perhaps this or some more elaborate variation is necessary.
>> Only the original poster knows for sure.

>I am the original poster and I am new to AWK. Does this:

>/(^|[^A-Za-z])HELLO([^A-Za-z]|$)/

>mean a record that contains the word HELLO, or any character not in A-
>Za-z and HELLO, or any character not in A-Za-z and HELLO and any
>character not in A-Za-z?

It means the string HELLO preceded by the beginning of the record (line
or in some cases field) or some character that isn't a letter, and
followed by the end of the record (line or in some cases field) or some
character that isn't a letter.

I say field because this is a possible form:

$2 ~ /(^|[^A-Za-z])HELLO([^A-Za-z]|$)/

It's an AWK regular expression, and awk has many places where they are
used.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 21 Jul 2002 03:00:00 GMT  
 why awk don't display the results?
To count HELLO's without being dependend on the seperation
in fields you could use

/HELLO/  { count += gsub("HELLO","HELLO")
           print
           line++
         }

END { print "HELLO occurs", count, "times in", line, "lines" }

Erik.

Quote:







> >> >In article


> >> ..
> >> >>/HELLO/ {
> >> >>  initcount = count
> >> >>  for (i = 1; i <= NF; ++i) count += ($i == "HELLO")
> >> >>  if (count > initcount) print
> >> >>}

> >> >But this fails to perform properly for lines like these:

> >> >HELLO, CAN I HELP YOU?
> >> >She shouted, "HELLO Paul!"



Tue, 23 Jul 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Help with parsing a file and don't obtain the expected results

2. I want to make a list of the results...but don't know how

3. I want to make a list of the results...but don't know how

4. awk's mod operator % question (application: log base2 non-integer result)

5. r,i,m keys don't display in labview

6. Humble Beginner's Question: How do I mul 2 dwords and display results

7. Why patterns don't suck

8. Why I don't Like HTML Help...

9. Animated Gifs Some work some don't why

10. Why don't Memo Entry Fields wrap?????

11. I don't understand why my haskell program works

 

 
Powered by phpBB® Forum Software