Read from data file

Hi,
I have a data file formatted like this:
[Name] [Number]

Ex:
Mike 3434
Jack 481
Peter 12
Alan 926

I want to get this data into 2 variables: "Names" and "Numbers" that I can using one "for" loop to get the value as Names [i]and Numbers
[i]Like this:

for i in 0 1 2 3 
do
   echo $Names[$i]
   echo $Numbers[$i]
done

Can anyone help ?

#!/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
1 Like

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?

Give a try on this...

#!/bin/bash
for i in $(awk -F" " '{print NR}' new)
do
nam_arr[$i]=$(awk -F" " -v cnter=$i '{ if (cnter == NR) {print $1;} }' new)
num_arr[$i]=$(awk -F" " -v cnter=$i '{ if (cnter == NR) {print $2;} }' new)
done

In my perv post, nam_arr[<index>] and num_arr[<index>] are Arrays!!!Hope this is what you need...
And 'new' is the file which contain the data

It works!
Thank jacoden and sb008!

Hi jacoden,

I have one questions:
Data file may contain empty lines or un-used lines (start with "#" character). Can we exclude them from the arrays.

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

If the blank lines contain spaces then use this

grep -v '^ *$'

Jacoden,

The sed and grep command does not work in that script.
Still have blank lines and comment_out lines

try this

for i in $(sed '/^#/d;/^ *$/d' file| awk -F" " '{print NR}')

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

Yeah, great!
Thank you all!