Skip to content

Instantly share code, notes, and snippets.

@webb
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webb/19ee7b1326e5f71d8cef to your computer and use it in GitHub Desktop.
Save webb/19ee7b1326e5f71d8cef to your computer and use it in GitHub Desktop.
GNU Awk (gawk) script to convert an Excel-generated CSV file to a simple XML format
BEGIN {
printf "<?xml version=\"1.0\" encoding=\"US-ASCII\" standalone=\"yes\"?>\n"
printf "<file xmlns=\"http://example.org/csv-to-xml\">\n"
FPAT = "[^,]*|(\"([^\"]|(\"\"))*\")"
RS = "\n"
}
{
printf "<row>\n"
for (i = 1; i <= NF; i++) {
if (match($i, /^"(.*)"$/, array))
$i = array[1]
gsub(/&/, "\\&amp;", $i)
gsub(/</, "\\&lt;", $i)
gsub(/>/, "\\&gt;", $i)
gsub(/""/, "\\&quot;", $i)
gsub(/'/, "\\&apos;", $i)
printf(" <column>%s</column>\n", $i)
}
printf "</row>\n"
}
END {
printf "</file>\n"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment