I'm really looking for a generic solution here, but for the sake of example, let's say I'm looking for the deepest 2 directories in a path. e.g.: Given a path /home/someone/foo/bar
, how can I use sed (or awk) to get foo/bar
It would be simple if the first part of the path were always the same, but in my case, I'm looking for an expression that will work for any arbitrary path, and ideally, for any depth n
.
If the path has fewer than n
(in this case 2) levels, it's not important how the command behaves.
I went do this today and found it tricky. I started to write a loop to do it instead, but I can't shake the feeling that this must be possible in a single command. I could be wrong. Any ideas?