Counts a number of unique word contained in the file and print them in alphabetical order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counts a number of unique word contained in the file and print them in alphabetical order
# 1  
Old 11-12-2009
Counts a number of unique word contained in the file and print them in alphabetical order

What should be the Shell script that counts a number of unique word contained in a file and print them in alphabetical order line by line?
# 2  
Old 11-12-2009
what have you got so far? after 100+ posts you are not a shell newbie any more.
# 3  
Old 11-12-2009
Not sure what you're looking for exactly. This will sort a file and give you the unique lines.

cat myfile | sort | uniq > outfile

If you are looking for something different, can you provide and example of the input file and then what you want the output to look like?
# 4  
Old 11-12-2009
There is a file containing the following:
Code:
Process is a program under execution but thread is a light weight process which has separate way of execution.

The output should be like:

Code:
a
execution
but
has
is
light
of
process
program
separate
thread
under
way
weight
which

i.e., Determine unique word contained in the file and print them in alphabetical order line by line.
# 5  
Old 11-12-2009
and so where is your code? what have you tried?
# 6  
Old 11-13-2009
Code:
$ cat input-file
process is a program under execution but thread is a light weight process which has separate way of execution.

Code:
$ tr ' ' '\n' < input-file | sort | uniq
a
but
execution
execution.
has
is
light
of
process
program
separate
thread
under
way
weight
which

Anyway you have to answer the @ghostdog74 questions.
# 7  
Old 11-13-2009
I think perl works nicely for this...

Code:
#!/usr/bin/perl

use strict;

my %seen;     # hash used to store how many times a word has been seen.
my @a_word;   # array to store uniq words.
my $word;     # variable to store each word.

#
# Loop through all lines in the file
#
while (<>)
{

   #
   # Split each line and proces each word.
   #
   for $word (split)
   {

      #
      # Change the word to lowercase.
      #
      $word = "\L$word";

      #
      # Create or increment the hash for the word.
      # This lets the hash act as a word counter as well.
      #
      $seen{$word}++;

      #
      # If this is the first time the word has been seen.
      # add the word to the arraw of words.
      #
      if ($seen{$word} == 1)
      {
         push( @a_word, $word );
      }
   }
}

#
# Sort the contents of the array of words.
#
@a_word = sort(@a_word);

#
# Print out each word and the number of times it was found.
#
foreach (@a_word)
{
   print "$_ $seen{$_}\n";
}

exit
#
# end of wc.pl
#

Sample Input Data

one two
two
three four
four
zabc
five
six Eight
seven
wdef
eight
nine
ten

Output

./wc.pl < sample.dat

eight 2
five 1
four 2
nine 1
one 1
seven 1
six 1
ten 1
three 1
two 2
wdef 1
zabc 1

Hope this helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. UNIX for Beginners Questions & Answers

How to create a summary file of all files in a directory sorted in reverse alphabetical order.?

I have an interactive script which works terrific at processing a folder of unsorted files into new directories. I am wondering how I could modify my script so that( upon execution) it provides an additional labelled summary file on my desktop that lists all of the files in each directory that... (4 Replies)
Discussion started by: Braveheart
4 Replies

3. Shell Programming and Scripting

Want to grep records in alphabetical order from a file and split into other files

Hi All, I have one file containing thousands of table names in single column. Now I want that file split into multiple files e.g one file containing table names starting from A, other containing all tables starting from B...and so on..till Z. I tried below but it did not work. for i in... (6 Replies)
Discussion started by: shekhar_4_u
6 Replies

4. Shell Programming and Scripting

Sorting lines between patterns in alphabetical order

Hi, need help in sorting lines between strings "<section status = “ole-service”>" and "</section>" in alphabetical order, based on the text in red. Hoping for an AWK or SED solution. Thank you. ... <section status = “ole-service”>... <p service = "OOO">XZZ</p> <p service = "AAA">AAA... (3 Replies)
Discussion started by: pioavi
3 Replies

5. UNIX for Dummies Questions & Answers

Script to list applications in alphabetical order

I've looking over a script for work and I've had a problem with the script not listing the files in alphabetical order. To look up PIDs for apps, it would be beneficial to have them listed in that order. Here is what I've been reviewing. #!/usr/bin/perl $str = sprintf "%4s %-40s", "PID",... (7 Replies)
Discussion started by: whysolucky
7 Replies

6. Shell Programming and Scripting

[SHELL] Userlist alphabetical order

Hi everyone! I am new to the forum and have recently started working with Linux. Quick question, I want a user list in alphabetical order as the output of a shell script. Who can help me!? Thanks! From the netherlands ;) (5 Replies)
Discussion started by: dennisbest85
5 Replies

7. UNIX for Dummies Questions & Answers

How can I list the file under a directory both in alphabetical and in reverse alphabetical order?

How can I list the file under current directory both in alphabetical and in reverse alphabetical order? (1 Reply)
Discussion started by: g.ashok
1 Replies

8. Shell Programming and Scripting

Regarding about the print line number/order

Hello, I am trying to print line number/order using this command awk '{print $0, FNR}' myfilename 11006 A41 1888 11006 A41 1888 11006 A41 1888 11006 A41 ... (2 Replies)
Discussion started by: davidkhan
2 Replies

9. Shell Programming and Scripting

alphabetical order with out using sort command

hai, how can i sort a file alphabetically without using sort command (6 Replies)
Discussion started by: rahul801
6 Replies

10. Shell Programming and Scripting

shell program for sorting strings in an alphabetical order

Hi, I trying to find the solution for writing the programming in unix by shell programming for sorting thr string in alphabetical order. I getting diffculty in that ,, so i want to find out the solution for that Please do needful Thanks Bhagyesh (1 Reply)
Discussion started by: bp_vanarse
1 Replies
Login or Register to Ask a Question