help xml

Group,

$ cat 2233
12236 ID2
12239 ID3

Please guide me to construct the following XML from the above input.


<Comp>
        <main>
                <hlp fS="12236" eS="12237">
                        <std no="2233" />
                        <id="ID2"/>
                </hlp>
                <hlp fS="12239" eS="12240">
                        <std no="2233" />
                        <id="ID3"/>
                </hlp>
        </main>
</Comp>

** eS is +1 of fS value
** std no is the file name

What have you tried, which parts are you having issues with? Do you have a preference for a particular implementation language?

(Isn't it a tad moronic to have a field value which is always another field value plus one?)

hi try below perl scirpt
say the script name is a.pl, below command can address your issue.

format STDOUT_TOP =
<Comp>
        <main>
.
format STDOUT =
		<hlp fs="@<<<<" es="@<<<<">
		$text1     $text2  
			<std no="@<<<"/>
			$text3
			<id="@<<"/>
			$text4
		</hlp>
.
$file=shift;
open(FH,"<$file") or die "Can not open file";
while(<FH>){
	@arr=split(" ",$_);
	$text1=$arr[0];
	$text2=$arr[0]+1;
	$text3=$file;
	$text4=$arr[1];
	write;
}
print "        </main>\n";
print "</Comp>\n";
close(FH);

This does what you asked for:

file=2233
{
 read num1 id1
 read num2 id2
} < "$file"
fmt='<Comp>
        <main>
                <hlp fS="%d" eS="%d">
                        <std no="%d" />
                        <id="%s"/>
                </hlp>
                <hlp fS="%d" eS="%d">
                        <std no="%d" />
                        <id="%s"/>
                </hlp>
        </main>
</Comp>'

printf "%fmt\n" "$num1" "$(( $num1 + 1 ))" "$file" "$id1" \
                "$num2" "$(( $num3 + 1 ))" "$file" "$id2"

More probably, what you want is:

file=2233
head='<Comp>
        <main>'
fmt='                <hlp fS="%d" eS="%d">
                        <std no="%d" />
                        <id="%s"/>
                </hlp>
'

tail='        </main>
</Comp>'

{
 printf "%s\n" "$head"
 while read nnum id
 do
   printf "$fmt" "$num" "$(( $num + 1 ))" "$file" "$id"
 done < "$file"
 printf "%s\n" "$tail"
} > NEWFILE


## (untested)

Era, thanks for replying. I tried for only one entry in the input file; something like this

....
FILE=$1
fS=$(awk '{print $1}' $FILE)
((eS=fS+1))

echo "<Comp>" >> myxml
...
...

But I am not able to do the same if num entries in the input file in more than 1. Please help.

cfajohnson, that worked perfectly fine for me. Thank you.