Reputation: 479
I have a var $ENTRIES
ENTRIES="<entry key="key-one">value</entry> <entry key="key-two">value/two</entry>"
Which needs to be converted to csv:
convertToCSV() {
# Do stuff with $ENTRIES
} >> dict.csv
Resulting in:
cat dict.csv
key-one,value
key-two,value/two
How would we extract the key and values from $ENTRIES? Would say we would need some kind of sed command? Where we take the key between:
key=" and ">
And take the value between:
> and </entry>
And somehow loop through all the entries in $ENTRIES...
Upvotes: 1
Views: 281
Reputation: 52888
Another option is to use XPath with xmlstarlet
...
ENTRIES='<entry key="key-one">value</entry> <entry key="key-two">value/two</entry>'
echo "<doc>$ENTRIES</doc>" | \
xmlstarlet sel -t \
-m "/doc/entry" \
-v "concat(@key,',',normalize-space())" \
-n \
> dict.csv
Notes:
$ENTRIES
variable with another element (doc
) to make the XML well-formed.ENTRIES
variable by removing the $
and changing the outer quotes to apostrophes.Upvotes: 0
Reputation: 133750
try following once and let me know if this helps you. Once you are happy with results you could re-direct this value to .csv output file then.
Solution 1st:
echo "$ENTRIES" | awk -v RS=" " '{sub(/.*=/,"");sub(/<.*/,"");sub(/>/,",");if($0){print}}'
Solution 2nd: Adding one more solution here too.
ENTRIES="<entry key=\"key-one\">value</entry> <entry key=\"key-two\">value/two</entry>"
echo "$ENTRIES" | awk -v RS='<entry key=|</entry>' 'NF{sub(/>/,",");gsub(/\"/,"");print}'
key-one,value
key-two,value/two
Upvotes: 1