Closing XML tags in one line

Any one can help
Example having an spml output as below (the complete one has more than 10000 tags):

<ts11>
<msisdn>123</msisdn>
<bcieID>TELEPHON</bcieID>
</ts11>
<ts21>
<msisdn>987</msisdn>
</ts21>
... =======
I want to have every tag closed in one line
<ts11>,<msisdn>123</msisdn>,<bcieID>abc</bcieID>,</ts11>,
<ts21>,<msisdn>987</msisdn>,</ts21>,

Try:

awk '/<\/ts11>/{print $0",";next}{printf $0","}END{printf "\n"}' file.xml

Thanks.
This is specific to ts11, I have 1000nds of different tags..

Is there any pattern to these thousands of tags? Will all ts[0-9]* tags do?

awk '/<\/ts[0-9]*>/{print $0",";next}{printf $0","}END{printf "\n"}' file.xml

OK, so try:

awk '/^<\/.*>$/{print $0",";next}{printf $0","}END{printf "\n"}' file.xml

This is the tricky thing for me, the tag name is not having the same format and number of lines between the open tag and the close tag different for every object, eg :
<ts11>
<msisdn>123</msisdn>
<bcieID>TELEPHON</bcieID>
</ts11>
<ts21>
<msisdn>987</msisdn>
</ts21>
<cfu>
<basicServiceGroup>TS10-telephony</basicServiceGroup>
<status>4</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
</cfu>

<cfb>
<basicServiceGroup>TS10-telephony</basicServiceGroup>
<isdnNumber>12345</isdnNumber>
<status>7</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
<notifyForwardingSubscriber>false</notifyForwardingSubscriber>
<ftnoType>internat</ftnoType>
</cfb>

This should work with empty lines:

awk 'NF==0{next}/^<\/.*>$/{print $0",";next}{printf $0","}END{printf "\n"}' file.xml

Thanks dears, here is the file example and below is the result I got with your commands

<?xml version="1.0" encoding="UTF-8"?>
<identifier>1234567890</identifier>

<tag>
<ntype>multi</ntype>
<mobileSubscriberType>genericSubscriber</mobileSubscriberType>
<umtsSubscriber>
<accTypeGSM>true</accTypeGSM>
<accTypeGERAN>true</accTypeGERAN>
<accTypeUTRAN>true</accTypeUTRAN>
</umtsSubscriber>
<wllSubscriber>false</wllSubscriber>
<mscat>10</mscat>
<odboc>0</odboc>
<odbic>0</odbic>
<odbr>0</odbr>
<odboprc>0</odboprc>
<odbssm>0</odbssm>
<clip>true</clip>
<clipOverride>false</clipOverride>
<colpOverride>false</colpOverride>
<hold>true</hold>
<mpty>true</mpty>
<nwa>3</nwa>
<odbgprs>0</odbgprs>
<rr>WhiteL</rr>
<sr>2</sr>
<odbsci>0</odbsci>
<ts11>
<msisdn>12345</msisdn>
<bcieID>TELEPHON</bcieID>
</ts11>
<ts21>
<msisdn>12345</msisdn>
</ts21>
<ts22>
<msisdn>12345</msisdn>
</ts22>
<bs30genr>
<msisdn>12345</msisdn>
<bcieID>VIDEO</bcieID>
</bs30genr>
<gprs>
<msisdn>12345</msisdn>
</gprs>
<cfu>
<basicServiceGroup>TS10-telephony</basicServiceGroup>
<status>4</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
</cfu>
<cfu>
<basicServiceGroup>BS30-dataSync</basicServiceGroup>
<status>4</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
</cfu>
<cfb>
<basicServiceGroup>TS10-telephony</basicServiceGroup>
<isdnNumber>1234567</isdnNumber>
<status>7</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
<notifyForwardingSubscriber>false</notifyForwardingSubscriber>
<ftnoType>internat</ftnoType>
</cfb>
</tag>

Result which was fine for some lines but not for others

