#!/usr/bin/ksh
# reading content of file into arrays
INDEX=0
while read NAMES[${INDEX}] NUMBERS[${INDEX}]
do
((INDEX=$INDEX+1))
done < <inputfile>
#displaying values in each array
for NAME in ${NAMES[@]}
do
echo ${NAME}
done
for NUMBER in ${NUMBERS[@]}
do
echo ${NUMBER}
done
Hi,
How about read the content into arrays, I can use awk to read it content by column
But the challenge is how to declare array NAMES and NUMBERS,
Can you show me the way?
What abt this...? I thnk this shuold remove the '#'es and blank lines... I hvn't tested this nyway....!!
sed 's/^#//g' data_file -->will remove #es and grep -v '^$' will remove the blank lines...
for i in $(sed 's/^#//g' data_file |grep -v '^$'| awk -F" " '{print NR}')
do
nam_arr[$i]=$(awk -F" " -v cnter=$i '{ if (cnter == NR) {print $1;} }' data_file)
num_arr[$i]=$(awk -F" " -v cnter=$i '{ if (cnter == NR) {print $2;} }' data_file)
done
Oops..Just tested and corrected my mistake..Sorry abt that!!!
Here is my tested data file:
-----------------------------------------------
Mike 3434
Jack 481
Peter 12
Alan 926
#
Jacob 3423
Dennis 123
------------------------------------------------------------------------
Try for this script....
for i in $(cat data_file | sed -e 's/^#//g' -e '/^ *$/d' | awk -F" " '{print NR}')
do
nam_arr[$i]=$(cat data_file | sed -e 's/^#//g' -e '/^ *$/d' | awk -F" " -v cnter=$i '{ if (cnter == NR) {print $1;} }' )
num_arr[$i]=$(cat data_file | sed -e 's/^#//g' -e '/^ *$/d' |awk -F" " -v cnter=$i '{ if (cnter == NR) {print $2;} }' )
done
echo Array details
for i in $(cat data_file | sed -e 's/^#//g' -e '/^ *$/d' | awk -F" " '{print NR}')
do
echo nam_arr[$i]=${nam_arr[i]}
echo num_arr[$i]=${num_arr[i]}
done
-------------------------------------------------------------------
Result is shown below:
Array details
nam_arr[1]=Mike
num_arr[1]=3434
nam_arr[2]=Jack
num_arr[2]=481
nam_arr[3]=Peter
num_arr[3]=12
nam_arr[4]=Alan
num_arr[4]=926
nam_arr[5]=Jacob
num_arr[5]=3423
nam_arr[6]=Dennis
num_arr[6]=123
-----------------------------------------------------------------------
I hope there won't b "MORE" mistakes in this