I want to get the tail from a given string which is generated by pwd command.
For example, if pwd command returns:
/home/example/src/github.com/abc/def
And I want to get string from github.com to end.
github.com/abc/def
You can use parameter expansion in bash to achieve what you are looking for. Assuming you have the path as mentioned in PWD variable(same as output of $(pwd)), you can use the syntax {str##*} syntax to get the shortest string from the occurrence of the de-limiter to end of the string. In this case we use multi-character delimiter as github.com. Just do
printf 'github.com%s' "${PWD##*github.com}"
To store it in a new path just use the -v syntax provided by printf
printf -v newvar 'github.com%s' "${PWD##*github.com}"
printf '%s\n' "$newvar"
github.com/abc/def
or much simply use without printf just as
newvar="github.com${PWD##*github.com}"
Yet another solution, with grep:
pwd | grep -Eo 'github.com.*'
Basically: Take pwd and only print out the bit including and after github.com
Or, even if you're in a subdirectory of github.com/// and only want the two bits (author and repo name) after the github.com:
pwd | grep -Eo 'github.com/([^/]+/?){2}'
which pulls exactly 2 segments after the github.com
You can use sed to get the rest of the string and then combine it with 'github.com' using xargs to get the desired string:
pwd | sed 's/^.*github\.com//' | xargs -I {} echo "github.com{}"
Output:
github.com/abc/def
If your goal is to start printing content with ANY directory segment that contains a period, you could do the following with awk:
$ awk 'BEGIN{ORS=RS="/"} /\./ {n=1} n' <<< "/home/example/src/github.com/abc/def"
github.com/abc/def
Of course, the same results could be achieved with sed
sed 's/.*[^a-z]\([a-z][a-z]*\..*\)/\1/'
Or grep alone:
egrep -o '[a-z]+\..*'