The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 11-18-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

i would suggest to first read in the whole file in three array, one for the serial, one for the user and one for the ipadd.

Code:
declare -a SERIAL ACCT IPADD 
let count=0 
while IFS=, read a b c 
do 
    SERIAL[$count]=$a; ACCT[$count]=$b; IPADD[$count]=$c; ((count++)) 
done < file
This sets the internal field separator to "," and reads the three comma separate fields into the three variables which are pushed into the declared arrays.

Now you can search this array like this:

Code:
let i=0 
while [[ $i -lt ${#SERIAL[@]} ]] 
do 
  [[ ${SERIAL[${i}]} = W120* ]] && printf "%s %s\n" ${ACCT[${i}]} ${IPADD[${i}]} 
  ((i++)) 
done
This loops trough the array. ${#SERIAL[@]} gives you the number of entries of an array. If a certain SERIAL is found, the corresponding data are output.

HTH Chris