counting the occurence of a word


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers counting the occurence of a word
# 1  
Old 06-30-2009
counting the occurence of a word

In a file I have to count a particular word. like i need apache how many times.

I tried this

$ tr "\011" "\012\012"<foo1 | tr -cd "[a-zA-z\012]" |sort\uniq -c


but I got result like this

32 apache
18 dns
12 doctor


Please sugest me
# 2  
Old 06-30-2009
If all you want is the count for apache then you could do:
Code:
$ tr "\011" "\012\012"<foo1 | tr -cd "[a-zA-z\012]" | grep "^apache$" | wc -l

Update I had assumed the tr bits were working but they aren't...

Here is something that works:
Code:
$ tr "\011\040" "\012\012"<foo1 | grep -ic "^apache$"


Last edited by TonyFullerMalv; 06-30-2009 at 05:04 PM.. Reason: Workinh version!
# 3  
Old 06-30-2009
Quote:
Originally Posted by TonyFullerMalv
If all you want is the count for apache then you could do:
Code:
$ tr "\011" "\012\012"<foo1 | tr -cd "[a-zA-z\012]" | grep "^apache$" | wc -l



Thanks !!! any simpler way ?
# 4  
Old 06-30-2009
Is 'apache' the same as 'apaches' and the same as apache's or 'apache,'?
# 5  
Old 07-06-2009
Quote:
Originally Posted by vgersh99
Is 'apache' the same as 'apaches' and the same as apache's or 'apache,'?
Not if you do what I did:
Code:
grep "^apache$"

Which is looking for apache with a start of line at the beginning and an end of line at the end!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete last occurence of word using Linux command?

Hi all, I am trying to delete last occurrence of word using sed command. for example. I have input like this on a.id1 = b.id1 and on a.id2 = b.id2 and on a.id3 = b.id3 and and I am expecting output like this on a.id1 = b.id1 and on a.id2 = b.id2 and on a.id3 = b.id3 I just need to... (11 Replies)
Discussion started by: nsk
11 Replies

2. Shell Programming and Scripting

Perl : Search for next occurence of a word in an array

I have an array as follows: Space: ABC Name: def Age: 22 Type: new Name: fgh Age: 34 Type: old Space: XYZ Name: pqr Age: 44 Type: new : : How can I separate the array with elements starting from Space:ABC until Space: XYZ & put them in a different array & so on... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

3. Shell Programming and Scripting

finding the number of occurence of a word in a line

suppose i have this line abs|der|gt|dftnrk|dtre i want to count the number of "|" in this line.. how can i do that. plz help:confused: (9 Replies)
Discussion started by: priyanka3006
9 Replies

4. UNIX for Dummies Questions & Answers

Counting occurence of a particular character on each line

Hi, I have the following data in a flat file: abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 I need to check the no. of occurence of "|" (pipe) on each line and the output should look like below:... (4 Replies)
Discussion started by: hey_mak
4 Replies

5. UNIX for Dummies Questions & Answers

counting the occurence of particular characters

I want to list the occurence of particular characters in a line. my file looks like this a,b,c,d e,f,g h,y:e,g,y s f;g,s,w and I want to count how many commas are in each line so the file in the end looks like this: a,b,c,d 3 e,f,g 2 h,y:e,g,y s 3 f;g,s,w ... (2 Replies)
Discussion started by: Audra
2 Replies

6. UNIX for Dummies Questions & Answers

search& count for the occurence of a word

Greetings, I need to search and count all the occurences of a word in all the files in a directory. Any suggestions greatly appreciated. Thanks (1 Reply)
Discussion started by: skoppana
1 Replies

7. Shell Programming and Scripting

Delete all occurence of a word in one shot

i have a file called file1 cat file1 i am namish namish lives in India India and namish both are good. I want to delete all the occurences of namish in one shot,if i do it with sed i guess all the lines will be deleted containing the pattern.Suggest me any idea without AWK. Thanks... (6 Replies)
Discussion started by: namishtiwari
6 Replies

8. Shell Programming and Scripting

Count the number of occurence of perticular word from file

I want to count the number of occurence of perticular word from one text file. Please tell me "less" command is work in ksh or not. If it is not working then instead of that which command will work. :confused: (40 Replies)
Discussion started by: rinku
40 Replies

9. Shell Programming and Scripting

Counting occurence of a coma

I have a .csv file that I would like to count on each line the occurences of the coma. Each line should have the same amount, but as an integrity check I would like to include in my script this count. What is the best way of doing this? (3 Replies)
Discussion started by: dbrundrett
3 Replies
Login or Register to Ask a Question