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
# 1  
Old 02-09-2012
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 the data it collects.

Anyway, I'm now trying to write a script to import this data from the csv file into Nagios so it can report the statistics. The easiest way I've heard of to do this is to create a script which involves populating an array from the csv file and then passing the contents of the array into Nagios (I can modify an existing script for that).

My knowledge on scripting is a bit limited and I certainly haven't created one involving passing a csv file into an array!! Does anyone have any pointers on how this can be done ? This is an example of the data in the csv file:
Code:
startup timestamp,1327702381482
cpu combined,0.0900
cpu idle,0.9100
cpu nice,0.0000
cpu sys,0.0360
cpu user,0.0540
cpu wait,0.0010
memory free,4415.501335 MB
memory max,4494.937500 MB
memory used,79.436165 MB

As you can see, there isn't a lot of data to insert into the array and I could possibly do without the first column as I can enter this in the Nagios script. Or, does anyone think there is a better way of doing this !?

Thanks for any help!

Cheers, Jim
# 2  
Old 02-09-2012
What's your system?

What's your shell?

How do you want the contents of the array to be arranged?
# 3  
Old 02-09-2012
Hi Corona,

The system its running on is Red Hat Enterprise Linux 6, and bash shell. As for the contents of the array, ideally it would just be arranged in the order that is in the csv file if that is possible?


Cheers, Jim
# 4  
Old 02-09-2012
Code:
#!/bin/bash

N=0
ARR=()

IFS=","

while read STR
do
        set -- "$STR"

        while [ "$#" -gt 0 ]
        do
                ARR[$N]="$1"
                ((N++))
                shift
        done
done < input.csv

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-09-2012
Corona688, you sir, are a genius!!

Thats exactly the kind of thing I've been looking for! How would I test it to see if, say, what is contained at number 5 in the array ? I just tried to add 'echo $ARR[2]' etc, but it always just outputs the first record in the array (and adds [2] to the end!).

It's been a long day and I'm getting confused . . . . . it doesn't take much though! Smilie
# 6  
Old 02-09-2012
You need the {} brackets.

Code:
echo ${ARR[2]}

---------- Post updated at 04:45 PM ---------- Previous update was at 04:42 PM ----------

I think I made an error in my original code.

Code:
set -- $STR

not
Code:
set -- "$STR"

The quotes will prevent it from splitting.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-09-2012
Thanks Corona688 - honestly, your help has been invaluable with my problems. Can't thank you enough.

Just hope that one day, I'll be able to help out on these boards like you do. Now, to get the books out and study your script so I know each thing its doing, one step at a time so I can learn this.

Thanks again!

Cheers, Jim
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