<div dir="ltr"><div dir="ltr">On Mon, 4 Nov 2019 at 18:36, William Witteman <<a href="mailto:wwitteman@gmail.com">wwitteman@gmail.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">No sed knowledge here, but what if you turn it around, and grab two characters and conditionally truncate, rather than the other way around?<div dir="auto"><br></div><div dir="auto">My only useful programming advice is when you get stuck, turn your problem around.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon., Nov. 4, 2019, 18:31 Giles Orr via talk, <<a href="mailto:talk@gtalug.org" target="_blank">talk@gtalug.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>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.</div><div><br></div><div>We have the directory we're in, for example:</div><div><br></div><div>    /Users/gorr/.bashprompt/really/deep/directory/structure/even/deeper</div><div><br></div><div>(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 ...)</div><div><br></div><div>I want to shorten the directory name to just the first letters:<br></div><div><br></div><div>    export newPWD=$(echo ${PWD} | sed -e "s@${HOME}@~@" -e 's@\(/.\)[^/]*@\1@g')</div><div>    echo $newPWD</div><div>    ~/./r/d/d/s/e/d</div><div><br></div><div>This does '~' replacement for $HOME and then substitutes the first letter of each directory for the full directory name.</div><div><br></div><div>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:</div><div><br></div><div>    ~/.b/r/d/d/s/e/d</div><div><br></div><div>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.</div></div></blockquote></div></blockquote><div><br></div><div>All that was needed was a walk home to think and solve it:</div><div><br></div><div>     export newPWD=$(echo ${PWD} | sed -e "s@${HOME}@~@" -e 's@\(/[.].\|/.\)[^/]*@\1@g') ; echo $newPWD<br>    ~/.b/r/d/d/s/e/d<br></div></div><div><br></div><div>It's a regex problem: capture a group that's a slash, a dot, and a character, OR a slash and a character.<br></div><div><br></div>-- <br><div dir="ltr" class="gmail_signature">Giles<br><a href="https://www.gilesorr.com/" target="_blank">https://www.gilesorr.com/</a><br><a href="mailto:gilesorr@gmail.com" target="_blank">gilesorr@gmail.com</a></div></div>