Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.shell > #4868

Trying to read values from a file into arrays (bash)

From deltaquattro@gmail.com
Newsgroups comp.unix.shell
Subject Trying to read values from a file into arrays (bash)
Date 2012-04-30 16:42 -0700
Organization http://groups.google.com
Message-ID <25106263.1541.1335829369694.JavaMail.geo-discussion-forums@vbqq1> (permalink)

Show all headers | View raw


Hi,

I'm trying to read the columns in the file foobar.fo:

073 1.819
085 2.187
100 2.439
115 2.879

into the array variables dir and mflow. However, I'm having some difficulties:

#!bin/bash

i=1
while read inputline
do 
   dir[i]=($(echo $inputline | cut -f1); mflow[i]=($(echo $inputline | cut -f2);
   ((i++));
done < foobar.fo
ndirs=i
echo ${dir[@]} ${mflow[@]}

exit 0

doesn't work. If I change the body of the while loop to:

   temp1=($(echo $inputline | cut -f1); temp2=($(echo $inputline | cut -f2);
   dir[i]=$temp1; mflow[i]=$temp2;
   ((i++));

everything runs fine. Why? Also, I'd prefer to use awk instead than cut, but I'm not sure how I can run awk on foobar.fo, get an output and store it into dir and mflow, while in a bash script. Can you show me how to do it? Finally, is there a way to read directly the whole columns into dir and mflow, instead than going through a while loop? Thanks,

Best Regards

Sergio Rossi




Back to comp.unix.shell | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Trying to read values from a file into arrays (bash) deltaquattro@gmail.com - 2012-04-30 16:42 -0700
  Re: Trying to read values from a file into arrays (bash) Janis Papanagnou <janis_papanagnou@hotmail.com> - 2012-05-01 08:44 +0200
    Re: Trying to read values from a file into arrays (bash) deltaquattro@gmail.com - 2012-05-02 08:48 -0700
      Re: Trying to read values from a file into arrays (bash) Barry Margolin <barmar@alum.mit.edu> - 2012-05-02 12:15 -0400
        Re: Trying to read values from a file into arrays (bash) deltaquattro@gmail.com - 2012-05-04 01:49 -0700
          Re: Trying to read values from a file into arrays (bash) Barry Margolin <barmar@alum.mit.edu> - 2012-05-04 10:12 -0400
      Re: Trying to read values from a file into arrays (bash) Janis Papanagnou <janis_papanagnou@hotmail.com> - 2012-05-02 18:36 +0200
        Re: Trying to read values from a file into arrays (bash) Janis Papanagnou <janis_papanagnou@hotmail.com> - 2012-05-02 18:48 +0200
          Re: Trying to read values from a file into arrays (bash) deltaquattro@gmail.com - 2012-05-04 02:10 -0700
  Re: Trying to read values from a file into arrays (bash) "Chris F.A. Johnson" <cfajohnson@gmail.com> - 2012-05-02 20:44 -0400
    Re: Trying to read values from a file into arrays (bash) deltaquattro@gmail.com - 2012-05-04 02:05 -0700

csiph-web