0

I have a text file with following data, need to convert it into xml with awk command in shell script.

Package1,class1
Package1,class2
Package2,Page1
Package2,Page2

This text file need to be converted into xml as shown in below format.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>class1</members>
        <members>class2</members>
        <name>Package1</name>
    </types>
    <types>
        <members>Page1</members>
        <members>Page2</members>
        <name>Package2</name>
    </types>
<version>48.0</version>
</Package>

i am new to shell script. please help me out....

4
  • 1
    There';s a lot of examples of doing that on this forum, search the archives. Commented Jul 10, 2020 at 17:43
  • Thanks Ed. Can you please help me with one relavent example. I referred this stackoverflow.com/questions/48866831/awk-script-create-xml-file. I am not sure how to get in mentioned format. Commented Jul 10, 2020 at 18:12
  • Just post a question with a minimal reproducible example that includes concise, testable sample input plus expected output plus the code you've tried to use. If that's code from some existing answer that you've tried to modify to your purposes but can't that's fine. Commented Jul 10, 2020 at 18:32
  • You might want to use something simpler and seemingly more relevant like stackoverflow.com/a/61011897/1745001 as your starting point though. Commented Jul 10, 2020 at 18:57

1 Answer 1

0

this should get you started

$  awk -F, 'function wrap(t,v) {return "<"t">"v"</"t">"}
            function rec()     {return wrap("types", members ORS wrap("name",$1) ORS)}                
            p && p!=$1 {print rec(); members=""}     
                       {p=$1; members=members ORS wrap("member",$2)} 
            END        {print rec()}' file

<types>
<member>class1</member>
<member>class2</member>
<name>Package2</name>
</types>
<types>
<member>Page1</member>
<member>Page2</member>
<name>Package2</name>
</types>

build up the record until the name changes and print.

Sign up to request clarification or add additional context in comments.

2 Comments

This helps for building xml but I got output as following. <types> </member> <name></name> </types> <types> <member></member> </member> <name></name> </types> <types> <member></member> </member> <name></name> </types> <types> <member></member> </member> <name></name> </types> <types> <member></member> <name></name> </types>
Either your input file is not as shown or you didn't copy the script correctly. Also the output you posted is not possible, missing the initial <member>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.