look in file, seperate letters, put in order...


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users look in file, seperate letters, put in order...
# 1  
Old 11-05-2004
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...
# 2  
Old 11-05-2004
Count the number of occurances of all letters in a file?

WHY?
# 3  
Old 11-05-2004
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  
Old 11-05-2004
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract columns into seperate file

I have a comma delimited file as per the one below and I am currently extracting the values in 2 columns (COL1 & COL6) to produce a smaller trimmed down version of the file which only contains the columns we need; COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9... (1 Reply)
Discussion started by: Ads89
1 Replies

2. Shell Programming and Scripting

Seperate Odd and Even numbers from 1 file to 2 files

Hey guys. I have been trying to figure out an easy way to seperate a liste of 150k numbers (10 digits) in a .txt file into odd and even numbers with each of their own files, for a project at work. I've tried Excel, but it was too much for it and it wasnt very simple. So i gave up after... (13 Replies)
Discussion started by: TranceC
13 Replies

3. UNIX for Dummies Questions & Answers

looping through file and creating seperate files

Hi, My first post!! I have a files with header, something like this Header_Row AMC|D1|D2|D2 AAO|D3|D4|D5 AMC|D6|D7|D8 AAO|D9|D10|D11 . . . . . and millions fo records thereafter like this. I want to read the above file in a loop and write the lines having AMC into another... (1 Reply)
Discussion started by: amitbakre
1 Replies

4. Shell Programming and Scripting

Put parentheses around all capital letters using SED

Hello everyone I tell you that I'm trying to do a bash program that can put parentheses around each capital letter of each line using SED. I tell you probe with: sed -e '1,$s/A/(A)/g' "$file" but only add parentheses in A. then tested with: sed 'y/AB/(A)(B)/' "$archivo" but it... (3 Replies)
Discussion started by: adiegorpc
3 Replies

5. Shell Programming and Scripting

Seperate file content

hi all, i have some file whoes contents are 0 /home8/mc09ats/UnixCw/a1 1 /home8/mc09ats/UnixCw/a2 2 /home8/mc09ats/b3 3 /home8/mc09ats/UnixCw/d1 i want to seperate the content following way... fileindex= 0 filepath=... (4 Replies)
Discussion started by: AbhijitIT
4 Replies

6. Shell Programming and Scripting

Set lines of in a file to seperate vars

In a bash script, I'm looking for a way to set each matching line of a file into its own variable, or variable array. As an example, i have a crontab file with several entries: 00 23 * * * /usr/local/bin/msqlupdate -all 00 11 * * * /usr/local/bin/msqlupdate -inc 00 03 * * *... (2 Replies)
Discussion started by: lochraven
2 Replies

7. UNIX for Dummies Questions & Answers

Script for parsing details in a log file to a seperate file

Hi Experts, Im a new bee for scripting, I would ned to do the following via linux shell scripting, I have an application which throws a log file, on each action of a particular work with the application, as sson as the action is done, the log file would vanish or stops updating there, the... (2 Replies)
Discussion started by: pingnagan
2 Replies

8. UNIX for Dummies Questions & Answers

Seperate contents in a file with | as delimiter

Hi, I do have a file with follwoing as contents: 816|817118| 816|933370| 816|1215241| I want to store the above values into two arrays as follows: arr1 = { 816,816,816} arr2 = {817118,933370,1215241} How it can be achieved ? (5 Replies)
Discussion started by: risshanth
5 Replies

9. Shell Programming and Scripting

seperate elements of a file

i want to write a script in Bash Shell that accept a list of files.an example of file is 4334:234 322.345:32 234:3452 e.t.c each file only contain lines like num1:num2 i want to count the lines of this file and find the summary of X=4334+322.345+234 and Y=234+32+3452 (1 Reply)
Discussion started by: nektarios4u
1 Replies

10. Shell Programming and Scripting

Split File into seperate files

Hi, So I have a text file which I want to separate into separate text files. I would use the split command but the problem here is that the text file is separated by delimiters. For example: blah blah blah ------ more text ----- and some more text So basically the first part should be... (4 Replies)
Discussion started by: eltinator
4 Replies
Login or Register to Ask a Question