Skip to content

Instantly share code, notes, and snippets.

@alialrahahleh
Last active July 15, 2019 17:29
Show Gist options
  • Save alialrahahleh/d18ef2f862f714e552a6bb75713e3ae2 to your computer and use it in GitHub Desktop.
Save alialrahahleh/d18ef2f862f714e552a6bb75713e3ae2 to your computer and use it in GitHub Desktop.
XML2CSV awk programin, usage: awk -vwth="ELEMENT-TO-CAPTURE" -vsp="," -f xml2cvs.awk XML-FILE-NAME
#Copyright 2019 Ali Mousa
#
#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
#1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
#2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
#3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
BEGIN {
#Implict support for record and line separtor.
RS=">"; FS="</"; isHeaderAlPrnt = 0; wth="ITEM"; pa=0;
}
# Extract Keys or values from assoitive array and print it
# @param list assoitive array to print.
# @param type enum(0, 1) 0 -> keys, 1 -> values
# @global sp seperator for the printed list items.
# @return void
function plist(list , type) {
str = ""
for (x in list) {
k = x;
if (type == 1)
k = list[x];
str = str sprintf("%s%s", k, sp);
}
print substr(str, 0, length(str))
}
# Print header for the first time.
isHeaderAlPrnt == 0 {
if ($0 ~ sprintf("<\/%s$", wth)) {
plist(header, 0);
isHeaderAlPrnt = 1;
}
}
{
if ($0 ~ sprintf("<\/%s$", wth)) {
plist(header, 1);
delete header
}
}
{
# Capture text in between targeted items
# Triggering a global switch on and off
# We can't use ranges because it doesn't support
# text substitution .
if ($0 ~ sprintf("<\/%s$", wth)) {
st=0;
} else if ($0 ~ sprintf("<%s$", wth)) {
st=1;
}
}
st == 1 {
#Only capture element with values.
if ( NF == 2 ) {
# Caputuring elements in assoitive array
header[$2] = $1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment