Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2007-10-02
5
Medium Priority
?
1,395 Views
Last Modified: 2013-12-26
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
Comment
Question by:waipahu
5 Comments
 
LVL 38

Accepted Solution

by:
yuzh earned 2000 total points
ID: 20003412
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
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20004283
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
 
LVL 48

Expert Comment

by:Tintin
ID: 20010504
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
 

Author Comment

by:waipahu
ID: 20017264
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
 
LVL 48

Expert Comment

by:Tintin
ID: 20017568
Seems like a complicated way of doing dircmp

man dircmp
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question