[GTALUG] Hardcore sed foo

William Witteman wwitteman at gmail.com
Mon Nov 4 18:36:44 EST 2019


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, <talk at gtalug.org> 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 at 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.
>
> --
> Giles
> https://www.gilesorr.com/
> gilesorr at gmail.com
> ---
> Post to this mailing list talk at gtalug.org
> Unsubscribe from this mailing list
> https://gtalug.org/mailman/listinfo/talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://gtalug.org/pipermail/talk/attachments/20191104/064cc6e3/attachment.html>


More information about the talk mailing list