tags:

views:

57

answers:

4

Input file format:

<Tariff>
<AA>10030</AA>
<AA>100</AA>
</Tariff>
<Tariff>
<AA>30004</AA>
<AA>30001</AA>
</Tariff>
<Tariff>
<AA>Account division</AA>
<AA>AIR</AA>
<AA>AA</AA>
<AA>10039</AA>
</Tariff>

Output format : Output should be aligned in a manner of Open Tag of "<Tariff>" & Having End Tag of "<\Tariff>" also separated by comma separator.

Output :

<Tariff>,<AA>10030</AA>,<AA>100</AA>,</Tariff>
<Tariff>,<AA>30004</AA>,<AA>30001</AA>,</Tariff>
<Tariff>,<AA>Account division</AA>,<AA>AIR</AA>,<AA>AA</AA>,<AA>10039</AA>,</Tariff>
A: 

Hello, with Vim:

vim -U file.txt -c 'g/^<Tariff>$/ .,/^<\/Tarrif>$/ - 1 s/$/,/' -c 'g/^<Tariff>,$/ .,/^<\/Tarrif>$/ join!' -c 'wq'

Will only work if your Tariff tags are alone on their own lines without heading nor trailing spaces.

Hope you will start accepting answers.

Benoit
+2  A: 

This is very easy using a simple XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="Tariff">
    <Tariff>
      <xsl:text>,</xsl:text>
      <xsl:apply-templates />
      <xsl:text>,</xsl:text>
    </Tariff>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
0xA3
A: 

You can use an awk script to do this as shown below:

pax> echo '<Tariff>
<AA>10030</AA>
<AA>100</AA>
</Tariff>
<Tariff>
<AA>30004</AA>
<AA>30001</AA>
</Tariff>
<Tariff>
<AA>Account division</AA>
<AA>AIR</AA>
<AA>AA</AA>
<AA>10039</AA>
</Tariff>' | awk '
    {
        if ($0=="</Tariff>") {
            printf "</Tarrif>\n"
        } else {
            printf $0","
        }
    }'

<Tariff>,<AA>10030</AA>,<AA>100</AA>,</Tarrif>
<Tariff>,<AA>30004</AA>,<AA>30001</AA>,</Tarrif>
<Tariff>,<AA>Account division</AA>,<AA>AIR</AA>,<AA>AA</AA>,<AA>10039</AA>,</Tarrif>

That's the nicely formatted version, the quick version is:

awk '{if ($0=="</Tariff>") {printf "</Tarrif>\n"} else {printf $0","}}' infile

Keep in mind this is a specific solution to your input file format. XML files should generally be handled with tools specific to the job since quick'n'dirty solutions will break when the input format changes (for example, if your end tags aren't on a line of their own with no spaces either side, or if you have a sinfle line containing two tariff sections).

However, if your input file format is limited as you describe, a quick'n'dirty solution will often be faster than trying to learn how to use XML transformation tools. It sometimes depends on whether you want the job done right, or done right now.

paxdiablo
A: 
$ awk 'ORS=(/<\/Tariff>/) ?"\n":","' file
<Tariff>,<AA>10030</AA>,<AA>100</AA>,</Tariff>
<Tariff>,<AA>30004</AA>,<AA>30001</AA>,</Tariff>
<Tariff>,<AA>Account division</AA>,<AA>AIR</AA>,<AA>AA</AA>,<AA>10039</AA>,</Tariff>
ghostdog74