We help IT Professionals succeed at work.

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

1,655 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?
Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
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.
CERTIFIED EXPERT
Top Expert 2007

Commented:
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

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
# ----------------------------------------
CERTIFIED EXPERT
Top Expert 2007

Commented:
Seems like a complicated way of doing dircmp

man dircmp

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.