Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-28-2006
Registered User
 

Join Date: Sep 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Read the csv file and assign the values in to variable

I have a csv file with the values seperated by commas.I want to extract these values one by one and assign to a variable using shell script.Any ideas or code?
Sponsored Links
    #2  
Old 09-28-2006
blowtorch's Avatar
AFK
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,351
Thanks: 0
Thanked 2 Times in 2 Posts
You need to post a sample from your file. 2-3 lines will do.

I would tell you to directly read the fields into variables by changing the IFS variable, but it would be better if you could give a sample.
Sponsored Links
    #3  
Old 09-28-2006
Registered User
 

Join Date: Sep 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
sample file name:sitenames.csv
www.real.com,324kb,1,MyPlace
www.yahoo.com.423kb,2,Mysite,Public site


Datas will be in this form
    #4  
Old 09-28-2006
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,849
Thanks: 3
Thanked 45 Times in 44 Posts
Not able to open the sample file
Sponsored Links
    #5  
Old 09-28-2006
Registered User
 

Join Date: Sep 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
i have just put the content of the sample file which is seperated by commas.Not attached any file..
Sponsored Links
    #6  
Old 09-28-2006
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 6,959
Thanks: 69
Thanked 351 Times in 338 Posts
assuming sitenames.csv

Code:
www.real.com,324kb,1,MyPlace
www.yahoo.com,423kb,2,Mysite,Public site


Code:
#!/bin/ksh

while IFS=, read site space number name
do
  echo "site      -> [${site}]"
  echo "space   -> [${space}]"
  echo "number -> [${number}]"
  echo "name     -> [${name}]"
done < sitenames.csv

Sponsored Links
    #7  
Old 09-28-2006
Registered User
 

Join Date: Jan 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
What if number of fields are not known and they vary among the lines
how do we capture and print them as shown above

while IFS=, read site space number . . . . . ( n number of fields )
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Assign Values to variables from a text file sarsani Shell Programming and Scripting 3 06-26-2009 07:05 PM
Awk/shell question: Read from file and assign to variables. akbar Shell Programming and Scripting 3 05-07-2008 06:10 PM
How to Read a config file and Assign to Variable redlotus72 UNIX for Dummies Questions & Answers 1 11-13-2006 11:14 AM
Assign value to a variable in a parameter file gopskrish Shell Programming and Scripting 2 06-22-2005 07:26 AM
How can I read variable values from file? redlotus72 UNIX for Dummies Questions & Answers 1 03-14-2005 04:38 AM



All times are GMT -4. The time now is 05:06 AM.