Last active
July 27, 2022 03:49
-
-
Save mattmc3/f4c2e415378d980914b6c5ba19ecfcaf to your computer and use it in GitHub Desktop.
Bash: split string on delimiter and echo substring before/after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
giturls=( | |
mattmc3/antidote | |
git@github.com:mattmc3/antidote.git | |
https://github.com/mattmc3/antidote | |
) | |
for url in $giturls; do | |
url=${url%.git} | |
url=${url:gs/\:/\/} | |
user=${${url%/*}##*/} | |
repo=${url##*/} | |
echo $url $user/$repo | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://tldp.org/LDP/abs/html/string-manipulation.html | |
function split_repo() { | |
local gitpath="${1:-github.com/mattmc3/myrepo}" | |
echo "\$gitpath = $gitpath" | |
echo "repo part \${gitpath##*/} = ${gitpath##*/}" | |
echo "user/repo \${gitpath#*/} = ${gitpath#*/}" | |
echo "url \${gitpath%%/*} = ${gitpath%%/*}" | |
echo "giturl/user \${gitpath%/*} = ${gitpath%/*}" | |
echo "user \${\${gitpath%/*}#*/} = ${${gitpath%/*}#*/}" | |
} | |
function split_repo2() { | |
local gitpath="$1" | |
local giturl="bbgithub.com" | |
local repo="${gitpath##*/}" | |
local usernm="${gitpath%/*}" | |
if [[ "$usernm" == */* ]]; then | |
giturl="${usernm%/*}" | |
usernm="${usernm#*/}" | |
fi | |
} | |
split_repo "bitbucket.org/joeuser/myrepo" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# split on the slash | |
value=start/end | |
echo start ${value%/*} | |
echo end ${value#*/} | |
# split on the colon | |
value=start:end | |
echo start ${value%:*} | |
echo end ${value#*:} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment