Last active
August 29, 2015 14:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(/&/, "\\&", $i) | |
gsub(/</, "\\<", $i) | |
gsub(/>/, "\\>", $i) | |
gsub(/""/, "\\"", $i) | |
gsub(/'/, "\\'", $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