Hi,
I use a command with awk in bash shell so as to extract info from a file.

awk -F'[=,: ]' '{print /uid=/?$4:(/^telephoneN/)?$2:$3}'  1.txt

the output is something like
aaaa
bbbb
cccc
dddd

eeee
ffff
gggg
tttt

I would like to write this output in a file 2.xml

<xml>
<name>aaaa</name>
<surname>bbbb</surname>
...
</xml>
<xml>
<name>eeee</name>
<surname>ffff</surname>
...
</xml>

could you help me do it please ?

thanks a lot in advance

Recommended Answers

Syntax of your xml is wrong. You can't have the outermost tag twice in a file. So modified it a bit and assume that's what you want.

<xml>
  <entry>
    <name>aaaa</name>
    <surname>bbbb</surname>
  </entry>
  <entry>
    <name>eeee</name>
    <surname>ffff</surname>
  </entry>
</xml>

Given that you already have code ( {print /uid=/?$4:(/^telephoneN/)?$2:$3}

Jump to Post

All 3 Replies

Hi,
I use a command with awk in bash shell so as to extract info from a file.

awk -F'[=,: ]' '{print /uid=/?$4:(/^telephoneN/)?$2:$3}'  1.txt

the output is something like
aaaa
bbbb
cccc
dddd

eeee
ffff
gggg
tttt

I would like to write this output in a file 2.xml

<xml>
<name>aaaa</name>
<surname>bbbb</surname>
...
</xml>
<xml>
<name>eeee</name>
<surname>ffff</surname>
...
</xml>

could you help me do it please ?

thanks a lot in advance

Somebody please ?

Syntax of your xml is wrong. You can't have the outermost tag twice in a file. So modified it a bit and assume that's what you want.

<xml>
  <entry>
    <name>aaaa</name>
    <surname>bbbb</surname>
  </entry>
  <entry>
    <name>eeee</name>
    <surname>ffff</surname>
  </entry>
</xml>

Given that you already have code ( {print /uid=/?$4:(/^telephoneN/)?$2:$3} ) that can print line 3, 4 & 7, 8. Just add the xml tag in front and back appropriately. For opening and closing of each ( <entry> ) can also be done in this line itself.
For teh opening and closing tags ( <xml> ) of the file itself use BEGIN{} and END{} of awk. These are like c'tor / d'tor in awk, get executed before and after parsing of 1.txt.

If there are cases where the input file isn't consistent (name / surname is missing), you can use if/else/...

thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.