|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Not able to open the sample file
|
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
i have just put the content of the sample file which is seperated by commas.Not attached any file..
|
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|