Word Frequency Sort


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Word Frequency Sort
# 1  
Old 03-18-2011
Word Frequency Sort

hello,
Here is a program for creating a word-frequency

# wf.gk --- program to generate word frequencies from a file
{
# remove punctuation: This will remove all punctuations from the file
gsub(/[^[:alnum:]_[:blank:]]/, "", $0)
#Start frequency analysis
for (i = 1; i <= NF; i++)
freq[$i]++
}
END
#Print output
{
for (word in freq)
printf "%s\t%d\n", word, freq[word]
}
The program runs fine but I cannot get the last part to print out the frequency first and then massage the data to sort from Highest to lowest.
Please help and if possible and if it is not too much trouble, could the code be commented to help me and others like me learn.
Many thanks in advance,

Gimley
# 2  
Old 03-18-2011
Code:
 your_command| sort -k2nr

# 3  
Old 03-18-2011
Hello,
Many thanks for the reply. I applied the command in the file but I still get the frequency ofn the right hand side, whereas the sort I need is as under:
FREQUENCY WORD
and the frequency should be from HIGHEST TO LOWEST.
Sorry for the hassle
Gimley
# 4  
Old 03-18-2011
if the frequency is in first column, try this command:

Code:
 your_command| sort -nr

This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 03-18-2011
Dear rdcwayx,
I tried but to no avail. The modified code is as under: Have I done anyhting wrong ? it consistently spews out word first and the frequency next. Sorry for the hassle. May be I should have given a sample and the desired output
Here is the sample:
FN: testwf
John Mary Henry Henry John Smith Gimley Tolkien

Present Output: testwf.frq

Tolkien 1
Smith 1
Gimley 1
Henry 2
Mary 1
John 2
The code sorts the Names in descending order
Desired output
What I need is frequency first sorted in descending order, followed by name:
2 John
2 Henry
1 Gimley
1 Mary
1 Smith
1 Tolkien
Here is the code, with the modification you suggested built in:

# wordfreq.awk --- print list of word frequencies
{
# remove punctuation
#gsub(/[^[:alnum:]_[:blank:]]/, "", $0)
for (i = 1; i <= NF; i++)
freq[$i]++
}
END {
for (word in freq)
printf "%s\t%d\n", word, freq[word]
sort -nr
}
# 6  
Old 03-18-2011
Try this,
Code:
awk '{gsub(/[^[:alnum:]_[:blank:]]/, "", $0);for (i = 1; i <= NF; i++) {freq[$i]++}} END {for (word in freq){printf "%d\t%s\n", freq[word],word}}' inputfile | sort -nr

# 7  
Old 03-18-2011
Hi Pravin27,
It works and frequency comes first. But the frequency does not seem to sort.
Here is the output
1 Tolkien
1 Smith
1 Gimley
2 Henry
1 Mary
2 John
This is not too bad. I can sort on the frequency if there is no way out.
Many thanks for the help

Gimley
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to align/sort the column pairs of an csv file, based on keyword word specified in another file?

I have a csv file as shown below, xop_thy 80 avr_njk 50 str_nyu 60 avr_irt 70 str_nhj 60 avr_ngt 50 str_tgt 80 xop_nmg 50 xop_nth 40 cyv_gty 40 cop_thl 40 vir_tyk 80 vir_plo 20 vir_thk 40 ijk_yuc 70 cop_thy 70 ijk_yuc 80 irt_hgt 80 I need to align/sort the csv file based... (7 Replies)
Discussion started by: dineshkumarsrk
7 Replies

2. UNIX for Advanced & Expert Users

Sort words based on word count on each line

Hi Folks :) I have a .txt file with thousands of words. I'm trying to sort the lines in order based on number of words per line. Example from: word word word word word word word word word word word word word word word word to desired output: word (2 Replies)
Discussion started by: martinsmith
2 Replies

3. Shell Programming and Scripting

Shell scripting: frequency of specific word in a string and statistics

Hello friends, I need a BIG help from UNIX collective intelligence: I have a CSV file like this: VALUE,TIMESTAMP,TEXT 1,Sun May 05 16:13:05 +0000 2013,"RT @gracecheree: Praying God sends me a really great man one day. Gotta trust in his timing. 0,Sun May 05 16:13:05 +0000 2013,@sendi__... (19 Replies)
Discussion started by: kraterions
19 Replies

4. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

5. Shell Programming and Scripting

Script to sort large file with frequency

Hello, I have a very large file of around 2 million records which has the following structure: I have used the standard awk program to sort: # wordfreq.awk --- print list of word frequencies { # remove punctuation #gsub(/_]/, "", $0) for (i = 1; i <= NF; i++) freq++ } END { for (word... (3 Replies)
Discussion started by: gimley
3 Replies

6. Shell Programming and Scripting

Help with calculating frequency of specific word in a string

Input file: #read_1 AWEAWQQRZZZQWQQWZ #read_2 ZZAQWRQTWQQQWADSADZZZ #read_3 POGZZZZZZADWRR . . Desired output file: #read_1 3 #read_1 1 #read_2 2 #read_2 3 #read_3 6 . . (3 Replies)
Discussion started by: perl_beginner
3 Replies

7. Shell Programming and Scripting

word frequency counter - awk solution?

Dear all, i need your help on this. There is a text file, i need to count word frequency for each word with frequency >40 in each line of file and output it into another file with columns like this: word1,word2,word3, ...wordn 0,0,1 1,2,0 3,2,0 etc -- each raw represents... (13 Replies)
Discussion started by: irrevocabile
13 Replies

8. Shell Programming and Scripting

Word frequency with additional information

Hello everyone, I am using a chunk of code to display the frequency of a file name in a list of directories. The code looks like this: find . -name "*.log" | cut -d/ -f4 | cut -d. -f1 | awk '{print $1}' | sort | uniq -c | sort -nr The file paths would look something like this:... (1 Reply)
Discussion started by: ToeLint
1 Replies

9. Shell Programming and Scripting

Determining Word Frequency of Specific Terms

Hello, I require a perl script that will read a .txt file that contains words like 224.199.207.IN-ADDR.ARPA. IN NS NS1.internet.com. 4.200.162.207.in-addr.arpa. IN PTR beeriftw.internet.com. arroyoeinternet.com. IN A 200.199.227.49 I want to focus on words: IN... (23 Replies)
Discussion started by: richsark
23 Replies

10. Shell Programming and Scripting

how to sort a word

Can you Tell me how to sort a word (alphabetically using shell scripts only not by using perl script) For example : input word is damodar Output : aaddmor (1 Reply)
Discussion started by: gyana_cboy
1 Replies
Login or Register to Ask a Question