LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-04-2010, 08:08 AM   #1
SuchANewb
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Rep: Reputation: 0
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
 
Old 11-04-2010, 08:38 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.
 
Old 11-04-2010, 08:41 AM   #3
SuchANewb
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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)
 
Old 11-04-2010, 08:47 AM   #4
djspits
LQ Newbie
 
Registered: Oct 2009
Location: The Hague, The Netherlands
Distribution: Ubuntu 10.04 LTS
Posts: 15

Rep: Reputation: 0
Try this:

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

That's all.

DJ
 
Old 11-04-2010, 09:02 AM   #5
SuchANewb
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 0
#!/bin/bash
# cat
DATOTEKA=$1

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

.
.
.

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

what does that mean?
 
Old 11-04-2010, 09:11 AM   #6
SuchANewb
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Reading numbers from text file and storing in array idaham Linux - General 3 05-27-2010 04:36 AM
storing values in an array in perl script :) kdelover Programming 5 09-16-2009 07:01 PM
Reading a CSV text file and storing the values in Oracle Database table shafi2all Linux - Newbie 3 04-17-2008 01:19 PM
bash; reading values from a file km4hr Programming 16 07-28-2005 03:07 PM


All times are GMT -5. The time now is 07:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration