Read csv file in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read csv file in bash
# 1  
Old 03-02-2013
Read csv file in bash

how to I use IFS to read 2 files (csv) and run the followiung script


Code:
./naviseccli -h 1.2.3.4 storagegroup -addhlu -gname $hostname -hlu $hlu_num -alu $alu_num


the csv file for $hostname is

Code:
host1
host2
.
.
.


for hlu and alu

its

Code:
alu,hlu
1,100
2,200
3,300
.
.
.

maybe I can do a while loop to read both files?

Code:
while IFS=, read $hostname
while IFS=, read alu hlu
do

./naviseccli -h 1.2.3.4 storagegroup -addhlu -gname $hostname -hlu $hlu -alu $alu
done < file1.csv
done < file2.csv


any help appreciated

thanks

Last edited by Don Cragun; 02-15-2014 at 10:57 PM.. Reason: Add CODE tags.
# 2  
Old 03-02-2013
Code:
#!/bin/bash

while read hostname                                                                                     # No IFS required if just one column: hostname
do

        while IFS="," read alu hlu                                                                      # Set IFS ro read alu & hlu
        do
                [[ "$alu" =~ ^[a-zA-Z]+$ ]] && continue                                                 # To skip header alu, hlu

                ./naviseccli -h 1.2.3.4 storagegroup -addhlu -gname "$hostname" -hlu $hlu -alu $alu

        done < file1.csv

done < file2.csv

# 3  
Old 03-03-2013
Please use code tags as required by forum rules!

For efficiency it might be wise to reverse the sequence of files read, so as to skip file1's header only once.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

2. Shell Programming and Scripting

Not able to read .csv file until open in vi editor

Hi, I am facing a problem regarding .csv file, my script does not read .csv file and if i open this file in vi editor and perform :wq option then only my script reads the .csv file. Thanks (5 Replies)
Discussion started by: ranabhavish
5 Replies

3. Shell Programming and Scripting

Read .csv file

Hello, this is my very first comment on this forum and i hope i don't mess it up. If i do, please forgive me (also for any language mistakes you may wanna know that i'm not native speaking). As i just started doing a bigger paper for my studies i got a bunch of data in seperate csv-files... (1 Reply)
Discussion started by: FabianDe
1 Replies

4. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

5. UNIX for Dummies Questions & Answers

skip first line when doing a read of csv file

Folks, how do i skip the first line in a csv, while doing the read of a csv file in to a variable line by line. eg : do echo $line done < $rpt where rpt is path to csv file The initial 1st line is a garbage that i want to avoid, and start reading from 2nd line ... (2 Replies)
Discussion started by: venu
2 Replies

6. Shell Programming and Scripting

read .csv file

Need UNIX script read below .csv file and print the line only records where type = TRN. Srno,Type,InputFileName,NewColumnData 1,TRN,File1.dat,11 2,TRN,File2.dat,12 3,TRN,File3.dat,13 4,REF,File4.dat, 5,REF,File5.dat, regards, santosh (4 Replies)
Discussion started by: santosh2k2
4 Replies

7. Shell Programming and Scripting

How can I read values from a CSV file using Shell?

SHELL SCRIPT Hi, I have a file in which contents are as follows: 9999,abdc,123 9988,aba_12,2323 and so on I want to read the contents of this file such that i can do echo "This is $a followed by $b an then $c" I tried the following but id did not work cat test | cut -d ',' -f1|... (7 Replies)
Discussion started by: mayanksargoch
7 Replies

8. Shell Programming and Scripting

Read Two lines in a CSV File and Compare

Hi , I have a CSV file ( file.csv) with some data as below: A,1,abc,x,y,z,,xyz,20100101,99991231 A,1,abc,x,y,z,234,xyz,20100101,99991231 I have to delete the duplicate line based on unique identifiers which are values in the fields- 2,3,4,8.These coulmns in both the rows have same... (6 Replies)
Discussion started by: Sheel
6 Replies

9. Shell Programming and Scripting

Read Csv file

Hi All, I need to check if a csv file is empty, leaving the first line as the first line is header pls help Thanks (3 Replies)
Discussion started by: gwrm
3 Replies

10. Shell Programming and Scripting

How to read and parse the content of csv file containing # as delimeter into fields using Bash?

#!/bin/bash i=0 cat 1.csv | while read fileline do echo "$fileline" IFS="#" flds=( $fileline ) nrofflds=${#flds} echo "noof fields$nrofflds" fld=0 while do echo "noof counter$fld" echo "$nrofflds" #fld1="${flds}" trying to store the content of line to fields but i... (4 Replies)
Discussion started by: barani75
4 Replies
Login or Register to Ask a Question