Results 1 to 10 of 24
Hello
I have a text file , I want to count every word how many times it's repeated in this file then put the results in outpout file like
Going ...
- 05-10-2007 #1Just Joined!
- Join Date
- May 2007
- Posts
- 15
counting words in file and sorting them according to their frequency
Hello
I have a text file , I want to count every word how many times it's repeated in this file then put the results in outpout file like
Going 9
right 8
yes 7
now 6
.....
hello 2
seperated by tab
ignoring the case of letters (YeS=yes)
I need your help to write a script achieve that
thanx
- 05-10-2007 #2Just Joined!
- Join Date
- May 2007
- Posts
- 15
help please
- 05-10-2007 #3
Is this homework?
Registered Linux user #270181
TechieMoe's Tech Rants
- 05-10-2007 #4Just Joined!
- Join Date
- May 2007
- Posts
- 15
Never,
In fact I'm doing research under linux and I'm beginner user of Linux, But I confirm that is really not a homework
thanx in advance
- 05-10-2007 #5
I'm not a scripting guru, but to find specific words in a file you can use grep. To count how many words you can use the word count utility wc. What you'd probably do is grep for a word and pipe the output of grep into wc, then print some sort of message with the output.
Registered Linux user #270181
TechieMoe's Tech Rants
- 05-10-2007 #6Linux Newbie
- Join Date
- Sep 2005
- Location
- CZ
- Posts
- 164
That's quite a too complex task for bash scripting...
For every word you have to
1) check if you have parsed the word alreadyFor 1) then, you must parse your list of parsed words...1a) if you have, increment a counter for this word
1b) if you don't, put the word in a list of parsed words and add a new counter for the word.
I don't know about application what does such primitives (steps), I would do it in C/C++ personally... Or you can try perl
- 05-10-2007 #7
I could easily do this in a programming language but... bash?
Two levels higher than a newb.
(I can search google)
- 05-10-2007 #8Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
This would do the trick:
RegardsCode:awk ' { line[$0]++ } END { for( num in line) { print num , line[num] } } ' file
- 05-10-2007 #9Linux Newbie
- Join Date
- Sep 2005
- Location
- CZ
- Posts
- 164
Great job!
Don't wanna complain but...
Either I got something wrong or this really wouldn't output what was asked for... A summary for each one word on one line...
Also I don't see any sorting support in your AWK "program".

But great for a start! A few modifications and perhaps it would work some day.
Still I would better use the C/C++

Edit:
Hey, please don't take any offense. I really respect your awk knowledges
- 05-10-2007 #10Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
I know that mystical dervish has seen an answer in another forum, but this problem has an interesting history, noted in a number of places. The most recent I have read is in Classic Shell Scripting, Robbins and Beebe, O'Reilly, 2005.
Briefly, in the 1980s, Knuth wrote a program to handle this task as a response to a challenge from Jon Bentley, author of Programming Pearls columns in the CACM.
Doug McIlroy looked at the program, and wrote a six-step shell script to solve the same problem.
You can see one version of that solution tucked away as one of the example scripts in the zipped file from the book at http://examples.oreilly.com/shellsrptg/ in file sh/wf.sh -- The README file in the zip archive has, among other things, a list of 150 URLs relating in some way to shell scripting.
NOTE: there are some requirements about posting the examples from the book ... cheers, drl
( edit 1: addition )Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote