On Mon, 4 Nov 2019 at 18:36, William Witteman <[email protected]> wrote:
> No sed knowledge here, but what if you turn it around, and grab two > characters and conditionally truncate, rather than the other way around? > > My only useful programming advice is when you get stuck, turn your problem > around. > > On Mon., Nov. 4, 2019, 18:31 Giles Orr via talk, <[email protected]> wrote: > >> I've tinkered with Bash prompts for a lot of years. That's where this >> problem originates, but it can be considered just as a thought experiment >> if you prefer. >> >> We have the directory we're in, for example: >> >> /Users/gorr/.bashprompt/really/deep/directory/structure/even/deeper >> >> (I keep my prompts in ~/.bashprompt/ , and a very long directory name is >> often a cause of breakage, so I keep one around to break them ...) >> >> I want to shorten the directory name to just the first letters: >> >> export newPWD=$(echo ${PWD} | sed -e "s@${HOME}@~@" -e 's@ >> \(/.\)[^/]*@\1@g') >> echo $newPWD >> ~/./r/d/d/s/e/d >> >> This does '~' replacement for $HOME and then substitutes the first letter >> of each directory for the full directory name. >> >> Here's the question: if the first letter of the directory name is a dot >> '.', can sed then capture one character more so that the output would >> become: >> >> ~/.b/r/d/d/s/e/d >> >> I think this would be pretty easy with Bash and a loop, but that's a lot >> of processing so I'd rather not go down that road. I suspect sed is >> capable of this, but I haven't delved deeply enough into the tool to even >> know where to start. This may in fact be a regex problem more than a sed >> problem - either way I'm kind of stumped. I'm open to simpler >> implementations using other (standard system) tools as well. >> > All that was needed was a walk home to think and solve it: export newPWD=$(echo ${PWD} | sed -e "s@${HOME}@~@" -e 's@ \(/[.].\|/.\)[^/]*@\1@g') ; echo $newPWD ~/.b/r/d/d/s/e/d It's a regex problem: capture a group that's a slash, a dot, and a character, OR a slash and a character. -- Giles https://www.gilesorr.com/ [email protected]
--- Post to this mailing list [email protected] Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
