[GTALUG] Hardcore sed foo

Giles Orr gilesorr at gmail.com
Mon Nov 4 18:31:37 EST 2019


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://gtalug.org/pipermail/talk/attachments/20191104/aa8e8e53/attachment.html>


More information about the talk mailing list