Help in counting the no of repeated words with count in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in counting the no of repeated words with count in a file
# 1  
Old 09-17-2010
Help in counting the no of repeated words with count in a file

Hi Pls help in solving my doubt.Iam having file like below

file1.txt
Code:
 
priya
jenny
jenny
priya
raj
radhika
priya
bharti
bharti

Output required:
I need a output like count of repeated words with name for ex:
Code:
priya 3
jenny 2
bharti 2

pls help me its urgent
# 2  
Old 09-17-2010
Code:
sort file1.txt | uniq -c

# 3  
Old 09-17-2010
Thanks a lot
# 4  
Old 09-17-2010
Code:
ruby -ne 'BEGIN{h={};h.default=0};h[$_.strip]+=1;END{h.each{|x| p x}}' file

# 5  
Old 09-17-2010
To get your required output is quite fiddly. Building on "chirel" post we then sort to reverse order of count, eliminate those with a count of value one, and then reverse the count and name fields.


Code:
sort file1.txt | uniq -c | sort -nr | while read count name
do
        if [ ${count} -gt 1 ]
        then
                echo "${name} ${count}"
        fi
done

priya 3
jenny 2
bharti 2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

2. Homework & Coursework Questions

Accepting a phrase and counting the number of times that it is repeated in a specific website

1. The problem statement, all variables and given/known data: Develop a shell script that accepts a phrase and counts the number of times that it is repeated in a specific website. Note: Im not sure if it's the whole website, or just a specific page but im guessing its thewhole website. ... (2 Replies)
Discussion started by: Zakerii
2 Replies

3. Shell Programming and Scripting

Find repeated word and take sum of the second field to it ,for all the repeated words in awk

Hi below is the input file, i need to find repeated words and sum up the values of it which is second field from the repeated work.Im trying but getting no where close to it.Kindly give me a hint on how to go about it Input fruits,apple,20,fruits,mango,20,veg,carrot,12,veg,raddish,30... (11 Replies)
Discussion started by: 100bees
11 Replies

4. Shell Programming and Scripting

Counting occurrence of all words in a file

Hi, Given below is the input file: http://i53.tinypic.com/2vmvzb8.png Given below is what the output file should look like: http://i53.tinypic.com/1e6lfq.png I know how to count the occurrence of 1 word from a file, but not all of them. Can someone help please? An explanation on the... (1 Reply)
Discussion started by: r4v3n
1 Replies

5. Shell Programming and Scripting

Counting number of files that contain words stored in another file

Hi All, I have written a script on this but it does not do the requisite job. My requirement is this: 1. I have two kinds of files each with different extensions. One set of files are *.dat (6000 unique DAT files all in one directory) and another set *.dic files (6000 unique DIC files in... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Programming

Counting the words in a file

Please find the below program. It contains the purpose of the program itself. /* Program : Write a program to count the number of words in a given text file */ /* Date : 12-June-2010 */ # include <stdio.h> # include <stdlib.h> # include <string.h> int main( int argc, char *argv ) {... (6 Replies)
Discussion started by: ramkrix
6 Replies

7. Shell Programming and Scripting

Averaging all fields while counting repeated records

Hi every one; I have a 31500-line text file upon which two following tasks are to be performed: 1: Rearranging the file 2: Taking the average of each column (considering number of zeros) and output the result into a new file This is the code I've come up with: awk '(NR%3150<3150)... (0 Replies)
Discussion started by: nxp
0 Replies

8. UNIX for Dummies Questions & Answers

counting words then amending to a file

i want to count the number of words in a file and then redirect this to a file echo 'total number of words=' wc -users>file THis isnt working, anyone any ideas. (1 Reply)
Discussion started by: iago
1 Replies

9. UNIX for Dummies Questions & Answers

count the number of files which have a search string, but counting the file only once

I need to count the number of files which have a search string, but counting the file only once if search string is found. eg: File1: Please note that there are 2 occurances of "aaa" aaa bbb ccc aaa File2: Please note that there are 3 occurances of "aaa" aaa bbb ccc... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies

10. Shell Programming and Scripting

Counting words in a file

I'm trying to figure out a way to count the number of words in the follwing file: cal 2002 > file1 Is there anyway to do this without using wc but instead using the cut command? (1 Reply)
Discussion started by: r0mulus
1 Replies
Login or Register to Ask a Question