Sponsored Content
Top Forums UNIX for Advanced & Expert Users look in file, seperate letters, put in order... Post 57780 by jim mcnamara on Friday 5th of November 2004 02:40:28 PM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

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

5. 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

6. 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

7. 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

8. 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

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

10. 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
LSEEK(2)						      BSD System Calls Manual							  LSEEK(2)

NAME
lseek -- reposition read/write file offset SYNOPSIS
#include <unistd.h> off_t lseek(int fildes, off_t offset, int whence); DESCRIPTION
The lseek() function repositions the offset of the file descriptor fildes to the argument offset according to the directive whence. The argu- ment fildes must be an open file descriptor. Lseek() repositions the file pointer fildes as follows: If whence is SEEK_SET, the offset is set to offset bytes. If whence is SEEK_CUR, the offset is set to its current location plus offset bytes. If whence is SEEK_END, the offset is set to the size of the file plus offset bytes. The lseek() function allows the file offset to be set beyond the end of the existing end-of-file of the file. If data is later written at this point, subsequent reads of the data in the gap return bytes of zeros (until data is actually written into the gap). Some devices are incapable of seeking. The value of the pointer associated with such a device is undefined. RETURN VALUES
Upon successful completion, lseek() returns the resulting offset location as measured in bytes from the beginning of the file. Otherwise, a value of -1 is returned and errno is set to indicate the error. ERRORS
Lseek() will fail and the file pointer will remain unchanged if: [EBADF] Fildes is not an open file descriptor. [ESPIPE] Fildes is associated with a pipe, socket, or FIFO. [EINVAL] Whence is not a proper value. SEE ALSO
dup(2), open(2) BUGS
This document's use of whence is incorrect English, but is maintained for historical reasons. STANDARDS
The lseek() function conforms to IEEE Std 1003.1-1988 (``POSIX.1''). 4th Berkeley Distribution April 19, 1994 4th Berkeley Distribution
All times are GMT -4. The time now is 03:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy