• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1504
  • Last Modified:

korne shell to read ascii file and load it into an array

I want to create a unix korne shell script that does the following:

- read an ascii file
- put each line into an array

how would I code this?
0
waipahu
Asked:
waipahu
1 Solution
 
yuzhCommented:
It is not a good idea to store the file into an array, The Ksh array can have a MAX 512 elements. [0-511], that means if your file has more than 512 lines, you will be in trouble.

If you still want to do it, here's an example of the script:

#!/bin/ksh
# You can modify it as you wish.
INPUT=/path-to/myfile
FLINES=`wc -l $INPUT | awk '{print $1}'`
if [ $FLINES -gt 512 ] ; then
    echo "$INPUT has more than 512 lines, cannot fix into an array"
    exit
fi
# Now put the file into an array
typeset -i cnt=0
IFS="\012"   # read in the whole line
exec 0<$INPUT
while read myarray[$cnt] do;
     echo "current line is $cnt
     ((cnt = $cnt + 1))
done
# then other actions.
exit

#End of script

To learn more:
http://members.tripod.com/b62/doc/docksh.htm
http://www.well.ox.ac.uk/~johnb/comp/unix/ksh.html
http://doc.novsu.ac.ru/oreilly/unix/ksh/ch06_03.htm
0
 
ghostdog74Commented:
actually you should describe what exactly you are trying to do. This is because arrays are of no use if you are not making use of it.
0
 
TintinCommented:
I'll 2nd ozo's and ghostdog's comments.

We see a lot of questions here asking how to X, when the solution was that it was much better to do Y once we'd found out what the actual problem was.
0
 
waipahuAuthor Commented:
This is what I finally ended up doing
I have batch jobs that creates reports. I needed a way to compare a set of reports against each other when I do testing. Comparing PRODUCTION reports against TEST reports.  

# ******************************
# compare_files
# - compares 1 or more files with the same prefix
# -  input prefix name (i.e. cpsjr726)
# ******************************
#read input_file
echo "--------------------------"
echo "Compare files"
echo "- syntax directory: i.e. /PROD/rpt"
echo "- syntax file name: i.e. cpsjr718"
echo "--------------------------"
echo "Enter 1st directory :"
read dir1
echo "Enter 2nd directory :"
read dir2
echo "Enter file prefix   :"
read prefix
echo "dir1 >" $dir1
echo "dir2 >" $dir2
echo "prefix >" $prefix
echo "------------------------------------"
set -A dir1_array
ls -d -1 $dir1/$prefix.* >/$DIRTMP/file.list1.txt
let x=0
while read line ; do
case $line in
    \#*) ;;
      *)  ((x=x+1));dir1_array[$x]=$line;;
# sum=$[sum+$x]

esac
# echo $x ${dir1_array[$x]}
done <$DIRTMP/file.list1.txt
# echo $x
# ------------------------------------------
set -A dir2_array
ls -d -1 $dir2/$prefix.* >/$DIRTMP/file.list2.txt
let y=0
while read line ; do
case $line in
     \#*) ;;
       *) ((y=y+1));dir2_array[$y]=$line;;
esac
done <$DIRTMP/file.list2.txt
# ------------------------------------------
if ((x != $y))
then
   echo "warrning: no. of records is different" $x $y
else
   echo "valid comparison:" $x $y
fi
if ((x <= $y))
then
   ((max=x))
else
   ((max=y))
fi

# -----------------------------------------
# loop max times
# execute diff command
# -----------------------------------------
cnt=0
while
   [ $cnt -lt max ]
   do
      ((cnt=cnt+1))
      echo " ---------------------------------------"
      echo " comparing: " $cnt ${dir1_array[$cnt]}
      echo " comparing: " $cnt ${dir2_array[$cnt]}
      echo " ---------------------------------------"
      diff ${dir1_array[$cnt]} ${dir2_array[$cnt]} | more
   done
# ----------------------------------------
0
 
TintinCommented:
Seems like a complicated way of doing dircmp

man dircmp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now