Say I have the path gui/site/junior/profile.py
How do I get this?:
gui
gui/site
gui/site/junior
Bonus if you tell me how to loop through each path :D
Say I have the path gui/site/junior/profile.py
How do I get this?:
gui
gui/site
gui/site/junior
Bonus if you tell me how to loop through each path :D
You can loop with awk:
See as one liner:
This takes advantage of
NF
, which counts how many fields the current record has. Based on that, it loops from the first up to the last one, printing every time first up to that value.a dash answer:
You can use the shell's built-in splitting facilities.
IFS
specifies what to split on.This could be refactored in many ways, but I want the change to
IFS
to only govern the actual splitting.The use of
set
to split a string into positional parameters is slightly obscure, but well worth knowing.You should properly take care to unset
IFS
if it was originally unset, but I'm skimping on that.Perl variant:
produces
if you have NULL separates pathnames, you can add
0
to arguments:e.g from
produces
For iterating over the paths, you can use the next:
it produces: