how to count number of times each word exist in a file | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

how to count number of times each word exist in a file

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-06-2011
shnkool shnkool is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 6 December 2011, 7:08 PM EST
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Question how to count number of times each word exist in a file

I'm trying to count the number of times each word in the file exist
for example if the file has:
today I have a lot to write, but I will not go for it. The main thing is that today I am looking for a way to get each word in this file with a word count after it specifying that this word has occured that number of times in the whole file.

today-->2
I-->3
have-->1
a-->3
lot-->1
write-->1
but-->1
will-->1
not-->1


and so on


I used tr to separate each word in a line but I'm not sure how I'm going to look for each word
Sponsored Links
    #2  
Old 12-06-2011
hergp hergp is online now Forum Advisor  
Problem Eliminator
 
Join Date: Jan 2010
Last Activity: 27 November 2014, 7:40 AM EST
Location: Vienna, Austria
Posts: 806
Thanks: 18
Thanked 171 Times in 153 Posts
Try

Code:
 tr ' ' '\12' <inputfile| sort | uniq -c | sort -nr

The output looks like

Code:
      3 word
      3 that
      3 I
      3 a
      2 today
      2 to
      2 this
      2 in
      2 for
      1 write,
      1 with
      1 will
...

The Following User Says Thank You to hergp For This Useful Post:
shnkool (12-06-2011)
Sponsored Links
    #3  
Old 12-06-2011
tarun_agrawal tarun_agrawal is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 29 May 2012, 10:09 PM EDT
Posts: 87
Thanks: 0
Thanked 16 Times in 15 Posts
echo "1 2 3 3 2 1" | xargs -n 1 | awk '{a[$0]++} END{for(i in a) {print i , a[i]} }'
The Following User Says Thank You to tarun_agrawal For This Useful Post:
shnkool (12-06-2011)
    #4  
Old 12-06-2011
shnkool shnkool is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 6 December 2011, 7:08 PM EST
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
counting not correct yet

that helps a lot
but when I run it on a big file, it repeats the word
like if I have the words
bad, bad. bad: bad,
it will show
2 bad,
1 bad.
1 bad:
I only want to count the word itself so I have to delete punctuation to get it right I guess
by the way what is /12 stand for???

Quote:
Originally Posted by hergp View Post
Try

Code:
 tr ' ' '\12' <inputfile| sort | uniq -c | sort -nr

The output looks like

Code:
      3 word
      3 that
      3 I
      3 a
      2 today
      2 to
      2 this
      2 in
      2 for
      1 write,
      1 with
      1 will
...

---------- Post updated at 06:03 PM ---------- Previous update was at 05:59 PM ----------

---------- Post updated at 06:07 PM ---------- Previous update was at 06:03 PM ----------

I couldn't implement this on what I've
I don't understand the logic behind this code
which I don't know where to add the file that I'm suppose to count the words in it



Quote:
Originally Posted by tarun_agrawal View Post
echo "1 2 3 3 2 1" | xargs -n 1 | awk '{a[$0]++} END{for(i in a) {print i , a[i]} }'
Sponsored Links
    #5  
Old 12-07-2011
MR.bean MR.bean is offline
Registered User
 
Join Date: Sep 2010
Last Activity: 10 October 2014, 2:03 AM EDT
Posts: 82
Thanks: 0
Thanked 16 Times in 16 Posts
Bug

you could change everything to lower-case and remove any non-letters characters to make it more accurate.


Code:
tr ' ' '\12' <inputfile | tr 'A-Z' 'a-z' | sed s/[^a-zA-Z]//g | sort | uniq -c | sort -nr

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Match and count the number of times cdfd123 Shell Programming and Scripting 1 05-24-2010 09:37 AM
Print a word specific number of times sam_2921 Shell Programming and Scripting 4 05-14-2010 08:06 AM
scripting - write a script that will count the number of times a particular word BigTool4u2 Shell Programming and Scripting 3 06-24-2009 02:46 PM
Looking for a single line to count how many times one character occurs in a word... Shingoshi Shell Programming and Scripting 2 06-12-2009 11:12 PM
Count the number of occurence of perticular word from file rinku Shell Programming and Scripting 40 08-10-2007 08:33 PM



All times are GMT -4. The time now is 09:05 AM.