Find the answer to your Linux question:
Results 1 to 6 of 6
How do i store each line of a file into a variable. I know how to read in each line and then print how many lines their are. So now ...
  1. #1
    Just Joined!
    Join Date
    Sep 2005
    Posts
    99

    files and arrays

    How do i store each line of a file into a variable. I know how to read in each line and then print how many lines their are. So now how to i make that instead of just print the number of lines take each line and store it in a variable? Then i want to break up what is stored in each variable because each variable will store 2 columns of numbers and store one column in one array and the other column in another array.
    Code:
    a=0
    while read line
    do a=$(($a+1));
    echo $a;
    done < "myfile"
    echo "Final line count is: $a";

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    You have already done so in your code snippet; see http://www.tldp.org/LDP/Bash-Beginne...ect_08_02.html and bookmark, look at the index, etc., to answer other similar questions ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined!
    Join Date
    Sep 2005
    Posts
    99
    Ok, I'm an idiot. line is the variable. So now i figured out how to get the values in the arrays. Now i have to find the avg of the values in the array. How do i call an array. I keep getting a syntax error teletype. Is it $arrayName[$index]? I don't think that works. Or is it just arrayName[$index]? I tried that too. basically i have a sum variable and then i have to add sum to arrayName[$index] and then store it back in sum. Then i take that divide by the size of the array to get the average but i keep getting the teletype error. Here is the part of the code to find the average. Anyone know why i am getting this error?
    Code:
    index=1
      #find x average
      while [ "$index" -lt "$sizeX" ]
      do
        tempX=$xValues[$index]
        xSum=$(echo "scale=5 ; $xSum + $tempX" | bc)
        (( index++ ))
      done
      xAvg=$(echo "scale=5 ; $xSum / $sizeX" | bc)
      echo "the x average is " $xAvg
    
      index=1
      #find y average
      while [ "$index" -lt "$sizeY" ]
      do
        ySum=$(echo "scale=5 ; ($ySum + $yValues[$index])" | bc)
        (( index++ ))
      done
      yAvg=$(echo "scale=5 ; ($ySum / $sizeY)" | bc)
      echo "the y average is " $yAvg

  4. #4
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    ${array_name[element]}

    Is how to reference an array's element. The size of the array is ${#array_name[*]}

    You're referencing your arrays all wrong, and you've got some other small errors in there.

  5. #5
    Just Joined!
    Join Date
    Sep 2005
    Posts
    99
    o yea, my fault. I'll go back and change how i reference the arrays. What other small errors are there?

  6. #6
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    NM I read it quick and thought I saw a couple, but I neglected to notice for instance the pipe to bc in that one variable assignment. Give it a go once you fix your array references, I think it will work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...