![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 | ||
|
|
|
|||
|
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
|