read file into array

read file into array

Post by Benjamin.Altma » Thu, 19 Apr 2001 03:34:42



Don't use expr for math firstly. Avoid doing system calls to get fields
secondly.

You could probably replace this:

        u_host[u_count]=`echo $line | awk '{print $1}'`
with:
        u_host[$((${#u_host} + 1))]=${line%%[     ]*}
where the [     ] contains a space and a tab.  The advantage being that 3
system calls have been replaced which should be a speed gain.

Ben


> #!/bin/bash

> u_hostfile=/etc/firewall/u_hosts

> # this works but is somewhat slow!!!
> while read line
>   do

>     u_host[u_count]=`echo $line | awk '{print $1}'`
>     echo Internal Host $u_count: ${u_host[$u_count]}
>   done <$u_hostfile

> # another attemp but it doesn'nt work, 'cause every element
> # of the array contains the complete file
> while true
>   do

>     u_host[u_count]=`cat $u_hostfile | awk '{print $1}' | grep -v '^#'`
>     echo Internal Host $u_count: ${u_host[$u_count]}
>   done

> Can someone tell me a solution or tell me why the first example
> is so slow. Can you give me any fast solutions?

> Thanx...
>            ...Ren

 
 
 

read file into array

Post by Benjamin.Altma » Thu, 19 Apr 2001 05:28:01


How about this?

    ip=(`grep "^[     ]*[0-9]" $u_hostfile | while ip _;do echo $ip;
done`)

This will leave you with an array called ip with all the addresses in
it.
And all I can say after that is that after using ksh all the time, when
it comes to dealing with bash and how it runs pipes in a subshell it is
a headache.


> I have found some other little problems so I post
> structure of my file and hope someone can give me
> good script advice.

> file:
> 192.168.1.44        # hostname
> 192.168.1.56        # hostname
> 192.168.2.45        # hostname
> #192.168.3.222      # hostname (removed host)
> 192.168.3.123      # hostname
> ...and so on.

> How can I read the not "removed hosts" into an array
> of hosts called host[]?

> I really have no idea of doing it efficiently.


 
 
 

read file into array

Post by Benjamin.Altma » Thu, 19 Apr 2001 06:04:28


This will be fast::
        hosts=(`awk '/^[     ]*[0-9]/ {print $1}' $u_hostfile`)
where [     ] contains a tab and a space.  hosts is an array containing
all the hosts.

Ben


> I have found some other little problems so I post
> structure of my file and hope someone can give me
> good script advice.

> file:
> 192.168.1.44        # hostname
> 192.168.1.56        # hostname
> 192.168.2.45        # hostname
> #192.168.3.222      # hostname (removed host)
> 192.168.3.123      # hostname
> ...and so on.

> How can I read the not "removed hosts" into an array
> of hosts called host[]?

> I really have no idea of doing it efficiently.

 
 
 

read file into array

Post by Helmut Webe » Thu, 19 Apr 2001 06:39:34



> I have found some other little problems so I post
> structure of my file and hope someone can give me
> good script advice.

> file:
> 192.168.1.44        # hostname
> 192.168.1.56        # hostname
> 192.168.2.45        # hostname
> #192.168.3.222      # hostname (removed host)
> 192.168.3.123      # hostname
> ...and so on.

> How can I read the not "removed hosts" into an array
> of hosts called host[]?

> I really have no idea of doing it efficiently.

If your file always has a '#' respectively numbers on the beginng of the
line I would start like this.

#!/bin/sh
host=`awk '/^[^#]/ {print $1}' file`

or

#!/bin/sh
host=`egrep "^[0-9]" file | awk '{print $1}'`

both return

192.168.1.44
192.168.1.56
192.168.2.45
192.168.3.123

have fun
Helmut

 
 
 

1. reading files into an array

Hello,
        I'm new and have a basic question:  I would like to read the files present
in a directory into an arrary then step through the array, feeding each
file name to another program ( since this program accepts 1 input file at a
time).  I looked at AWK because of it's record/field abilities but don't
want to get that involved. Anyone know of a simpler way to accomplish
feeding files in a dir to a do-wile loop within a script or other approach
using bsh,ksh,csh or ?
TIA

2. VFAT access for other than root?

3. read/write array to file with csh (tcsh)

4. Linux FTP install via Wingate on 98

5. FORTRAN: READING BINARY FILES INTO ARRAYS

6. UNIX's Book about the internal system

7. reading file values into an array

8. Rainbow runner supported in Linux?

9. How to read an array from a file?

10. consistent read() on file while rename() file.new -> file?

11. input: sunkbd.c - fix reading beyond end of keycode array [14/14]

12. Sparc Storage Array: vxva in read only mode?

13. purify->array bounds write/read