Search code examples
bashshellloopssedcut

Iterate over string without space as seperator in bash-shell


I'm trying to parse some output of xmllint since hours, but I can't get it to work like i would need it.

Output of "xmllint --xpath "//fub/@name" menu.xml"*

name="Kessel" name="Lager" name="Puffer " name="Boiler.sen" name="Boiler.jun" name="HK Senior" name="HK Junior" name="Fbh" name="Solar" name="F.Wärme" name="Sys "

Now I need to seperate all the names (inclusive spaces) and get them in to seperate variables. My approach was this:

fubNames=$(xmllint --xpath "//fub/@name" menu.xml | sed 's/name=//g')
for name in $fubNames
do
    echo $name
done

but this does not workout because the for-loop seperates the string on spaces. i need the names with spaces. (note: some names have a space at the end)

Does anyone know how to do this properly?


Solution

  • I suggest:

    xmllint --xpath "//fub/@name" menu.xml | grep -o '"[^"]*"' | while IFS= read -r name; do echo "$name"; done