I am writing a shell script and must use the korn shell. I have a comma delimited file with some spaces in the data and I need to use a for loop to read each record for additional processing.
File layout of file.csv (sample):
"Record 1","Field 2","Field 3","Field 4"
"Record 2","Field 2","Field 3","Field 4"
I am trying to use the following code (simplified for demonstration purposes) to read each line in the file:
nCounter=1
for newRecord in `cat file.csv`
do
echo "$nCounter $newRecord"
nCounter=`expr $nCounter + 1`
done
My problem is this...the for ... in `cat ...` doesn't work when there are spaces in the records. So I am getting the following:
1 "Record
2 1","Field
3 2","Field
4 3","Field
4 4"
5 "Record
6 2","Field
...
I want it to read the whole line (until end of line) not a portion of each line (until space). I need the following results:
1 "Record 1","Field 2","Field 3","Field 4"
2 "Record 2","Field 2","Field 3","Field 4"
Any help would be GREATLY appreciated.
Thanks,
Rick
File layout of file.csv (sample):
"Record 1","Field 2","Field 3","Field 4"
"Record 2","Field 2","Field 3","Field 4"
I am trying to use the following code (simplified for demonstration purposes) to read each line in the file:
nCounter=1
for newRecord in `cat file.csv`
do
echo "$nCounter $newRecord"
nCounter=`expr $nCounter + 1`
done
My problem is this...the for ... in `cat ...` doesn't work when there are spaces in the records. So I am getting the following:
1 "Record
2 1","Field
3 2","Field
4 3","Field
4 4"
5 "Record
6 2","Field
...
I want it to read the whole line (until end of line) not a portion of each line (until space). I need the following results:
1 "Record 1","Field 2","Field 3","Field 4"
2 "Record 2","Field 2","Field 3","Field 4"
Any help would be GREATLY appreciated.
Thanks,
Rick