LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 05-26-2010, 09:46 AM   #1
idaham
LQ Newbie
 
Registered: Aug 2009
Posts: 26

Rep: Reputation: 0
Reading numbers from text file and storing in array


Dear all!

I'm writing a bash script where I read a text file (containing a column of numbers) and store each line in an array. There seem to be some problems with the whole thing however, but only for some files and not others. Here's what I do:
Code:
#!/bin/bash 
file=time_notOk.txt ### The file with a column of numbers
i=0 ### Array counter

### Read the file
exec 3<$file

### Loop over each line of the file and store in array
while read value <&3
do
	array[$i]=$value	
	echo test1 ${array[$i]} test2	
	i=$(($i+1))
done
Running this I get the echo output (from the loop):
Code:
 
 test20
 test21
 test22
 test23
 test24
 test25
 test26
...
Apparently something's wrong! When I read another file however, just the same but with other numbers in it, it works and I get the output as it should be:
Code:
test1 0 test2
test1 10 test2
test1 20 test2
test1 30 test2
test1 40 test2
test1 50 test2
test1 60 test2
...
I've attached the two text files. Is there something wrong with my input files or the code? Thanks!
Ida
Attached Files
File Type: txt time_notOk.txt (176 Bytes, 4 views)
File Type: txt time_ok.txt (153 Bytes, 3 views)
 
Old 05-26-2010, 10:06 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
time_notOk.txt has DOS/Windows line ends on all but the last line. These can be seen in vi or using the od command. Fix by, for example, using fromdos command.
 
1 members found this post helpful.
Old 05-27-2010, 01:50 AM   #3
idaham
LQ Newbie
 
Registered: Aug 2009
Posts: 26

Original Poster
Rep: Reputation: 0
Dear catkin,
Thank you so much for your reply! You're a lifesaver! I ran the dos2unix (fromdos) command and converted my files and now it's working properly... Thank you!
Ida

Quote:
Originally Posted by catkin View Post
time_notOk.txt has DOS/Windows line ends on all but the last line. These can be seen in vi or using the od command. Fix by, for example, using fromdos command.
 
Old 05-27-2010, 03:36 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Glad it solved the problem for you

Threads can be marked SOLVED via the Thread Tools menu.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script:- Reading numbers embedded in brackets from a text file rsan Linux - Newbie 6 07-05-2009 06:01 AM
Reading a CSV text file and storing the values in Oracle Database table shafi2all Linux - Newbie 3 04-17-2008 12:19 PM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
storing text in a text file for a specified length of time. mrobertson Programming 7 08-02-2005 10:27 AM


All times are GMT -5. The time now is 09:20 AM.

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