Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-06-2010, 09:02 PM   #1
LQ Newbie
Registered: Nov 2004
Posts: 5

Rep: Reputation: 0
Bash - Reading csv delimited file to array and for further manipulation

I am trying to do this:
1. Read csv delimited file line1 and store all values in array
2. Use the values stored in the array and replace values in other text file with them
3. read line2 in the cvs file and repeat the process
4. Do above for all lines in the cvs file

for example:
file1.cvs content:
text1,text2,text3,"text 4"
a1,a2,3,"a 4"

file.txt content:
some text $array1$ some text
some text $array2$ some text

1. read line 1 - text1,text2,text3,"text 4"
put each value in array X[]
lines that contain spaces in cvs will have double quotes

2. read x[1] and replace value $array1$ (in file.txt) with x[1]
read x[2] and replace value $array2$ (in file.txt) with x[2] and so on

Can above be accomplished in BASH and how?
Old 01-06-2010, 10:38 PM   #2
LQ Newbie
Registered: Nov 2004
Posts: 5

Original Poster
Rep: Reputation: 0
I created below script:

while IFS=, read ext password name
echo "ext -> [${ext}]"
echo "password -> [${password}]"
echo "name -> [${name}]"

rm -f filetemp
cp basefile filetemp

sed -i 's/#ext#/'"${ext}"'/g' filetemp
sed -i 's/#password#/'"${password}"'/g' filetemp
sed -i 's/#name#/'"${name}"'/g' filetemp

mv filetemp "${ext}filetemp"

done < input

anyone can think of something better?


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
bash script using sed/scp/ssh has issues with delimited file ScottThornley Programming 5 03-18-2009 04:45 PM
Manipulating comma delimited CSV - newbie help with sed etc jonnymorris Programming 16 09-19-2008 07:14 AM
Reading a CSV text file and storing the values in Oracle Database table shafi2all Linux - Newbie 3 04-17-2008 01:19 PM
.csv file upoload, manipulation, download script help donv2 Programming 6 12-19-2007 04:20 PM
Reading crontab into bash array nutthick Programming 2 05-22-2006 01:44 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 PM.

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