#awk '/^<\/.*>$/{print $0",";next}{printf $0","}END{printf "\n"}' testtag
<?xml version="1.0" encoding="UTF-8"?>,<identifier>1234567890</identifier>,,<tag>,<ntype>multi</ntype>,<mobileSubscriberType>genericSubscriber</mobileSubscriberType>,<umtsSubscriber>,<accTypeGSM>true</accTypeGSM>,<accTypeGERAN>true</accTypeGERAN>,<accTypeUTRAN>true</accTypeUTRAN>,</umtsSubscriber>,
<wllSubscriber>false</wllSubscriber>,<mscat>10</mscat>,<odboc>0</odboc>,<odbic>0</odbic>,<odbr>0</odbr>,<odboprc>0</odboprc>,<odbssm>0</odbssm>,<clip>true</clip>,<clipOverride>false</clipOverride>,<colpOverride>false</colpOverride>,<hold>true</hold>,<mpty>true</mpty>,<nwa>3</nwa>,<odbgprs>0</odbgprs>,<rr>WhiteL</rr>,<sr>2</sr>,<odbsci>0</odbsci>,<ts11>,<msisdn>12345</msisdn>,<bcieID>TELEPHON</bcieID>,</ts11>,
<ts21>,<msisdn>12345</msisdn>,</ts21>,
<ts22>,<msisdn>12345</msisdn>,</ts22>,
<bs30genr>,<msisdn>12345</msisdn>,<bcieID>VIDEO</bcieID>,</bs30genr>,
<gprs>,<msisdn>12345</msisdn>,</gprs>,
<cfu>,<basicServiceGroup>TS10-telephony</basicServiceGroup>,<status>4</status>,<notifyCallingSubscriber>false</notifyCallingSubscriber>,</cfu>,
<cfu>,<basicServiceGroup>BS30-dataSync</basicServiceGroup>,<status>4</status>,<notifyCallingSubscriber>false</notifyCallingSubscriber>,</cfu>,
<cfb>,<basicServiceGroup>TS10-telephony</basicServiceGroup>,<isdnNumber>1234567</isdnNumber>,<status>7</status>,<notifyCallingSubscriber>false</notifyCallingSubscriber>,<notifyForwardingSubscriber>false</notifyForwardingSubscriber>,<ftnoType>internat</ftnoType>,</cfb>,

First, please use code tags: video tutorial on using code tags. Second, post desired output for this sample data.

It is best to use a XML tool to process XML file.

Install Perl-XML-XPath

# yum install perl-XML-XPath.noarch
or
# perl -MCPAN -e 'install XML::XPath'

Try this:

$ cat a.xml
<?xml version="1.0"?>
<something>
<ts11>
<msisdn>123</msisdn>
<bcieID>TELEPHON</bcieID>
</ts11>
<ts21>
<msisdn>987</msisdn>
</ts21>
<cfu>
<basicServiceGroup>TS10-telephony</basicServiceGroup>
<status>4</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
</cfu>

<cfb>
<basicServiceGroup>TS10-telephony</basicServiceGroup>
<isdnNumber>12345</isdnNumber>
<status>7</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
<notifyForwardingSubscriber>false</notifyForwardingSubscriber>
<ftnoType>internat</ftnoType>
</cfb>
</something>


$ cat a.pl
#! /usr/bin/perl
use XML::XPath;

$xp=XML::XPath->new($ARGV[0]);
$nodes=$xp->find('/something/*');
foreach $n ($nodes->get_nodelist) {
        print  $n->getName(), "\n";
        $str=$n->toString();
        $str=~s/\n//g;
        $str=~s/></>,</g;
        print $str, "\n";
}


$ ./a.pl a.xml
ts11
<ts11>,<msisdn>123</msisdn>,<bcieID>TELEPHON</bcieID>,</ts11>
ts21
<ts21>,<msisdn>987</msisdn>,</ts21>
cfu
<cfu>,<basicServiceGroup>TS10-telephony</basicServiceGroup>,<status>4</status>,<notifyCallingSubscriber>false</notifyCallingSubscriber>,</cfu>
cfb
<cfb>,<basicServiceGroup>TS10-telephony</basicServiceGroup>,<isdnNumber>12345</isdnNumber>,<status>7</status>,<notifyCallingSubscriber>false</notifyCallingSubscriber>,<notifyForwardingSubscriber>false</notifyForwardingSubscriber>,<ftnoType>internat</ftnoType>,</cfb>

Thanks a lot dears, with Perl it works perfectly...