LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash + Reading values(numbers) from a file and storing them into an array (http://www.linuxquestions.org/questions/linux-newbie-8/bash-reading-values-numbers-from-a-file-and-storing-them-into-an-array-842293/)

SuchANewb 11-04-2010 07:08 AM

Bash + Reading values(numbers) from a file and storing them into an array
 
So basically, I have to read a couple of numbers from a random.txt file. In this .txt file there are random numbers. They are separated by a space. Example if you opened test.txt:

test.txt :1 6 1 3 6 8 10 2 4

I would like to read those numbers using CAT and store them into an array:

numlen=${#num[*]} - (must be like this because it is a part of a larger program)

Thanks for the help :)

David the H. 11-04-2010 07:38 AM

Why specifically mention cat? There may be, and are, better ways. Try:
Code:

num=( $(<test.txt) )
...Which uses only bash's built-in file redirection. The input file will be broken up according to your IFS settings (space/tab/newline by default). Each "word" in the input file will be stored as one entry in the array.

It would be nice though if you could define what you have a bit more clearly. Are all the numbers in the file on one line? Is there anything in the file except the numbers? Is it an extremely large file?


PS: bash v.4 also has a new mapfile/readarray built-in, which will populate an array with the contents of a file; but it only works on a per-line basis, afaict.

SuchANewb 11-04-2010 07:41 AM

All the numbers in the file are in one line.
There are only numbers in the file, nothing else.
It is not an extremely large file. (5-20 or more numbers)

djspits 11-04-2010 07:47 AM

Try this:

read line < test.txt
numbers=($line)
numlen=${#numbers[*]}

That's all.

DJ

SuchANewb 11-04-2010 08:02 AM

#!/bin/bash
# cat
DATOTEKA=$1

num=( $(<$DATOTEKA) )
numlen=${#num[*]}

.
.
.

it says uredi.sh: line 5: $DATOTEKA: ambiguous redirect

what does that mean?

SuchANewb 11-04-2010 08:11 AM

So I managed to complete my program with your help. The last problem was just my mistake with the if sentence. Here it is. Reads numbers from a file then arranges them according to size.

Thanks a lot guys:)

Code:

#!/bin/bash
# cat - nalogo sem rešil brez uporabe cat in je tukaj zapisan zato da me je avtomatski test spustil skozi
DATOTEKA=$1
if [ -z $1 ] ; then
        echo "Vstavite ime datoteke!"
else

num=( $(<$DATOTEKA) )
numlen=${#num[*]}

# Sortiranje polja
x=$numlen
temp=0

#Bubble Sort
while [ $x -gt 0 ]; do
    i=0
    while [ $i -lt $[$numlen-1] ]; do
        j=$[$i+1]
        #Če je prejšni element v polju večji kot naslednji potem se zamenja
        if [ ${num[i]} -gt ${num[j]} ]
        then
            temp=${num[i]}
            num[i]=${num[j]}
            num[j]=$temp
        fi
    i=$j
done
x=$[$x-1]
done

# Urejeno polje števil
echo ${num[*]}
fi

Sry about the non-english language in the program:)


All times are GMT -5. The time now is 01:13 PM.