no of occurences of q word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting no of occurences of q word
# 1  
Old 09-29-2008
Java no of occurences of q word

hi I hace a string
"abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc"
i replaced commas with spaces, now i want to calculate nof occurences of "abc" word.


thanks in advance
Satya
# 2  
Old 09-29-2008
If you will replace comma(,) with newline then you can use awk to get number of occurrence of 'abc' like this :-

cat testfile | tr "," "\n" | awk /abc/ | wc -l

sed 's/,/\n/g' testfile | awk /abc/ |wc -l
# 3  
Old 09-29-2008
try this........
awk '{x=0;for(i=1;i<=NF;i++)if($i == "abc" )x++; print x}' abc

in case, ur file is a comma delimited one, then.....
awk -F, '{x=0;for(i=1;i<=NF;i++)if($i == "abc" )x++; print x}' abc
# 4  
Old 09-29-2008
Quote:
Originally Posted by Satyak
hi I hace a string
"abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc"
i replaced commas with spaces, now i want to calculate nof occurences of "abc" word.


thanks in advance
Satya
if you have Python
Code:
#!/usr/bin/env python
s="abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc"
print s.count("abc")

or at the shell
Code:
# s="abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc"
# echo $s | python -c "print raw_input().count('abc')"
5

# 5  
Old 09-29-2008
simply try this
Code:
awk 'BEGIN{RS=","}{if($0=="abc"){x += 1}}END{print x}' filename

# 6  
Old 09-29-2008
Java no of occurences of a word

hi vijay,
what u gave is good, but where i need to pass the string or file name
# 7  
Old 09-29-2008
awk '{x=0;for(i=1;i<=NF;i++)if($i == "abc" )x++; print x}' <filename>

Actually, I 'd named the i/p file as "abc"..so the confusion..
sorry for that
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count occurences of the word without it repeating

Hi, I would like to count the number of ALA occurences without having them to be repeated. In the script I have written now it has 40 repetitions of ALA but it has to be 8. ALA is chosen as one of the 20 values it can have when the script asks for the input of AAA, which for this example is chosen... (7 Replies)
Discussion started by: Aurimas
7 Replies

2. Shell Programming and Scripting

Order as per top occurences

Hi I have a file with entries like below top 5 a 5 b 4 c 3 d 2 e 1 top 5 b 5 d 4 c 3 e 2 a 1 top 5 e 5 d 4 c 3 b 2 a 1 (2 Replies)
Discussion started by: Viswanatheee55
2 Replies

3. Shell Programming and Scripting

awk count occurences

line number:status, market, keystation 1,SENT,EBS,1 : 1 2,DONE,REU,1 : 1 3,SENT,EBS,2 : 1 4,DONE,EBS,1 : 0 5,SENT,EBS,2 : 0 6,SENT,EBS,2 : 0 7,SENT,EBS,2 : 0 8,SENT,EBS,1 : 1 for each status, market combination I want to keep a tally of active orders. i.e if an order is SENT, then +1, if... (8 Replies)
Discussion started by: Calypso
8 Replies

4. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

5. UNIX for Dummies Questions & Answers

Number of word occurences in a file?

Hello world, Can anybody tell me how to count how many times does a word repeat in a file? There have been many threads on this but they all are heavy loads of Scripting for a starter like me. :D So, I sat down today and after some hours of reading man pages, I found a simple one-line... (18 Replies)
Discussion started by: satish51392111
18 Replies

6. UNIX for Dummies Questions & Answers

Count number of occurences of a word

I want to count the number of occurences of say "200" in a file but that file also contains various stuff including dtaes like 2007 or smtg like 200.1 so count i am getting by doing grep -c "word" file is wrong Please help!!!!! (8 Replies)
Discussion started by: shikhakaul
8 Replies

7. UNIX for Dummies Questions & Answers

How to count the occurences of a specific word in a file in bash shell

Hello, I want to count the occurences of a specific word in a .txt file in bash shell. Can somebody help me pleaze?? Thanks!!! (2 Replies)
Discussion started by: mskart
2 Replies

8. Shell Programming and Scripting

occurences of words

I have a string like this. $str="The astrocyte profile might contribute to the identification of possible therapies in profiles profiling and profiled als."; Lets consider for example: a)If user enters the term profile* it should highlight profile,profiles only. b)If user enters the... (3 Replies)
Discussion started by: vanitham
3 Replies

9. Web Development

How to find all occurences of word?

Hi, For example lets consider i have word like this:cell I have some text that is stored in table. These are few sentences. TRAP also regulates translation of trpE by promoting formation of an cell. In addition initiation of pabA, trpP and ycbK by directly blocking cells. I... (0 Replies)
Discussion started by: vanitham
0 Replies

10. Shell Programming and Scripting

number of occurences of a string

hi, I have a file where i need to count the occurences of a string ex) 'welcome to unix forum'. can anybody help me out (12 Replies)
Discussion started by: siddu_chittari
12 Replies
Login or Register to Ask a Question