Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
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 ...
  1. #1
    Just 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

  2. #2
    Just Joined!
    Join Date
    May 2007
    Posts
    15
    help please

  3. #3
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Is this homework?
    Registered Linux user #270181
    TechieMoe's Tech Rants

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

  5. #5
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    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

  6. #6
    Linux 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 already
    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.
    For 1) then, you must parse your list of parsed words...

    I don't know about application what does such primitives (steps), I would do it in C/C++ personally... Or you can try perl

  7. #7
    Linux User Dark_Stang's Avatar
    Join Date
    Jun 2006
    Location
    Around St. Louis
    Posts
    284
    I could easily do this in a programming language but... bash?
    Two levels higher than a newb.
    (I can search google)

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    This would do the trick:

    Code:
    awk ' { line[$0]++ } END { for( num in line) { print num , line[num] } } ' file
    Regards

  9. #9
    Linux 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

  10. #10
    drl
    drl is offline
    Linux Engineer drl's Avatar
    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 )

Page 1 of 3 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...