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

Posted on 2007-10-02
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?
Question by:waipahu
    LVL 38

    Accepted Solution

    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:

    # You can modify it as you wish.
    FLINES=`wc -l $INPUT | awk '{print $1}'`
    if [ $FLINES -gt 512 ] ; then
        echo "$INPUT has more than 512 lines, cannot fix into an array"
    # 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))
    # then other actions.

    #End of script

    To learn more:
    LVL 9

    Expert Comment

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

    Expert Comment

    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.

    Author Comment

    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]

    # 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;;
    done <$DIRTMP/file.list2.txt
    # ------------------------------------------
    if ((x != $y))
       echo "warrning: no. of records is different" $x $y
       echo "valid comparison:" $x $y
    if ((x <= $y))

    # -----------------------------------------
    # loop max times
    # execute diff command
    # -----------------------------------------
       [ $cnt -lt max ]
          echo " ---------------------------------------"
          echo " comparing: " $cnt ${dir1_array[$cnt]}
          echo " comparing: " $cnt ${dir2_array[$cnt]}
          echo " ---------------------------------------"
          diff ${dir1_array[$cnt]} ${dir2_array[$cnt]} | more
    # ----------------------------------------
    LVL 48

    Expert Comment

    Seems like a complicated way of doing dircmp

    man dircmp

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    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.
    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…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now