Shell snip to import CSV data into BASH array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell snip to import CSV data into BASH array
# 1  
Old 01-05-2011
Shell snip to import CSV data into BASH array

I have been trying to write a simple snip of bash shell code to import from 1 to 100 records into a BASH array.

I have a CSV file that is structured like:

Code:
record1,item1,item2,item3,item4,etc.,etc. .... (<= 100 items)
record2,item1,item2,item3,item4,etc.,etc. .... (<= 100 items)
record3,item1,item2,item3,item4,etc.,etc. .... (<= 100 items)
record4,item1,item2,item3,item4,etc.,etc. .... (<= 100 items)
record5 ......
record6 ......
...... and so on

I am trying to get this data into corresponding arrays as such:

Code:
$record1[item1-n]
$record2[item1-n]
$record3[item1-n]
$record4[item1-n]

Here is what i have so far:

++++++++++++++++++++++++++++++
Code:
#!/bin/bash
testfile="./test-dev/test-global-data-import.csv"
var1=`cat $testfile`
IFS=',' record1=( ${var1} )

echo ""
echo "Here is the data without the comma separators."
echo ""
echo ${record1[*]:0}
echo ""
echo "Here is the data with comma separators."
echo ""
echo "${record1[*]:0}"
echo ""

#EOF

++++++++++++++++++++++++++++++

Here are the problems I am having:

1. I am trying to have each record (line in the CSV file) become its own array, such as $record1[*]

2. There will be lots of strange formatting within the CSV file (i.e. IP addresses, street addresses w/ commas and periods) and right now each record has items 1-35 and it could possibly grow to <=100 fields.

