Splitting CSV into variables then to XML file

I have a text file that looks like this:

 
  
 FIELD1, FIELD2, THIS IS FIELD3, FIELD4
 FIELD1, FIELD2, THIS IS FIELD3, FIELD4
 FIELD1, FIELD2, THIS IS FIELD3, FIELD4
 

I need it to turn it into an XML file to run against a custom application. My ultimate goal is for it to look like this:

 
 <FIRSTFIELD="FIELD1">
   <task="update" name="FIELD2">
      <field3="FIELD3" task="add" field4="FIELD4" />
 

Here is what I have tried and it doesn't seem to be parsing the input file properly, or forming the XML. Could it be because of the spaces in field3?

for i in `cat input.txt`
do
 field1=`echo $i | cut -d"|" -f1`
 field2=`echo $i | cut -|"," -f2`
 field3=`echo $i | cut -d"|" -f3`
 field4=`echo $i | cut -d"|" -f4`
 
echo <FIRSTFIELD="FIELD1"> >> out.xml
echo  <task="update" name="FIELD2"> >> out.xml
echo  <field3="FIELD3" task="add" field4="FIELD4" /> >> out.xml 
done

Help is appreciated

Hello jeffs42885,

Could you please try following and let me know if this helps.

awk -v s1="\"" -F'[ ,]' '{print "<FIRSTFIELD=" s1 $1 s1 ">" RS "\t<task=" s1 "update" s1 " name=" s1 $3 s1 ">" RS "\t\t<field3=" s1 $7 " task=" s1 "add" s1 " field4=" s1 $NF" />"}'  Input_file

Thanks,
R. Singh

something along these lines:

awk -F '[ ,]' -v qq='"' '{printf("<FIRSTFIELD=%s%s%s>\n\t<task=%supdate%s name=%s%s%s>\n\t\t<filed3=%s%s%s task=%sadd%s field4=%s%s%s />\n", qq, $1,qq,qq,qq,qq,$2,qq,qq,$3,qq,qq,qq,qq,$4,qq)}' myCSVfile.csv

Indeed it is because of the spaces, `cat input.txt` breaks the content of the input file into space separated fields.
--

A better way of doing this would go along these lines:

while IFS=, read f1 f2 f3 f4 
do
  cat << EOF
 <FIRSTFIELD="${f1}">
   <task="update" name="${f2}">
     <field3="${f3}" task="add" field4="${f4}" />
EOF
done < infield > out.xml
1 Like

Not working for me.

 
  
 [jeff@server:/path/jeff]> ./2.ksh
./2.ksh[5]: syntax error at line 7 : `<' unmatched

 

There isn't a < on line 7, so show your code.

 
 #!/bin/ksh
  
 while IFS=, read f1 f2 f3 f4
do
cat << EOF
 <applicationGroup name="${f1}">
 <field task="update" name="${f2}">
 <mapping dbvalue="${f4}" task="add" displayedValue="${f3}" />
 EOF
done < input.txt > out.xml
 

remove spaces in front of closing EOF label

 
 #!/bin/ksh
  
 while IFS=, read f1 f2 f3 f4
do
cat < EOF
        <applicationGroup name="${f1}">
        <field task="update" name="${f2}">
        <mapping dbvalue="${f4}" task="add" displayedValue="${f3}" />
EOF
done < input.txt > out.xml
 
./2.ksh[5]: syntax error at line 9 : `newline or ;' unexpected

cat << EOF

 
 ./2.ksh[3]: syntax error at line 5 : `<' unmatched
 

cat <<EOF

Along the same lines, but without a here-document (and IMHO a lot easier to read and to maintain, but that might be personal bias):

 
#!/bin/ksh

exec 3>/path/to/outfile    # to overwrite the file
# exec 3>>/path/to/outfile # instead, to append to the file
  
while IFS=, read f1 f2 f3 f4 ; do
     print -u3 - "<applicationGroup name=\"${f1}\">"
     print -u3 - "     <field task=\"update\" name=\"${f2}\">"
     print -u3 - "          <mapping dbvalue=\"${f4}\" task=\"add\" displayedValue=\"${f3}\" />"
done < input.txt

exec 3>&-

I hope this helps.

bakunin

I tried the above solution and it does not output anything into my output file which Im assuming is defined in

  exec 3>

Yes.

exec 3> /some/filename

will open the file /some/filename in I/O-channel 3 for writing. Subsequent print -u3 -commands will write to that channel.

Either you specified a path which doesn't exist (or the script is not allowed to write to) or i am at a loss as to why it not works for you.

As this is working for me (AIX 7.x, ksh88) please post the exact script you used (copy and paste from the terminal to here), all error messages and what OS and which ksh version you are using.

bakunin

Also have you tried #4 verbatim and without introducing typos as others have pointed out?