How to read and parse the content of csv file containing # as delimeter into fields using Bash?

#!/bin/bash
i=0
cat 1.csv | while read fileline
do
echo "$fileline"
IFS="#" flds=( $fileline )
nrofflds=${#flds[@]}
echo "noof fields$nrofflds"
fld=0
while [ "$fld" -lt "$nrofflds" ]
do
      echo "noof counter$fld" 
      echo "$nrofflds"
 
     #fld1="${flds[0]}" trying to store the content of line to fields but i am unable to do it. 
for example the line look like this A#B#C#D#E#F
i want to A in one feild B in other and so on 
 
      echo "$col2" 
      fld=$(($fld+1))
   done
 
 
 
i=$(($i+1))
echo "$i"
done

thanks
barani

What exactly you are trying to do with the csv file as input ?.

use tr
change the # to a tab - tab characters don't show in this example just enter tic (') then a tab, then another tic

cat 1.csv | tr '#' '	'| while read fileline
do
echo "$fileline"
IFS='	 ' flds=( $fileline )
nrofflds=${#flds[@]}
echo "noof fields$nrofflds"
fld=0
while [ "$fld" -lt "$nrofflds" ]
do
      echo "noof counter$fld" 
      echo "$nrofflds"
 
      fld1="${flds[0]}" 
 
      echo "$col2" 
      fld=$(($fld+1))
   done
 
 
 
i=$(($i+1))
echo "$i"
done

You have other errors as well - the fld variable is not initialized. You cannot do arithmetic comparisons on an unset variable.

Consider awk - not shell - it is meant for what you seem to want to do.

Hi,

the csv file with the delimeter #.

A#B#C#D#E#F#G#H
1#2#3#4#5#6#7#8
Z#x#c#V
7#2#8#9
N.

I want to read the file line by line and store in rowarray.
then the rowarray content should be spilt or made to fields using the delimeter #.
i am not sure can we store the fields in to column array.then it may be loaded to database.

then these fields are to be loaded to a oracle database.
may be in awk.provide the code if possible.

thanks
barani

I would consider using Oracle SQL*Loader ...