3. I am a newbie :-( .... I have edited BASH scripts in the past ... but that no way make me proficient .... so can someone please help.

P.S. I am pouring over several resources on a daily basis (Beginners & Advanced BASH guides on tldp.org and I have a BASH book that I have my nose buried in constantly. I still need some expert assistance though. Smilie

Any help is greatly appreciated. Smilie

Last edited by radoulov; 01-05-2011 at 04:04 PM.. Reason: Code tags, please!
# 2  
Old 01-05-2011
Could you try to explain what exactly do you want to achieve?
I won't recommend a pure shell for text manipulation.
This User Gave Thanks to radoulov For This Post:
# 3  
Old 01-05-2011
Okay, so here is the 10k fly by ....

I have one group of persons inputting data about sites into a spreadsheet and saving it as someinputdata.csv. Then, I have several template files with VAR_1, VAR_2, VAR_3, and so on declared. I am trying to assign the CSV data to array elements so I can pluck out the data I need with a sed statement like this ...

I really thought it was a simple problem to solve. One group of people is collecting data and one group will be mass producing network devices with the cfg files.

+SNIP+

# set the template variable
TEMPLATE="some text file with VAR_1 in it"

# code here to insert the CSV data into the array ....
..... MISSING ......

# set the variable to the array element
VAR_1=$record1[12]

#
cat $TEMPLATE | sed -e "s/VAR_1/$VAR_1/"g | tee ./output/$VAR_1.cfg >/dev/null 2>&1

+SNIP+
# 4  
Old 01-05-2011
Please post a sample of your input data and an example of the expected modified output after the substitution.

I'm not saying that reading an input record into a bash array is difficult,
I'm just saying that bash is not the right tool for parsing csv data.
# 5  
Old 01-05-2011
Quote:
Originally Posted by radoulov
Please post a sample of your input data and an example of the expected modified output after the substitution.

I'm not saying that reading an input record into a bash array is difficult,
I'm just saying that bash is not the right tool for parsing csv data.

Above in this post is the example of the input CSV data.

to re-iterate ....

I have a CSV file that is structured like:
Code:
record1,item1,item2,item3,item4,etc.,etc. .... (<= 100 items)
record2,item1,item2,item3,item4,etc.,etc. .... (<= 100 items)
record3,item1,item2,item3,item4,etc.,etc. .... (<= 100 items)
record4,item1,item2,item3,item4,etc.,etc. .... (<= 100 items)
record5 ......
record6 ......
...... and so on

I am trying to get this data into corresponding arrays as such:
Code:
$record1[item1-n]
$record2[item1-n]
$record3[item1-n]
$record4[item1-n]


Last edited by joeyg; 01-05-2011 at 04:48 PM.. Reason: cleaned up with CodeTags
# 6  
Old 01-05-2011
Yes,
I've already read your original post, I mean that we need a representative sample.
Do you have any values with embedded commas, for example?

By expected output I mean the final product, not the intermediate variables/arrays.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using bash script : How to Import data from a dsv file into multiple tables in mysql

HI I have a dsv file that looks like: <<BOF>> record_number|id_number|first name|last name|msisdn|network|points|card number|gender 312|9101011234011|Test Junior|Smith|071 123 4321|MTN|73|1241551413214444|M 313|9012023213011|Bob|Smith|27743334321|Vodacom|3|1231233232323244|M... (4 Replies)
Discussion started by: tera
4 Replies

2. Shell Programming and Scripting

Import 2 columns from 8 .csv files into pandas df (side by side) and write a new csv

I have 8 .csv files with 16 columns and "n" rows with no Header. I want to parse each of these .csv and get column and put the data into a new.csv. Once this is done, the new.csv should have 16 columns (2 from each input.csv) and "n" rows. Now, I want to just take the average of Column from... (3 Replies)
Discussion started by: Zam_1234
3 Replies

3. Shell Programming and Scripting

Shell script to extract data from csv file

Hi everyone, I have a csv file which has data with different heading and column names as below. Static Data Ingested ,,,,,,,,,,,,Known Explained Rejections Column_1,column_2,Column_3,Column_4,,Column_6,Column_7,,% Column_8,,Column_9 ,Column_10 ,... (14 Replies)
Discussion started by: Vivekit82
14 Replies

4. Shell Programming and Scripting

Array comparision in bash shell

I'm not sure if i can put the problem in understandable form.Let me try: I have a array which is like and always fixed: Array1=(new inprogress pending Restored Resolved ) Other 2 array array2 which may varry but always subset of above array: Like it can be : Array2=(New inprogress... (2 Replies)
Discussion started by: InduInduIndu
2 Replies

5. Shell Programming and Scripting

Converting variable space width data into CSV data in bash

Hi All, I was wondering how I can convert each line in an input file where fields are separated by variable width spaces into a CSV file. Below is the scenario what I am looking for. My Input data in inputfile.txt 19 15657 15685 Sr2dReader 107.88 105.51... (4 Replies)
Discussion started by: vharsha
4 Replies

6. Shell Programming and Scripting

Shell script to populate an array from a csv file

Hi Guys, I've got a script that runs and collects statistics from one of our machines and outputs the data into a csv file. Now, that script runs fine and I managed to create another one (with a lot of help from this forum!!) to trim the csv file down to all the data that I need, rather than all... (9 Replies)
Discussion started by: jimbob01
9 Replies

7. Programming

import .txt and split word into array C

Hi, if I want to import .txt file that contain information and the number separate by space how can I split and put into array In C Example of .txt file 3 Aqaba 49789 10000 5200 25.78 6987 148976 12941 15.78 99885 35262 2501 22.98 Thank (3 Replies)
Discussion started by: guidely
3 Replies

8. UNIX for Advanced & Expert Users

shell script to format .CSV data

Hi all, I have written a shell script to search a specified directory (e.g. /home/user) for a list of specific words (shown as ${TMPDIR}/wordlist below). The script works well enough, but I was wondering if there was a way to display the line number that the word is found on? Thanks! cat... (1 Reply)
Discussion started by: tmcmurtr
1 Replies

9. Shell Programming and Scripting

Shell script to format a .CSV data

Hi There I needed to write a Unix shell script which will pick up the data from a .CSV file and reformat it as per the requirement and write it to another .CSV file. Currently I am in the proess of Data Import to "Remedy System" (A one kind of incident mangement Application) and this... (8 Replies)
Discussion started by: Uday1982
8 Replies

10. Shell Programming and Scripting

merging CSV data using a one liner from shell?

I'm trying to merge multiple CSV (comma separated value) files into one large master file. All files have a field that is unique to act as the key for entry/merging into the master file & and all files have the same number of fields that are in the master file. I'll give an example here: ... (2 Replies)
Discussion started by: jjinca
2 Replies
Login or Register to Ask a Question