Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ksh + comma seperated file 1

Status
Not open for further replies.

toddyl

Technical User
Sep 26, 2005
102
US
Hi,

I have a flat file containing data such as:

Orange Juice,1.25
Crisps,0.60
Diet Cola,0.75

I have a ksh script which I'm using to try and parse this file and load the data into a database. The path to the flat file is given as a parameter to the script and then each line is read in and passed to a stored procedure which takes the Item and value as parameters and loads these.

The problem is that my script works fine for entries like Crisps,0.60 but does not work for Orange Juice,1.25 as the space is causing a problem.

Can anyone help me get this to work properly?

Thanks,

tom

Script:

#!/usr/bin/bash

msg=`cat $1`

for I in $msg
do
my_array[0]=$(echo $I | cut -d',' -f1)
my_array[1]=$(echo $I | cut -d',' -f2)

sqlplus -s schema_name/schema_password@database|&
print -p "exec LOAD_SALESPRICE('${my_array[0]}',${my_array[1]});"
print -p "quit"

while read -p LINE
do
print - ${LINE}
done
done

exit 0
 
Try adding
Code:
set IFS='
'
Thats just a CR between the single quotes

Ceci n'est pas une signature
Columb Healy
 
I set the IFS ='
'
and this did not work.

I'm still getting the splitting occuring on the space.

I'm afraid my knowledge is not good enough to make out what is happening in the link to the script.

Any other ideas?
 
Sorry try
Code:
export IFS='
'
IFS is the Input Field Separator, by default it is set to the space character. The code above sets it to the carriage return.
Before the change 'Orange Juice' is two fields separated by the space, after the change it is one field as there is no CR.
OK?

Ceci n'est pas une signature
Columb Healy
 
Hi

Columb Healy said:
IFS is the Input Field Separator, by default it is set to the space character.
Not only to space.
man ksh said:
IFS Internal field separator, used during substitution and by the
read command, to split values into distinct arguments; normal-
ly set to space, tab, and newline.

Feherke.
 
Instead of doing this:

msg=`cat $1`

for I in $msg
do

why don't you change to a while loop eliminating the `cat $1':

Code:
while read I
do
.
.
done < $1

Then you don't need to mess with IFS
 
Hi

He want to split up the lines on commas ( , ), so maybe messing with IFS can even help.
Code:
IFS=,
while read -a my_array
do
  .
  .
done < $1
Note, that the above works with [tt]bash[/tt]. No idea if [tt]ksh[/tt] can read into array or not. If not, this probably will work :
Code:
IFS=,
while read my_array[0] my_array[1]
do
  .
  .
done < $1

Feherke.
 
Tried the export and it appears to be working.
Want to do more testing with this first.

Like the other suggestions as well and will attempt to update script to use this instead.

Thanks for the help.
 
feherke,
to simplify things a bit - and similarly for 'ksh'...
Code:
while IFS=, read -a my_array
do
  .
  .
done < $1

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top