
10-07-2008
|
|
Shell programmer, author
|
|
|
Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
|
|
Quote:
Originally Posted by uwork72
Group,
Code:
$ 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
|
This does what you asked for:
Code:
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:
Code:
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)
|