AWK Scripting: Convert Single Column XML to CSV

2019-05-31 05:56发布

问题:

I am new to AWK-scripting, and need advice on converting a set of single-column data to a pivoted(?) set of CSV.

The data resembles:

<field1>A</field1>
<field2>B</field2>
<field3>C</field3>
<field1>1</field1>
<field2>2</field2>
<field3>3</field3>

...and I would like to generate:

field1,field2,field3
A,B,C
1,2,3

Any assistance and advice would be greatly appreciated.

Thank you!

回答1:

tst.awk:

BEGIN{FS="[</>]"; OFS=","}
!($5 in label2colNr) {
    label2colNr[$5] = ++numCols
    colNr2label[numCols] = $5
}
{
    colNr = label2colNr[$5]
    val[++numRows[colNr],colNr] = $3
    maxRows = (numRows[colNr] > maxRows ? numRows[colNr] : maxRows)
}
END {
    for (colNr=1; colNr <= numCols; colNr++) {
        printf "%s%s", colNr2label[colNr], (colNr<numCols ? OFS : ORS)
    }

    for (rowNr=1; rowNr <= maxRows; rowNr++) {
        for (colNr=1; colNr <= numCols; colNr++) {
            printf "%s%s", val[rowNr,colNr], (colNr<numCols ? OFS : ORS)
        }
    }
}

Usage: awk -f tst.awk file.xml

Output:

field1,field2,field3
A,B,C
1,2,3

Credits to: Ed Morton: https://stackoverflow.com/a/48252943/3776858



标签: xml csv awk pivot