Navigation Bar

Saturday, July 9, 2016

Basic Arrary usage in shell scripts

The basic block of any programming language is arrays. In shell scripting however their usage may not be much due to the limitation of traversing through the array.
Below are a few examples of reading data from a file and putting the data into an array, adding new elements to an array, copying data from one array into another array, adding an array element in between a list of array elements etc. Error handling, user interface etc are not handled in the scripts.

sample file a.txt
-------------------
aaaaaaa
bbbbbbb
ccccccc
ddddddd
eeeeeee
fffffff
ggggggg
hhhhhhh

A simple example to read elements from a file, add them to an array and print the array elements
----------------------------------------------------------------------------------------------------------------------
arr.sh
#!/bin/bash
arrcnt=0
for i in `cat a.txt`
do
# Techo $i
 arrcnt=`expr $arrcnt + 1`
 array[$arrcnt]=$i
done
index=1
while [ "$index" -le "$arrcnt" ]
do
# echo $index
 echo ${array[$index]}
    index=`expr $index + 1`
done


to compare 2 arrays and print the mismatching elements
-------------------------------------------------------------------
To compare the data in 2 files and print the mismatch in records. In case the files contain sentences
you will need to put Field separator as "newline" at the beginning of the script
IFS='
'
spaces etc will also need to be handled using something like tr -s " "" " for each line that is read, to convert multiple spaces into single space.

arrcmp.sh
#!/bin/bash
arrcnt=0
for i in `cat a.txt`
do
# echo $i
 arrcnt=`expr $arrcnt + 1`
 array[$arrcnt]=$i
done
arrcnt=0
for i in `cat b.txt`
do
# echo $i
 arrcnt=`expr $arrcnt + 1`
 array1[$arrcnt]=$i
done
index=1
while [ "$index" -le "$arrcnt" ]
do
# echo $index
# echo ${array[$index]}
 if [ ${array[$index]} = ${array1[$index]} ]
 then
  echo "Match"
 else
  echo "Mismatch - " ${array[$index]} " :: " ${array1[$index]}
 fi
    index=`expr $index + 1`
done

An example to insert a new element before a particular element in an array
----------------------------------------------------------------------------------------
In this example the array is traversed till the matching element is found. The new element is added in this position and the remaining elements are pushed down the array list.

--arrInject.sh
#!/bin/bash
arrcnt=0
injval=$1
before=$2
for i in `cat a.txt`
do
#       echo $i
        arrcnt=`expr $arrcnt + 1`
        array[$arrcnt]=$i
done
index=1
injflag=0
while [ "$index" -le "$arrcnt" ]
do
#       echo $index
        if [ ${array[$index]} = $before ]
        then
                tmp=${array[$index]}
        array[$index]=$injval
                injflag=1
        else
                if [ $injflag -eq 1 ]
                then
#                       echo "111111111"
                        tmp1=${array[$index]}
                        array[$index]=$tmp
                        tmp=$tmp1
                fi
        fi
    index=`expr $index + 1`
done
#Handling the boundary condition
array[$index]=$tmp
index=1
newarrcnt=`expr $arrcnt + 1`
while [ "$index" -le "$newarrcnt" ]
do
        echo ${array[$index]}
    index=`expr $index + 1`
done


to concatenate data from 2 files into a single array
------------------------------------------------------------
Here the elements of 1 file is read into an array. The array counter is incremented and elements of the 2nd file are appended to this array
arrconcat.sh
#!/bin/bash
arrcnt=0
for i in `cat a.txt`
do
# echo $i
 arrcnt=`expr $arrcnt + 1`
 array[$arrcnt]=$i
done
for i in `cat b.txt`
do
#       echo $i
        arrcnt=`expr $arrcnt + 1`
        array[$arrcnt]=$i
done
index=1
while [ "$index" -le "$arrcnt" ]
do
# echo $index
 echo ${array[$index]}
    index=`expr $index + 1`
done


Quote for the day
“Knowing Is Not Enough; We Must Apply. Wishing Is Not Enough; We Must Do.”- Johann Wolfgang Von Goethe

No comments:

Post a Comment