Generating XMLfile using shellscript

I have a text file in the following form and i want to store it in 2-d array. After that i have to generate a XMLfile using the elements of array.
Please tell me :how store this file in the form of 2-d array and then howto generate XML file using that array...all coding has to be done in bash shell

Text file: Let Name be : Test.txt

Abc Def Ghi Jkl
Lmn Nop Qrs Tuv
... ... ... ...
... ... ... ... (anynumber of records )

The array should be like that

arr[0][0] =Abc arr[0][1]=Def arr[0][2]=Ghi arr[0][3]=Jkl
arr[1][0] = Lmn arr[1][1]= Nop ........ ........
...... ...... ........ ...........

Then XML format is known to me i to generate XMLfile using that array elements...

Note : You can suggest any other form of data structure for storing information (Text File) but should be 2-d ,but please also mention about how to generate XMLfile ...

Post the output format that your expecting , since i am not familiar with the XML format.

the format of XML is

<data>
< Field1 name="Abc">
<Otherfield name="Def"><\Otherfield>
<Otherfield name="Ghi"><\Otherfield>
<Otherfield name="Jkl"><\Otherfield>
<\Field1>
<Field2 name="Lmn">
<Otherfield name="Mno"><\Otherfield>
<Otherfield name="Pqr"><\Otherfield>
....................................
....................................
<\Field2>
.................
................

I hope its clear that Abc , Def .... above are the values that has to be retrieved form 2-d array and has to be placed in its proper place

plzz feel free to ask any other question

Never worked in array kind of stuff,

Might be this one will be useful.

awk  '{cou++;for(i=1;i<=NF;i++)
{ if(i==1) { print "< Filed"cou" name=\"" $1 "\">" } 
  else {print "<Otherfield name=\"" $i "\"><\Otherfield>" } 
} {print "<\Filed"cou">" }
} '  file_name.txt 

There are no 2D arrays in bash. It would be simplier to make the xml file directly by processing the input file.
Say if you want to do so.

panyam almost there :wink:

# awk  'BEGIN{print "<data>"}
{cou++;for(i=1;i<=NF;i++)
{ if(i==1) { print "< Filed"cou" name=\"" $1 "\">" }
  else {print "<Otherfield name=\"" $i "\"><\Otherfield>" }
} {print "<\/Filed"cou">" }
} END{print "<\/data>"}' flatfile

Thanks "danmero" . I missed the "data" part. :slight_smile:

a bash script

{	J=1
	echo '<data>'
	while read LINE
	do
		VAR=( $LINE )
		echo '<Field'$J' name="'${VAR[0]}'"><\Field'$J'>'
		for I in 1 2 3
		do	echo '<OtherField name="'${VAR[$I]}'"><\Otherfield>'
		done
		echo '<\Field>'
	done < InputFile1
	echo '<\data>'
} > OutputFile.xml

ok frans ... please tell me to generate xml file directly

---------- Post updated at 07:09 AM ---------- Previous update was at 07:01 AM ----------

thanks frans ... its working ...

---------- Post updated at 07:35 AM ---------- Previous update was at 07:09 AM ----------

This is not showing me any input ..
plzz tell me the command to execute this awk file
also where to see the output

Ok. Copy the above mentioned awk script and put it in any file , for example : scrpt.sh and then run the script as :

. script.sh 

Dont forget to replace the "file_name.txt" in the script with your input file name.

If you really want to generate a XML your syntax is wrong. Closing elements
should start with "</" - not "<\" i.e.

#!/bin/bash

n=1;

echo "<data>" > outfile
while read f o1 o2 o3
do
   echo "<Field$n name="\"$f\"">" >> outfile
   echo "<Otherfield name="\"$o1\""></Otherfield>" >> outfile
   echo "<Otherfield name="\"$o2\""></Otherfield>" >> outfile
   echo "<Otherfield name="\"$o3\""></Otherfield>" >> outfile
   echo "</Field$n>" >> outfile
   n=$((n+1))
done < infile
echo "</data>" >> outfile

# awk 'BEGIN{print "<data>"}
{cou++;for(i=1;i<=NF;i++)
{ if(i==1) { print "< Filed"cou" name=\"" $1 "\">" }
else {print "<Otherfield name=\"" $i "\"><\Otherfield>" }
} {print "<\/Filed"cou">" }
} END{print "<\/data>"}' flatfile

This code is not working ... i am using name of my file instead
of flatfile .. plzz tell me way to execute this file..
like what should be the extension what should be the command for executing this file

---------- Post updated at 10:25 PM ---------- Previous update was at 10:16 AM ----------

thanks for helping me .. but i have to store the contants of that file in a data structure ... plzz tell me a way to store the contents in data structure and then generate aXML file form it..

awk itself is a "command" , yo do not need any thing else to execute it.

Just copy paste the thing what ever we have posted and execute the script.

awk  'BEGIN{print "<data>"}  {cou++;for(i=1;i<=NF;i++){ if(i==1) { print "< Filed"cou" name=\"" $1 "\">" } else {print "<Otherfield name=\"" $i "\"><\Otherfield>" } } {print "<\/Filed"cou">" } } END{print "<\/data>"}'  file_name.txt

I am not sure of any data structures for storing these elements.

Sorry for spoiling the discussion at the last moment.

This is not a scalable and fail proof way to create XML formats. Instead well supported already created modules are available from CPAN
Probably you could start of with XML::Simple

It is very simple to complete your task

i am not finding a way to use xml::simple.
if possible plzz write a shell script for that...