Quantcast
Channel: Generate tree output from specific/general XML file in Bash - Ask Ubuntu
Viewing all articles
Browse latest Browse all 3

Generate tree output from specific/general XML file in Bash

$
0
0

I am trying to generate a tree from an XML file in Bash.

This is a part of the XML file:

<menu name="main_menu" display="Main Menu"><application name="load_profiles" display="Load Profile"/><application name="save_profiles" display="Save Profile"/><application name="remove_profiles" display="Delete Profile"/></menu>

I have tried to use CAT and GREP and AWK:

cat menu.xml | grep menu\ name | awk -v FS="(display=\"|\" help)" '{print $2}'> menulist.txt

I have first GREPed using the lines that have "Menu Name" and then printed the Tests between 'display="' and '" help' and came out with this output:

Main Menu">BroadbandLoad and Save ProfilesxDSL Interface

But what I want is to Grep all the lines that have "Menu Name", "parameter type", "application name" and "value id" and print their display name in a tree like output. I am not sure how I can Grep multiple values from multiple lines and print a specific string from it.

Then I have seen that it is comparatively easier to do this with a XML parser tool. So I have tried with XMLStarlet:

xmlstarlet el menu.xml | awk -F'/''BEGIN{print "digraph{"}{print $(NF-1)" -> "$NF}END{print"}"}'> menumenutxt.txt

Using this command I have found the following output:

menu -> menumenu -> onentermenu -> menumenu -> applicationmenu -> applicationmenu -> applicationmenu -> parameterparameter -> valueparameter -> value

Which definitely looks better and closer to what I want. But it's not printing the display name.

What I am trying to print is something like this:

 Main Menu ->            -> Broadband                         -> Load and Save Profiles                                                  -> Load Profile                                                  -> Save Profile                                                  -> Delete Profile

Or the following:

Main Menu -> Broadband --> Load and Save Profiles---> Load Profile---> Save Profile---> Delete Profile

My aim to get an output as close to it as possible. Can anyone suggest me how I should proceed with this?


Viewing all articles
Browse latest Browse all 3

Trending Articles