Number of word occurences in a file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Number of word occurences in a file?
# 8  
Old 03-03-2012
First understand what the post was before saying rubbish.. I clearly said that "I cannot paste in the closed threads" and not "I cannot copy from the thread"!

Let me explain clearly:
"Many of the threads on this subject are filled with complex scripting commands and so I am trying to know if anyone can suggest a simpler one. If none could, I would have posted my findings."
# 9  
Old 03-03-2012
The "count words" scripts get progressively more complex according to the exact requirement.
Many forget to deal with punctuation and upper-case.

In this example list I believe that there is only one different word, therefore we would count 5.
Code:
goodbye
Goodbye
"Goodbye"
goodbye.
;goodbye


You can definitely post on this current thread. Please post your specification, sample input an output, and your solution.
Please also mention what Operating System and version you are working on today and what Shell you are using.
# 10  
Old 03-03-2012
Supposing, I have text file f1 which has, say, the long listing information of a directory.

Code:
 
# cd /testdir ; ls -l > ft ; more ft
total 18
drwxr-xr-x 4 root root 512 Feb 16 16:03 dir1
drwxr-xr-x 3 root root 512 Feb 16 14:54 dir2
drwxr-xr-x 4 root root 512 Feb 16 16:34 dir3
-rw-r--r-- 1 root root 824 Mar 3 07:22 f1
-rw-r--r-- 1 root root 54 Feb 12 18:58 f2
-rw-r--r-- 1 root root 772 Mar 3 10:03 f3
-rw-r--r-- 1 root root 770 Feb 26 10:30 f4
-rw-r--r-- 1 root root 772 Mar 3 10:07 f5
-rw-r--r-- 1 root root 0 Mar 3 10:13 ft
-rw-r--r-- 1 root root 9 Feb 24 15:06 root

=> I need to count to count how many times the word 'root' has repeated in this file.
I'm using a Solaris box and I'm in a /sbin/sh.
(1.You are right! The strings "root" and ";root" aren't the same. But, all I could only come up with a search which shows combined result.
2. Interestingly, when I dynamically created a file by
Code:
# ls -l > ft

, the file ft is also included itself in the listing!)

Last edited by satish51392111; 03-03-2012 at 12:30 PM.. Reason: Codetags!
# 11  
Old 03-03-2012
This should work even with the original version of awk on Solaris. If not try nawk instead of awk

Code:
ls -al| awk ' { gsub( "\n", "" ); c[$1]++; } END{ print c["root"]; }' RS=" "

This will 'double count' where root is both group and owner, and tripple count in the instance where the file is also named root. If you only want single counts (if root appears multiple times on a single line only count it once), then something as simple as
Code:
ls -al|grep -c "root"

should work.
# 12  
Old 03-03-2012
One possible method (taken from a more complex case and undersimplified):
Code:
cat ft|tr " " "\n"|sort|grep -v \^\$| grep "root"|uniq -c

21 root


Tested with old Bourne Shell but not with Solaris.

Hope you realise that /sbin/sh on Solaris is the old Bourne Shell.

Last edited by methyl; 03-03-2012 at 12:46 PM..
# 13  
Old 03-03-2012
Quote:
2. Interestingly, when I dynamically created a file by

Code:
# ls -l > ft
, the file ft is also included itself in the listing!)
... but with size zero bytes.
# 14  
Old 03-03-2012
Quote:
Originally Posted by satish51392111
2. Interestingly, when I dynamically created a file by
Code:
# ls -l > ft

, the file ft is also included itself in the listing!)

This is expected. When you run the command, the shell creates the file for redirection (ft) before it starts the ls command, and as a result ft is added to the directory before ls opens it, and thus it is included in the list.

Last edited by agama; 03-03-2012 at 03:43 PM.. Reason: typo
 
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

Number of Consecutive Occurences

:b: Hi Folks, I have a requirement to find the number of consecutive occurences based on month. Here is the below sample of my requirement. I have say four columns CARUSERNAME BRAND_CODE MONTH YEAR Below is sample Data John|C|JAN|2013 Tim|C|FEB|2013 Tom|C|Mar|2013 Ron|C|Apr|2013... (4 Replies)
Discussion started by: dinesh1985
4 Replies

3. Shell Programming and Scripting

Sort the file based on number of occurences

I have a file (input) I want to sort the file based on the number of times a pattern in the first column occurs for example grapes occurs 4 times in combination with other patterns so i want it to be first like shown in the output file. then apple ocuurs thrice so it occupies second position and so... (7 Replies)
Discussion started by: anurupa777
7 Replies

4. 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

5. Shell Programming and Scripting

counting the number of occurences

say i've got a text file with >10million sequences: ssss ssss tttttt uuuuuu uuuuuu uuuuuu ... I'd like to convert the file so that the output will report the number of occurence right by each sequence: 2 ssss 2 ssss 1 tttttt 3 uuuuuu 3 uuuuuu 3 uuuuuu .... (3 Replies)
Discussion started by: johjoh
3 Replies

6. 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

7. Shell Programming and Scripting

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 (6 Replies)
Discussion started by: Satyak
6 Replies

8. 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

9. UNIX for Dummies Questions & Answers

Counting number of occurences

Hi All, I have to count the number of occurences of the character " ; " in a given line. I had used the following awk command to achieve the same echo $KOP.dat|awk '{split($1,my,";"); for(i in my)c++ }END{print c-1}' My file KOP.dat had the following data ... (1 Reply)
Discussion started by: kingofprussia
1 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