Place the contents of a .CSV file to an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Place the contents of a .CSV file to an array
# 1  
Old 08-23-2012
Place the contents of a .CSV file to an array

Hi,
I am trying to place the contents of a .CSV file to an array, but not sure how to do that.

Here is my .CSV file content:
Code:
App,SLA,Job name,Avg start time,Avg run time,Frequency,Downstream apps
XYZ,,ABC14345,3:00 AM,00.04.00,Daily,STAMP
XYZ,9:00,ABC12345,3:15 AM,00.05.00,Daily,STAMP

Here's my code that I am trying:
Code:
#!/bin/ksh
conf=Jobs_template_track3.csv
Appl=""
st_dt=""
end_dt=""
status=""
job_start_date=""
job_start_time=""
echo "\nAPPLICATION |SLA |Start_Time|Avg Start time| End_Time|Total_Runtime|Status"
echo "----------------------------------------------------------------------------------------------------"
i=1;
exec<$conf
while read job
do
 if [[ "$i" -eq 1 ]]
 then
  ((i+=1))
  continue;
 fi
        set -A Arr $job
        appl=${Arr[0]}
        if [ -z "$appl" ]
        then 
         continue;
        fi
        avg_st_time=${Arr[3]}
SLA=${Arr[1]}
job1=${Arr[2]}
echo $job1
#job2=${Arr[2]}

This is not the complete code, but got stuck in the first thing, placing the contents in an array. Please let me know on how to place the contents.


Thanks
Ajay
# 2  
Old 08-23-2012
You can change the "IFS - Internal Field Separator", for example:
Code:
oIFS=$IFS  ## Save current setting
IFS=","
while read job
do
  set -A Arr $t
  IFS=$oIFS
  echo "${Arr[*]}"
  IFS=","
done < job
App SLA Job name Avg start time Avg run time Frequency Downstream apps
XYZ  ABC14345 3:00 AM 00.04.00 Daily STAMP
XYZ 9:00 ABC12345 3:15 AM 00.05.00 Daily STAMP

This User Gave Thanks to spacebar For This Post:
# 3  
Old 08-23-2012
Do you actually need an array? The shell has several ways to solve that kind of thing:

Code:
IFS=","
exec <file
read G # Ignore first line

while read LINE
do
        set -- $LINE
        echo "First column is $1"
        echo "Second column is $2"
        echo "And so forth"
done

which allows you to write code which will work in any shell.
# 4  
Old 08-23-2012
Or even:

Code:
exec <file
while IFS="," read APP SLA JOBNAME AVGSTART AVGRUN FREQ DOWNSTREAM
do
        echo "First column is $APP"
        echo "Second column is $SLA"
        echo "And so forth"
done

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-24-2012
Thanks for the replies. They are working.

---------- Post updated at 12:51 PM ---------- Previous update was at 10:35 AM ----------

Now I have my data in variables.. Like duration, volume, start time and end time..
How do I place the contents in a .csv file, so that I can mail them as attachment ?
Used the below code:
Code:
printf "%12s|%8s|%8s|%11s|%13s|%10s|%8s|\n" $appl $SLA $job1 $st_tm $avg_st_time $en_tm $status  >> aj.csv
cat aj.csv|mailx -s "Track3 report" "$addr"
#uuencode aj.csv aj.csv|mailx -s "Track3 report" "$addr"

But the formatting doesnt look good and the uuencode is also not working. Getting some corrupted values instead of attachment.

Thanks
Ajay

---------- Post updated at 05:40 PM ---------- Previous update was at 12:51 PM ----------

Got it.
Used
Code:
 echo "$appl"",""status"",""$job" >> aj.csv

and mailx -m is able to send the attachment correctly.

Thanks
Ajay
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to place specific contents filename within text file

I am trying to use awk to place the contens of a filename in $1 and $2 followed by the data in the text file. Basically, put the filename within the text file. There are over 1000 files in the directory and as of now each file is saved with a unique name but it is not within the file. Thank you... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. UNIX for Dummies Questions & Answers

Trying To Write File Contents To Specfic .csv Cell

Hi, I'm attempting to write the entire contents of a file to a specific .csv cell. So far have only a nawk one liner that will write a value into a specific .csv cell. Trying to use man page but can't seem to get any farther. Any help would be appreciated. nawk -v r=2 -v c=3 -v val=5 -F,... (7 Replies)
Discussion started by: jimmyf
7 Replies

3. Shell Programming and Scripting

Write array contents to file

Hi, I have a bash script that currently holds some data. I am trying to write all the contents to a file called temp.txt. I am using echo ${array} > temp.txt The problem that I am experiencing is that the elements are being written horizontally in the file. I want them written... (5 Replies)
Discussion started by: Filter500
5 Replies

4. Shell Programming and Scripting

Issues using array credentials to read contents of a file

Hi, I am trying to read the contents of a file using array credentials in unix. The file I am trying to read is tab separated and contains the below contents. # partnerid Direc Server Port Source_Dir Target_Dir Mask Remove Files Passwordless Compare Files ... (3 Replies)
Discussion started by: aartikara
3 Replies

5. Shell Programming and Scripting

Store table contents in csv file

I need to write a script to store the contents of a table in a csv file I'm using Toad, it's a Oracle database. (5 Replies)
Discussion started by: ladyAnne
5 Replies

6. Shell Programming and Scripting

changing csv file contents to file of rows

i have a file a.txt contents as 1,2,3,4,......etc...in a single line, i want to write to another file in rows as 1 2 3 4 5 can u help? i do not know the length of a.txt (4 Replies)
Discussion started by: pravfraz
4 Replies

7. Shell Programming and Scripting

Most reliable way to store file contents in an array in bash

Hi Guys, I have a file which has numbers in it separated by newlines as follows: 1.113 1.456 0.556 0.021 -0.541 -0.444 I am using the following code to store these in an array in bash: FILE14=data.txt ARRAY14=(`awk '{print}' $FILE14`) (6 Replies)
Discussion started by: npatwardhan
6 Replies

8. Shell Programming and Scripting

Shell script for converting file contents into CSV

Hi, I am new in unix, I just want to replace some values from text file according to column numbers. Like, I am having a table as given below: val1 val2 val3 val4 val5 val6 val7 val8 val9 val10 val11 val12 val13 Now i want... (5 Replies)
Discussion started by: rish_max
5 Replies

9. UNIX for Dummies Questions & Answers

compare array contents with file

I have an array "arrA" with the following contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf My data file "Files.dat" has the same contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf I have a script that compares... (0 Replies)
Discussion started by: orahi001
0 Replies

10. Shell Programming and Scripting

Can I assign the contents of file into an array?

I am trying to assign the contents of file e.g ls "$HOME" into an array. If it is possible then please guide me without using the concept of awk,sed, and perl16 Thanks (10 Replies)
Discussion started by: murtaza
10 Replies
Login or Register to Ask a Question