3

If I have a text-file with a structured list like this:

#linux
##audio
###sequenzer
####qtractor
###drummachine
####hydrogen

##scores ###lilypond ###musescore

##bureau ###kalender ####calcurse ###todo ####tudu

How can I print it tree like to the command-line?

linux/
├── audio
│   ├── drummachine
│   │   └── hydrogen
│   └── sequenzer
│       └── qtractor
├── bureau
│   ├── kalender
│   │   └── calcurse
│   └── todo
│       └── tudu
└── scores
    ├── lilypond
    └── musescore

Is there an application that I'm missing out?

pLumo
  • 22,565
nath
  • 5,694

1 Answers1

3

Use awk to convert the structure to "normal" pathes.

linux/
linux/audio/
linux/audio/sequenzer/
linux/audio/sequenzer/qtractor/
linux/audio/drummachine/
linux/audio/drummachine/hydrogen/
...

Then you can use tree --fromfile . to read it:


convert_structure.awk:

{
    delete path_arr
    path = ""
    level=match($0,/[^#]/)-1
    sub(/^#*/,"")
    p[level]=$0
    for (l=1;l<=level;l++) {
        path_arr[l]=p[l]
        path = path p[l] "/"
    }
    print path
}

RUN:

awk -f convert_structure.awk structure.txt | tree --fromfile . --noreport

OUTPUT:

.
└── linux
    ├── audio
    │   ├── drummachine
    │   │   └── hydrogen
    │   └── sequenzer
    │       └── qtractor
    ├── bureau
    │   ├── kalender
    │   │   └── calcurse
    │   └── todo
    │       └── tudu
    └── scores
        ├── lilypond
        └── musescore

Notes:

  • Check here if your implementation of awk does not support delete of an array.

  • This works fine with pathes that include spaces, but obviously won't work with pathes inlcuding newlines.

pLumo
  • 22,565
  • You can always use split("", path_arr) if delete path_arr is not supported (delete is standard, it's passing it an array as a whole that used not to be). – Stéphane Chazelas Feb 04 '22 at 10:05
  • I'm on cygwin where tree --version outputs tree v1.7.0 (c) 1996 - 2014 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro and it doesn't have a --fromfile option (or any other that'd let it take such input as far as I can see). What tree version are you using? – Ed Morton Feb 04 '22 at 23:14
  • -> tree v1.8.0 – pLumo Feb 05 '22 at 08:32
  • This goes a bit beyond the original question, but is there a way of keeping the order of the scructered-list rather then getting an alphabetical order as if it was a directory-list? – nath Feb 10 '22 at 03:12