Getting variables out of .csv lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting variables out of .csv lines
# 1  
Old 09-26-2003
Getting variables out of .csv lines

I have a csv file looking like:

echo,w-cai,w-cai-ssl
echo,countrywide,countrywide-ssl
haystack,intranet3,intranet3-ssl
haystack,pnf,pnf-ssl


Basically, I want to process this file row by row assigning each word delimited by a comma to a variable.

ie. for the first row, $variable1=echo $variable2=w-cai and $variable3=w-cai-ssl

these variables are used and then the next row is read with the variables now taking the values of those on row 2, delimited by commas.

Any script suggetsions ti do this?
# 2  
Old 09-26-2003
You could do one of the following:
Use awk to process your file by setting the -F (field separator) value to a comma
or
you could use sed to change the commas to spaces and then process each line as if it were a list (assuming your list in not extraordinarily long
or
you could again use sed to change the commas to spaces, then read each line into an array then process the file.
# 3  
Old 09-26-2003
Quote:
Originally posted by google
You could do one of the following:
Use awk to process your file by setting the -F (field separator) value to a comma
or
you could use sed to change the commas to spaces and then process each line as if it were a list (assuming your list in not extraordinarily long
or
you could again use sed to change the commas to spaces, then read each line into an array then process the file.
You could also just change the IFS variable to ',' and the use 'read' to read each value. The 'cut' command could be of some use here if you need to pull individual fields, but "awk -F\," would probably give you a bit more power to do what you need.
# 4  
Old 09-27-2003
Somthing like this may work for you:

#! /usr/bin/ksh
FILE=somefile.txt
cat ${FILE} | sed 's/,/ /g' | while read LINE
do
set ${LINE}
< do what you want to do here>
done


A little explanation. I think you can follow the cat and sed command. The 'while read LINE' basically reads one line at a time and pumps it into the body of the while loop. The 'set ${LINE}' is the cool part. It sets the first string up to the first white space to $1. It sets the second string to the second white space at $2, and so forth. You can then use these variables to do what ever you need to do. This is much more effiecient then doing a for or while loop and then awking each variable individually.
# 5  
Old 09-29-2003
thanks for all the replys guys, I opted for the 'IFS' method which was recommended by someone I work with, and which seems to be perfectly suited for processing csv files.

Thanks again for all the help, there really seems to be no unix problem that can't be posted and solved in this forum! It really is the best around. Smilie
# 6  
Old 09-29-2003
oh, just to clarify, i used the 'set' command to parse the line into data elements (as tony suggested) which I thought was quite cool! (i'm easily amused... :P )
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert a horizontal lines to vertical lines in a csv file

Hi.. I need some help in converting the below horizontal lines to vertical lines format. can anyone help me on this. input file Hour,1,2,3,4,5 90RT,106,111,111,112,111 output file Hour,90RT 1,106 2,111 3,111 4,112 5,111 (3 Replies)
Discussion started by: Raghuram717
3 Replies

2. UNIX for Beginners Questions & Answers

Generate files and use csv data to replace multiple variables in a template

I have a source csv file consists of first field as variable name, and the rest are site-specific information (converted from excel file, where site -specific values in columns). I am trying to create a file for every site using a template and replace the multiple variables with values from the... (3 Replies)
Discussion started by: apalex
3 Replies

3. Shell Programming and Scripting

Splitting CSV into variables then to XML file

I have a text file that looks like this: FIELD1, FIELD2, THIS IS FIELD3, FIELD4 FIELD1, FIELD2, THIS IS FIELD3, FIELD4 FIELD1, FIELD2, THIS IS FIELD3, FIELD4 I need it to turn it into an XML file to run against a custom application. My ultimate goal is for it to look like... (15 Replies)
Discussion started by: jeffs42885
15 Replies

4. UNIX for Beginners Questions & Answers

How to count lines of CSV file where 2 fields match variables?

I'm trying to use awk to count the occurrences of two matching fields of a CSV file. For instance, for data that looks like this... Joe,Blue,Yes,No,High Mike,Blue,Yes,Yes,Low Joe,Red,No,No,Low Joe,Red,Yes,Yes,Low I've been trying to use code like this... countvar=`awk ' $2~/$color/... (4 Replies)
Discussion started by: nmoore2843
4 Replies

5. Shell Programming and Scripting

Assign variables to CSV string (bash)

Hi guys, New to the forum, and been messing around with Linux for about a year now. I'm still very much a rookie, so just assume that I'm a total idiot: I currently have a shell that spits out a CSV number string of about 8 numbers as follows: 1.00,2.00,3.00 ... ,8.00I need to assign a... (7 Replies)
Discussion started by: hansol
7 Replies

6. Shell Programming and Scripting

Help with multiple variables into one row in CSV!

I'm new to shell scripting so I'm guessing I'm just not looking at this from the correct angle as this has to be a common task. What I'm trying to do is take data I've compiled for servers (Name, IPs, HBA WWN's, Storage, etc) and trying to turn that into one row in a CSV file. So File1:... (3 Replies)
Discussion started by: The_Grim_Coder
3 Replies

7. Shell Programming and Scripting

Find and replace variables using a csv table

I have a flat file (template) where I want to replace variables based upon a value in another file (csv). The variables in the template are named %VAR_X_z% The values are in the csv file and X is field 0 of each line and y field 1 and up. Example of the csv: Badidas, 13.00, 12.00, 11.00,... (8 Replies)
Discussion started by: biscayne
8 Replies

8. Shell Programming and Scripting

CSV to SQL insert: Awk for strings with multiple lines in csv

Hi Fellows, I have been struggling to fix an issue in csv records to compose sql statements and have been really losing sleep over it. Here is the problem: I have csv files in the following pipe-delimited format: Column1|Column2|Column3|Column4|NEWLINE Address Type|some descriptive... (4 Replies)
Discussion started by: khayal
4 Replies

9. Shell Programming and Scripting

Turning CSV files into individual Variables

I want to be able to convert the following data from a CSV into individual variables from the columns 2 4 and 8 I can use awk to grab the columns using var1=`cat text.csv | awk "," '{print $2}'` but how do I create separate variables for each line. 595358 ,ECON1010 ,THU ,08:00 - 10:00 ,11 Mar... (6 Replies)
Discussion started by: domsmith
6 Replies

10. Shell Programming and Scripting

Reading variables from CSV file

Hi I am using KSH and trying to read variables from a csv file. I've set the IFS=, and it workds. Problem is where one of the values is text containing a comma. For example the following lines exist in my file. How can I read everything between the quotes into a single variable? APW13812,,1... (2 Replies)
Discussion started by: ventris
2 Replies
Login or Register to Ask a Question