Shell script to populate an array from a csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to populate an array from a csv file
# 8  
Old 02-09-2012
I'll break it down, then.

Just initialization
Code:
N=0
ARR=()
# Special variable which controls when unquoted variables split.
# By default it's space, but we want to split on , for the csv
IFS=","

Code:
# Read the entire file one line at a time, putting each line in STR
while read STR
do
...
done <file.csv

Code:
# set -- a b c will set $1="a", $2="b", $3="c"
# If we set IFS=",", and VAR="a,b,c", then do set -- $VAR, it will
# set $1="a", $2="b", $3="c"
# So we use it here to split a line of "a,b,c" into $1, $2, $3.
set -- $STR

# $# is a special variable defining how many $1 variables we have.
# If we have $1, $2, $3, $# will be 3.  We loop until they're all gone.
while [ $# -gt 0 ]
do
        ARR[$N]=$1 # Put it in the array at index N
        ((N++)) # Increment N
        shift # Remove $1, set $1=$2, $2=$1, ...
done

This User Gave Thanks to Corona688 For This Post:
# 9  
Old 02-09-2012
Corona688, there was no need to do that for me, it's really, really appreciated but there was no need for you to go to that much trouble!!

I just can't thank you enough.

Cheers, Jim.
# 10  
Old 02-24-2012
Thanks it resolve my problem. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

Hello Unix Shell Script Experts, I have a script that would mask the columns in .csv file or .txt file. First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder. Two parameters are passed ... (5 Replies)
Discussion started by: Mahesh G
5 Replies

2. Shell Programming and Scripting

CSV File Creation Within Shell Script

Hi All, I am trying to create a CSV file within a shell script test.ksh and the code snippet is something like below: #!/usr/bin/ksh # Set required variables. . $HOME/.prof # Output file path Group1=/tmp/G1.csv Group2=/tmp/G2.csv Group3=/tmp/G3.csv $ORACLE_HOME/bin/sqlplus -s... (2 Replies)
Discussion started by: swasid
2 Replies

3. Shell Programming and Scripting

Reading a csv file using shell script

Hello All, I have a csv file that looks like below ProdId_A,3.3.3,some text,some/text,sometext_1.2.3 ProdId_B,3.3.3,some text,some/text,sometext_1.2.3 ProdId_C,3.3.3,some text,some/text,sometext_1.2.3 ProdId_A,6.6.6,some text,some/text,sometext_9.9.9 I will get ProdId from... (5 Replies)
Discussion started by: anand.shah
5 Replies

4. UNIX for Dummies Questions & Answers

Help to parse csv file with shell script

Hello ! I am very aware that this is not the first time this question is asked here, because I have already read a lot of previous answers, but none of them worked, so... As said in the title, I want to read a csv file with a bash script. Here is a sample of the file: ... (4 Replies)
Discussion started by: Grhyll
4 Replies

5. Shell Programming and Scripting

Using eval to populate an array in the background

Hi. I am trying to populate an array using eval in the background within a function. So this function will create a list of 3 character directories from SVN and store in an array. I want to call this function when the script starts in the background unknown to the user. The problem is that... (2 Replies)
Discussion started by: jmiaebrown
2 Replies

6. Shell Programming and Scripting

populate specified value in csv

Hi, I've requirement of populating two blank column ( 4rth and 6th ) in a csv file with specified value for all the rows having value within the first colmn. I know it can be done with sed, as given in the below command, but i am not sure how pull blank value or specify target column etc or... (11 Replies)
Discussion started by: john_prince
11 Replies

7. Shell Programming and Scripting

how to create csv file using shell script

I have a file in multiple directory which has some records in the following format File: a/latest.txt , b/latest.txt, c/latest.txt -> Name=Jhon Age=27 Gender=M Street=LA Road Occupation=Service I want to generate a csv file from the above file as follows File: output.csv -> ... (9 Replies)
Discussion started by: rjk2504
9 Replies

8. UNIX for Advanced & Expert Users

format csv file using shell script

i have a report.csv file from oracle datavase In that file data is like this with report heading and date SALES DAILY REPORT DATE: 06-26-2007 REPORT NAME: SALES DATA AA.BB.CCCC.DDDD,BBBBB,06-26-2007,0,BEGIN,END ZZ.VV.DDDD.XXXXXXX,MMMMMM,06-25-2007,18,BEGIN,END... (3 Replies)
Discussion started by: raosurya
3 Replies

9. Shell Programming and Scripting

Modifying a csv file from Shell Script

Hi all, I have some script that creates a temp csv file. What I need to do is do some search and replace and modify the file from my shell script. I know the commands to open the file and then apply the reg ex but wasnt sure how I could do this from a script and modify the file? Any help... (2 Replies)
Discussion started by: not4google
2 Replies

10. Shell Programming and Scripting

using the getline to populate an array from a file

Hello, I am trying to use the awk getline command to populate an array from a flat file. Has anyone done this before? So we have file1: a b c d I want to create an array like index that contains these four elements (8 Replies)
Discussion started by: penfold
8 Replies
Login or Register to Ask a Question