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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I read values from a CSV file using Shell?
# 1  
Old 07-08-2010
How can I read values from a CSV file using Shell?

SHELL SCRIPT

Hi,
I have a file in which contents are as follows:

Code:
9999,abdc,123
9988,aba_12,2323

and so on

I want to read the contents of this file such that i can do

Code:
echo "This is $a followed by $b an then $c"

I tried the following but id did not work

Code:
cat test | cut -d ',' -f1| cut -d ',' -f2| cut -d ',' -f3| while read a read b
do 
echo "This is $a followed by $b an then $c"
done


Last edited by Scott; 07-08-2010 at 02:05 PM.. Reason: Code tags, please...
# 2  
Old 07-08-2010
here is how using awk

The following done with a CSV I had on my system.
Code:
C:\cygwin\tmp>gawk -F',' '{print "This is "$1" followed by "$2" and then "$3}' bopis.txt
This is S followed by h and then 07/06/2010
This is S followed by h and then 07/06/2010

# 3  
Old 07-08-2010
Thanks Joeyg.
This works great but actually i had to use these variables in a HTTP Call something like

Code:
echo "http://mayank.gup.com/method=add&number=1st_field&name=2nd_field&mask=3rd_field"

So how can i assign or put these values in their respective fields with your method beats me???

Please Help...

Last edited by Scott; 07-08-2010 at 02:05 PM..
# 4  
Old 07-08-2010
Code:
awk -F',' '{print "echo http://mayank.gup.com/method=add&number="$1"&name="$2"&mask="$3}' infile

# 5  
Old 07-08-2010
Quote:
Originally Posted by mayanksargoch
Thanks Joeyg.
This works great but actually i had to use these variables in a HTTP Call something like

echo "http://mayank.gup.com/method=add&number=1st_field&name=2nd_field&mask=3rd_field"

So how can i assign or put these values in their respective fields with your method beats me???

Please Help...
Try something like this:
Code:
echo "http://mayank.gup.com/method=add&number=1st_field&name=2nd_field&mask=3rd_field" |
awk -F"[=&]" '{print $4, $6, $8}' |
while read a b c
do
  # do your stuff here with $a, $b and $c...
  .
  .
done

# 6  
Old 07-08-2010
Quote:
Originally Posted by Franklin52
Try something like this:
Code:
echo "http://mayank.gup.com/method=add&number=1st_field&name=2nd_field&mask=3rd_field" |
awk -F"[=&]" '{print $4, $6, $8}' |
while read a b c
do
  # do your stuff here with $a, $b and $c...
  .
  .
done


Sorry Buddy this does not work as i am not reading from the HTTP URL but from a file and putting the values into a HTTP URL.
# 7  
Old 07-08-2010
Sorry, I misunderstood the question, maybe something like this?
Code:
awk -F, '{print "http://mayank.gup.com/method=add&number=" $1 "&name=" $2 "&mask=" $3}' file

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

How to calculate avg values of csv file using shell scripting .?

hi all i have a reporting work and i want it to be automated using shell scripting kindly let me know how can i make that possibe . eg data are :... (2 Replies)
Discussion started by: Avinash shaw
2 Replies

3. Shell Programming and Scripting

Read in shell variable values from a file

Hello, I have a simple script that runs an application, # these arguments have the same value for all splits ARCH=12.11.1 BATCHES=50 EPOCHS=5000 LEARN_MODE=ONLINE LEARN_RATE=0.25 PROJ=02_BT_12.11.1.proj echo "processing split A on hex" cd A/ DATA_SET=S2A_v1_12.1.1_1... (4 Replies)
Discussion started by: LMHmedchem
4 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. Shell Programming and Scripting

Korn shell program to parse CSV text file and insert values into Oracle database

Enclosed is comma separated text file. I need to write a korn shell program that will parse the text file and insert the values into Oracle database. I need to write the korn shell program on Red Hat Enterprise Linux server. Oracle database is 10g. (15 Replies)
Discussion started by: shellguy
15 Replies

6. Shell Programming and Scripting

Read data from .csv file through shell script & modify

I need to read data from a file called "test.csv" through shell script where the file contains values like name,price,descriptor etc. There are rows where descriptor (& in some rows name) are written as string & other characters like "car_+" OR "bike*" etc where it should contains strings like... (3 Replies)
Discussion started by: raj100
3 Replies

7. Shell Programming and Scripting

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? (11 Replies)
Discussion started by: rajbal
11 Replies

8. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies

9. Shell Programming and Scripting

how can i read text file and assign its values to variables using shell

Hello, I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables. my cat.dat is: 11 12 +380486461001 12 13 +380486461002 13 14 +380486461003 i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
Discussion started by: rosalinda
4 Replies

10. Shell Programming and Scripting

Korn Shell Script - Read File & Search On Values

I am attempting to itterate through a file that has multiple lines and for each one read the entire line and use the value then to search in other files. The problem is that instead of an entire line I am getting each word in the file set as the value I am searching for. For example in File 1... (2 Replies)
Discussion started by: run_unx_novice
2 Replies
Login or Register to Ask a Question