LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2010, 01:53 PM   #1
daberkow
LQ Newbie
 
Registered: May 2009
Posts: 11

Rep: Reputation: 0
bash read csv file


I have a comma separated file (file.txt) that is a list of numbers like:

20.0, 50.0, 80.1, 10.0, 30.0

I'd like to read this file and store the numbers in an array in order to loop through with corresponding items from another array-

command ${other_array[5]} ${csv_array[5]}

I don't have to use arrays if there is an easier/better way, but the number of items in the 'read' list matches the number of items in the other list and I just need to match them up in order.

My problem is really just reading and getting rid of the commas. I'm not sure the best way to read in and then what to do... maybe read then awk but it seems this should be easy.
 
Old 04-27-2010, 02:23 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yes, awk could do it, but it can be handled by bash alone if you change the IFS to include a comma.
Code:
oldIFS=$IFS
IFS=,$IFS
list='20.0, 50.0, 80.1, 10.0, 30.0' #or list=$(<file.txt)

myarray=( $list )

for n in ${!myarray[@]}; do
     echo ${myarray[$n]}
done

IFS=$oldIFS

#output:

20.0
50.0
80.1
10.0
30.0
BTW, I included the loop simply to demonstrate this neat feature I discovered recently. ${!array[@]} will list the current index numbers of an array.

Edit: Actually, after reading again, you could also simply use tr or sed to remove the commas (or change them to spaces) before processing the file.

Or yet another option, which also removes the commas:
Code:
list='20.0, 50.0, 80.1, 10.0, 30.0'

myarray=( ${list//,} )

Last edited by David the H.; 04-27-2010 at 02:39 PM. Reason: addendum
 
1 members found this post helpful.
Old 04-27-2010, 02:38 PM   #3
daberkow
LQ Newbie
 
Registered: May 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks, that's nice.

However, the $list should come from a text file I need to read in, it is very long and the values are generated by another program and can change often. I'm not sure how to do the while/read part and still break up the line.


Ok, this seems to do what I need:
Code:
list=$( cat $1 )

myarray=( ${list//,} )

Last edited by daberkow; 04-27-2010 at 02:47 PM.
 
Old 04-27-2010, 06:48 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,600

Rep: Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926
Or
Code:
list=($(awk 'gsub(/'/," ")' $1))

or

list=($(sed 's/,/ /g' $1))
 
Old 04-28-2010, 03:13 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by daberkow View Post
... the $list should come from a text file I need to read in, it is very long ...
Is it a single line file or a multi-line file?
 
  


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
CSV file - Duplicate records need merging | BASH? lmedland Programming 21 12-10-2010 05:01 AM
Update csv file into mysql db via bash script xmrkite Linux - Software 4 01-13-2010 12:43 AM
[SOLVED] Need help create a bash script to edit CSV File imkornhulio Programming 13 02-05-2009 10:23 AM
C++ read csv file row into vector taban1 Programming 3 11-08-2004 02:01 PM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration