|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I have to display the data from a file in tree structure(ksh). The file format is as follows(first field parent, second field child) : PM01 PM02 PM01 PM1A PM02 PM03 PM03 PM04 PM04 PM05 PM04 PM06 PM1A PM1B PM1A PM1C The output should be like this : PM01 -- PM02 -- PM03 -- PM04 -- PM05 PM06 PM01 PM1A -- PM1B PM1C Anybody have idea ? Regards, Thobias |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
dear, u can have this out of display by having a simple bash script.
On Mar 26, 4:06pm, "Vakayil Thobias" <vakayil.thob...@alcatel- lucent.com> wrote: > I have to display the data from a file in tree structure(ksh). > The file format is as follows(first field parent, second field child) : > > PM01 PM02 > PM01 PM1A > PM02 PM03 > PM03 PM04 > PM04 PM05 > PM04 PM06 > PM1A PM1B > PM1A PM1C > > The output should be like this : > PM01 -- PM02 -- PM03 -- PM04 -- PM05 > PM06 > PM01 PM1A -- PM1B > PM1C > > Anybody have idea ? > > Regards, > Thobias |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Vakayil Thobias wrote:
> I have to display the data from a file in tree structure(ksh). > The file format is as follows(first field parent, second field child) : > > PM01 PM02 > PM01 PM1A > PM02 PM03 > PM03 PM04 > PM04 PM05 > PM04 PM06 > PM1A PM1B > PM1A PM1C > > The output should be like this : > PM01 -- PM02 -- PM03 -- PM04 -- PM05 > PM06 > PM01 PM1A -- PM1B > PM1C > The above "output" doesn't seem to have a structure that is anyhow related to a tree. > Anybody have idea ? > > Regards, > Thobias > > |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Janis Papanagnou wrote:
> Vakayil Thobias wrote: >> I have to display the data from a file in tree structure(ksh). >> The file format is as follows(first field parent, second field child) : >> >> PM01 PM02 >> PM01 PM1A >> PM02 PM03 >> PM03 PM04 >> PM04 PM05 >> PM04 PM06 >> PM1A PM1B >> PM1A PM1C >> >> The output should be like this : >> PM01 -- PM02 -- PM03 -- PM04 -- PM05 >> PM06 >> PM01 PM1A -- PM1B >> PM1C >> > > The above "output" doesn't seem to have a structure that is > anyhow related to a tree. I think that should have been perhaps PM01 -- PM02 -- PM03 -- PM04 -- PM05 + -- PM06 PM01 -- PM1A -- PM1B + -- PM1C Or some variation thereof. -- All the commands are tested with bash and GNU tools, so they may use nonstandard features. I try to mention when something is nonstandard (if I'm aware of that), but I may miss something. Corrections are welcome. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 3/26/2008 6:06 AM, Vakayil Thobias wrote:
> I have to display the data from a file in tree structure(ksh). > The file format is as follows(first field parent, second field child) : > > PM01 PM02 > PM01 PM1A > PM02 PM03 > PM03 PM04 > PM04 PM05 > PM04 PM06 > PM1A PM1B > PM1A PM1C > > The output should be like this : > PM01 -- PM02 -- PM03 -- PM04 -- PM05 > PM06 > PM01 PM1A -- PM1B > PM1C > > Anybody have idea ? Here's a start: $ cat file PM01 PM02 PM01 PM1A PM02 PM03 PM03 PM04 PM04 PM05 PM04 PM06 PM1A PM1B PM1A PM1C $ cat dt.awk function descend(indent,branch, child,childA) { printf "%"indent++"s%s\n","",branch split(children[branch],childA) for (child in childA) descend(indent,childA[child]) } { branches[$1] branches[$2] children[$1] = children[$1] FS $2 parent[$2] = $1 } END { for (branch in branches) if (!(branch in parent)) { descend(0,branch) delete branches[branch] } print "-----" for (branch in branches) descend(0,branch) } $ awk -f dt.awk file PM01 PM02 PM03 PM04 PM05 PM06 PM1A PM1B PM1C ----- PM05 PM1A PM1B PM1C PM1B PM06 PM1C PM02 PM03 PM04 PM05 PM06 PM03 PM04 PM05 PM06 PM04 PM05 PM06 Regards, Ed. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"Janis Papanagnou" <Janis_Papanag...@hotmail.com> wrote:
> Vakayil Thobias wrote: > > ... > The above "output" doesn't seem to have a structure that is > anyhow related to a tree. Indeed. How can we appropriately answer to your issue if it is not well formed? "A problem, properly stated, is a problem on it's way to being solved" -- Buckminster Fuller That being said, your problem looks a lot like an application of tsort (RTFM) and some awk/sed post-processing. Or, just maybe: "decision_tree_hierarchy.c++" http://atlas.kennesaw.edu/~rbentley/...erarchy.tar.gz If we're solving a homework problem, presumably the former avenue of approach will do best. =Brian |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
"Ed Morton" <morton@lsupcaemnt.com> wrote in message news:47EA5FE9.5000800@lsupcaemnt.com... > On 3/26/2008 6:06 AM, Vakayil Thobias wrote: >> I have to display the data from a file in tree structure(ksh). >> The file format is as follows(first field parent, second field child) : >> >> PM01 PM02 >> PM01 PM1A >> PM02 PM03 >> PM03 PM04 >> PM04 PM05 >> PM04 PM06 >> PM1A PM1B >> PM1A PM1C >> >> The output should be like this : >> PM01 -- PM02 -- PM03 -- PM04 -- PM05 >> PM06 >> PM01 PM1A -- PM1B >> PM1C >> >> Anybody have idea ? > > Here's a start: > > $ cat file > PM01 PM02 > PM01 PM1A > PM02 PM03 > PM03 PM04 > PM04 PM05 > PM04 PM06 > PM1A PM1B > PM1A PM1C > > $ cat dt.awk > function descend(indent,branch, child,childA) { > printf "%"indent++"s%s\n","",branch > split(children[branch],childA) > for (child in childA) > descend(indent,childA[child]) > } > { > branches[$1] > branches[$2] > children[$1] = children[$1] FS $2 > parent[$2] = $1 > } > END { > for (branch in branches) > if (!(branch in parent)) { > descend(0,branch) > delete branches[branch] > } > print "-----" > for (branch in branches) > descend(0,branch) > } > > $ awk -f dt.awk file > PM01 > PM02 > PM03 > PM04 > PM05 > PM06 > PM1A > PM1B > PM1C > ----- > PM05 > PM1A > PM1B > PM1C > PM1B > PM06 > PM1C > PM02 > PM03 > PM04 > PM05 > PM06 > PM03 > PM04 > PM05 > PM06 > PM04 > PM05 > PM06 > > Regards, > > Ed. > > Hello Ed Morton, Thanks for your reply. It's working fine. Great work. Regards, Thobias |
|
![]() |
| Outils de la discussion | |
|
|