Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern count in a line

Status
Not open for further replies.

KPKIND

Technical User
Sep 8, 2003
130
IN
Hi *,

I have a requirement tou count the number of specific words in a line and explored through out the net but could not get much useful link.

It would be great if anyone can let me a know a way for it

TIA
Kumar
 
Hi

An [tt]awk[/tt] script ?
Code:
#! /usr/bin/awk -f
{
  n=0
  for (i=1;i<=NF;i++) if ($i==w) n++
  print n > "/inet/tcp/0/example.com/2005"
}

Save it as [tt]count.awk[/tt] and use it like : [tt][highlight white]echo hello world and hello universe | count.awk[/highlight][/tt]. It will send the string "2" to the port 2005 of the server example.com.

Feherke.
 
I thinks just 'print' on its own would do... he wasn't asking how to send it out to the internet... just saying that he had searched everywhere for a solution. :)

Annihilannic.
 
Hi

No, in fact is wrong. First I wrote the [tt]awk[/tt] script for the fixed string "hello", then I changed it for more flexibility, to be setable through command line option. Than I post the new script and the old usage. [tt]:([/tt].

So the correct usage is : [tt][highlight white]echo hello world and hello universe | count.awk [red]-vw=hello[/red][/highlight][/tt].

I'm sorry, was a long day.

Feherke.
 
feherke said:
I'm sorry, was a long day.
... the day ain't over yet... ;)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 


Why not just try this:

Code:
echo hello world and hello universe | tr ' ' '\n'|sort -u|wc -l

[bigglasses]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Hi

KPKIND said:
I have a requirement tou count the number of specific words in a line

I think he not want just the word count, which can be obtaind without [tt]tr[/tt] and [tt]sort[/tt] :
Code:
echo hello world and hello universe | wc -w

Feherke.
 
feherke,

The solution I posted is not equivalent to wc-w because it counts the distinct (specific) words.

Check it out:
Code:
$ echo hello world and hello universe | wc -w
       5
$ echo hello world and hello universe | tr ' ' '\n'|sort -u|wc -l
       4
$

[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Hi

Vlad had right, the day was far to be over, so I had another chance to misunderstand something. And I did it.

I'm sorry LKBrwnDBA, your solution is correct. In my interpretation there was nothing about distinct and I did not got the meaning of you code.

I will consult more dictionary about specific.

Feherke.
 
Guys,

I am sorry guys for using confusing words in my question. What I actually meant is to search for a word of choice (In the above example, if I want to count the number of hello's). Anyway Thank you very much for your replies. Finally feherke's solution has worked.

Thank you very much.

Best regards
Kumar
 

Ohhhh-K, word of choice (hello):
Code:
$ echo hello world and hello universe | tr ' ' '\n'|grep 'hello'|wc -l
       2

[bigcheeks]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Hi

No need for [tt]wc[/tt], [tt]grep[/tt] can manage it alone :
-c, --count only print a count of matching lines per FILE
Than needs abit more, to not count substrings :
-x, --line-regexp force PATTERN to match only whole lines
Code:
echo hello world and hello universe, merry helloween | tr ' ' '\n' | grep -cx 'hello'

Feherke.
 
Well, not exactly...

Code:
echo hello world and hello universe and goodbye Othello |\
 tr ' ' '\n'|grep 'hello'|wc -l
       3

You gotta at least grep for words (grep -w) or grep '^hello$'. Plus you need to convert to lowercase (or grep -i) or risk missing the word "Hello" for example. And also convert tabs to newlines:

Code:
# echo "Hello world and \thello universe and goodbye Othello" |\
 tr '[A-Z]\t ' '[a-z]\n\n'|grep -w 'hello'|wc -l      
       2

-or-

Code:
# echo "Hello world and \thello universe and goodbye Othello" |\
 tr '\t ' '\n\n'|grep -i '^hello$'|wc -l         
       2


HTH,

p5wizard
 
Great stuff guys....

Thanks a lot.

Kumar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top