Picking out a part of a string

A limited use-case, but in case you get to the situation where you need the following bold part of a string:

/home/*/public_html/git/git.git

(The asterisk matching any word)

And here’s the slightly messy solution I used; in this case, I needed the first three directories of the path the script was running in:

fullpath=`pwd`
pubpath=`echo $fullpath | rev | cut -c13- | rev`

So I’ve reversed it so I know the exact number of characters from the start of the string that I need, used cut to select part of the string; from character 13 to the end (reversed form of /home/*/public_html), then reserved it back again.

Leave a Reply

Your email address will not be published. Required fields are marked *