user494766
user494766

Reputation: 155

awk result problem

I use a command for finding strings and numbers in a file

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

the output is something like

a
b
c
d

e
f
g
t

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>

I don't know how to manage the result from awk. Could you help me please?

Thanks in advance

Upvotes: 0

Views: 78

Answers (1)

shellter
shellter

Reputation: 37278

I would be nice to see what your real data looks like, but given that your output shows 4 fields and your input shows 4 fields, here is the basic idea.

awk 'BEGIN {
   RS="" # make blank line between sets of data the RecordSep
   FS="\n" # make each line as a field in the rec (like $1, $2 ...)
}
{ # this is the main loop, each record set is procssed here   
    printf("<xml>\n\t<name>%s</name>\n\t<surname>%s</surname>\n\t<Addr1>%s</Addr1>\n\t<Addr2>%s</Addr2>\n</xml>",
       $1, $2, $3, $4 )
} ' 1.txt > 1.xml

Note: there should be only 1 blank like between your record sets.

I hope this helps.

P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, or give it a + (or -) as a useful answer.

Upvotes: 1

Related Questions