The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Seperate contents in a file with | as delimiter risshanth UNIX for Dummies Questions & Answers 5 01-23-2008 01:26 AM
seperate elements of a file nektarios4u Shell Programming and Scripting 1 11-02-2007 09:53 AM
Split File into seperate files eltinator Shell Programming and Scripting 4 08-03-2007 11:27 AM
How can I find the 3 first letters from the name file steiner Shell Programming and Scripting 8 06-17-2005 05:10 AM
Reversing file order using SED MBGPS Shell Programming and Scripting 2 09-30-2002 06:43 AM

Closed Thread
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-05-2004
Registered User
 

Join Date: Nov 2004
Posts: 2
look in file, seperate letters, put in order...

okay, I need some help! Im trying to write a script where it looks in the file you designate, pulls apart all the words so i can count how many of each letter there is in the file, then i need to put them in the order of the most occuring letter to the least. This most likley will need a loop inside a loop so i can go through the whole alphabet, but my understanding of the loops and their structure is quite limited at the moment.
the loop will probably need to look somthing like this:
for x in {item1 item2...itemn}; do
statements to be repeated
done

i know thats the basic, but there need to be a loop on the inside, and i have no idea how to set either up, i need to somehow make it run like in c++ like i > z i++ type of thing, so any help possible would be awesome! thanks...
Forum Sponsor
  #2 (permalink)  
Old 11-05-2004
RTM's Avatar
RTM RTM is offline
Hog Hunter
 
Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,039
Count the number of occurances of all letters in a file?

WHY?
  #3 (permalink)  
Old 11-05-2004
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,491
Code:
#!/bin/ksh 

set -A mycount  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
set -A alphabet A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
let offset=0
char=""
record=""
add_letter ()
{
   let offset=30
   case $char in
     [Aa]) let offset=1
     ;;
     [Bb]) let offset=2
     ;;
     [Cc]) let offset=3
     ;;
     [Dd]) let offset=4
     ;;
     [Ee]) let offset=5
     ;;
     [Ff]) let offset=6
     ;;
     [Gg]) let offset=7
     ;;
     [Hh]) let offset=8
     ;;
     [Ii]) let offset=9
     ;;
     [Jj]) let offset=10
     ;;
     [Kk]) let offset=11
     ;;
     [Ll]) let offset=12
     ;;
     [Mm]) let offset=13
     ;;
     [Nn]) let offset=14
     ;;
     [Oo]) let offset=15
     ;;
     [Pp]) let offset=16
     ;;
     [Qq]) let offset=17
     ;;
     [Rr]) let offset=18
     ;;
     [Ss]) let offset=19
     ;;     
     [Tt]) let offset=20
     ;;
     [Uu]) let offset=21
     ;;
     [Vv]) let offset=22
     ;;
     [Ww]) let offset=23
     ;;
     [Xx]) let offset=24
     ;;
     [Yy]) let offset=25
     ;;
     [Zz]) let offset=26
     ;;
    esac 
    if [ $offset -lt 30 ]; then
       let offset=$offset-1
       mycount[$offset]=`echo "${mycount[offset]} + 1" | bc`
    fi
}
split_to_char()
{
	echo $record | awk '{ for (i=1;i<length($0);i++) print substr($0,i,1) }' |
	while read char
	do
	   
	    add_letter
    done
}

while read record
do
   split_to_char;    
done < $1

touch tmp.tmp

> tmp.tmp

let i=0

while [ $i -lt ${#mycount[*]} ]
do
     printf "%5d %s\n" ${mycount[i]} ${alphabet[i]} >> tmp.tmp
     let i=$i+1
done
echo "Letter count for $1"
echo "Count Letter"
echo "----- ------"
sort -n tmp.tmp 
echo " "
echo "Most common letter"
echo "Count Letter"
echo "----- ------"
sort -n -r tmp.tmp  | head -1
rm -f tmp.tmp
exit
  #4 (permalink)  
Old 11-05-2004
Kelam_Magnus's Avatar
Unix does a body good.
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
Re: look in file, seperate letters, put in order...

Quote:
Originally posted by chekeitout
okay, I need some help!

i know thats the basic, but there need to be a loop on the inside, and i have no idea how to set either up, i need to somehow make it run like in c++ like i > z i++ type of thing, so any help possible would be awesome! thanks...
Chek,

This seems amazingly like homework. It seems to me if it were a real world question, you would have at least attemped to code it. Which is or should be a requirement for our site.

In the future, dont post these types of questions. Blatantly asking for an answer is not the purpose or goal of this website.

US helping YOU to solve YOUR problem is the goal, enriching knowledge, not supplying answers.

I am closing this thread based on the fact that this is a homework question.

Keep on posting and remember to at least attempt the answer first.
__________________
My brain is your brain
Google UNIX.COM
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0