Wednesday, June 8, 2011

reading a file into array

#!/bin/bash
# Reading a file into a array and then parsing out
if [ -z "$1" ]
then
   echo $0: usage: $0 filename
  exit 0
fi
if [ ! -s "$1" ]
then
        echo "The file is Empty : $1 "
fi
datafile=$1
data=() # Array Declaration
count=0
filecontent=( `cat "$datafile" `)
for t in "${filecontent[@]}"
do
 data[$count]=$t
 echo ${data[$count]}
#IN="hostname;cpu"
arr=$(echo ${data[$count]} | tr "," "\n")
for x in $arr
do
    echo $x
done
done
echo "Read file content!"
echo ${#data[@]}

No comments: