# Splitting CSV into variables then to XML file

**URL:** <https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616>\
**Category:** Shell Programming and Scripting\
**Created:** [July 26, 2017, 11:40am UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616 "2017-07-26T11:40:15Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [July 26, 2017, 11:40am UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/1 "2017-07-26T11:40:15Z")

</div>

I have a text file that looks like this:

```nohighlight
 
  
 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:

```nohighlight
 
 <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?

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://community.unix.com/letter_avatar/ravindersingh13/32/6_449c533f586939b6e5f246632a46421f.png) [@RavinderSingh13](https://community.unix.com/u/RavinderSingh13)\
**Post date:** [July 26, 2017, 11:53am UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/2 "2017-07-26T11:53:52Z")

</div>

Hello jeffs42885,

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

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [July 26, 2017, 11:54am UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/3 "2017-07-26T11:54:41Z")

</div>

something along these lines:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [July 26, 2017, 11:59am UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/4 "2017-07-26T11:59:31Z")

</div>

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:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [July 26, 2017, 2:33pm UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/5 "2017-07-26T14:33:52Z")

</div>

Not working for me.

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

 

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [July 26, 2017, 2:51pm UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/6 "2017-07-26T14:51:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [July 26, 2017, 2:53pm UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/7 "2017-07-26T14:53:57Z")

</div>

```nohighlight
 
 #!/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
 

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [July 26, 2017, 3:17pm UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/8 "2017-07-26T15:17:16Z")

</div>

remove spaces in front of closing `EOF` label

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [July 26, 2017, 3:23pm UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/9 "2017-07-26T15:23:06Z")

</div>

```nohighlight
 
 #!/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
 

```

```nohighlight
./2.ksh[5]: syntax error at line 9 : `newline or ;' unexpected

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [July 26, 2017, 3:23pm UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/10 "2017-07-26T15:23:50Z")

</div>

`cat << EOF`

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [July 26, 2017, 3:29pm UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/11 "2017-07-26T15:29:42Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [July 26, 2017, 5:32pm UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/12 "2017-07-26T17:32:13Z")

</div>

`cat <<EOF`

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [July 26, 2017, 6:24pm UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/13 "2017-07-26T18:24:47Z")

</div>

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):

```nohighlight
 
#!/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

---

<div class="post-metadata">

**Author:** ![jeffs42885](https://community.unix.com/letter_avatar/jeffs42885/32/6_449c533f586939b6e5f246632a46421f.png) [@jeffs42885](https://community.unix.com/u/jeffs42885)\
**Post date:** [July 27, 2017, 10:47am UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/14 "2017-07-27T10:47:13Z")

</div>

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

```nohighlight
  exec 3>

```

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [July 27, 2017, 12:36pm UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/15 "2017-07-27T12:36:01Z")

</div>

> [@jeffs42885](#):
>
> which Im assuming is defined in
> 
> ```plaintext
> exec 3>
> 
> ```

Yes.

```plaintext
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

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [July 27, 2017, 3:34pm UTC](https://community.unix.com/t/splitting-csv-into-variables-then-to-xml-file/367616/16 "2017-07-27T15:34:23Z")

</div>

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