Reading from a loop - Aix

This is a discussion on Reading from a loop - Aix ; Hi, I have a file as folows. 2400000 2399680 1896288 503392 And i am trying to read the numbers from this file and perform the required calculations. I mean i want to divide (503392/2400000). How do i do that? Thanks ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Reading from a loop

  1. Reading from a loop

    Hi,

    I have a file as folows.

    2400000
    2399680
    1896288
    503392

    And i am trying to read the numbers from this file and perform the
    required calculations. I mean i want to divide (503392/2400000). How do
    i do that?

    Thanks in advance,

    Krish


  2. Re: Reading from a loop

    ksh, ksh93, bash awk or perl ? or doesnt it matter

    There are many ways to acheive this .. you could drop the contents of
    the file onto one line by deleting the at the end of each line and
    read the variables on one line as var1 var2 var3 var4 restofline ..

    This is assuming that you only want the 1st 4 lines as vars and the
    rest of the line is dropped into the restofline var

    i.e.

    cat file.txt | tr "\012" "\040" | read var1 var2 var3 var4 restofline

    echo "var1 == $var1"
    echo "var2 == $var2"
    blah blah ..

    Another way, would be to read the lines into an array in a loop ..

    #!/bin/ksh93
    count=1
    cat file | while read line
    do
    array[${count}]=$line
    (( count = count + 1 ))
    done

    echo "array[1] == ${array[1]}"
    echo "array[2] == ${array[2]}"
    blah blah ..

    IIRC you will be limited to 1024 elements in the array though, so for
    files wil more than 1024 lines this wont work

    Its more efficient to do this in awk or perl .. but the above will do
    for small amounts of data ..

    HTH
    Mark Taylor


    chaitu wrote:
    > Hi,
    >
    > I have a file as folows.
    >
    > 2400000
    > 2399680
    > 1896288
    > 503392
    >
    > And i am trying to read the numbers from this file and perform the
    > required calculations. I mean i want to divide (503392/2400000). How do
    > i do that?
    >
    > Thanks in advance,
    >
    > Krish



+ Reply to Thread