I have the following string
git#bitbucket.org:user/my-repo-name.git
I want to extract this part
my-repo-name
With bash:
s='git#bitbucket.org:user/my-repo-name.git'
[[ $s =~ ^.*/(.*)\.git$ ]]
echo ${BASH_REMATCH[1]}
Output:
my-repo-name
Another method, using bash's variable substitution:
s='git#bitbucket.org:user/my-repo-name.git'
s1=${s#*/}
echo ${s1%.git}
Output:
my-repo-name
I'm not sure if there's a way to combine the # and % operators into a single substitution.
Related
This is how my input string looks like:
INPUT_STRING="{/p1/p2=grabthistext}"
I want to print grabthistext from the INPUT_STRING.
I tried echo "${INPUT_STRING##*=}" which prints grabthistext}
How do I read only grabthistext using parameter expansion expression?
If you really want a single parameter expansion then you can use:
#!/bin/bash
shopt -s extglob
INPUT_STRING="{/p1/p2=grabthistext}"
echo "${INPUT_STRING//#(*=|\})}"
grabthistext
I would use a bash regex though:
#!/bin/bash
INPUT_STRING="{/p1/p2=grabthistext}"
[[ $INPUT_STRING =~ =(.*)} ]] && echo "${BASH_REMATCH[1]}"
grabthistext
temp="${INPUT_STRING##*=}"
echo "${temp%\}}"
grabthistext
You can do it in two steps: first extract the fragment after = as you already did, and store it in a new variable. Then use the same technique to remove the undesired } suffix:
INPUT_STRING="{/p1/p2=grabthistext}"
TEMP_STRING=${INPUT_STRING##*=}
OUTPUT_STRING=${TEMP_STRING%\}}
echo "$OUTPUT_STRING"
# grabthistext
Check it online.
I have this string:
package: name='my.package.name versionCode='221013140' versionName='00.00.05' platformBuildVersionName='12' platformBuildVersionCode='32' compileSdkVersion='32' compileSdkVersionCodename='12'
Using bash, how can I get this value?
00.00.05
Use parameter expansion:
#!/bin/bash
string="package: name='my.package.name versionCode='221013140' versionName='00.00.05' platformBuildVersionName='12' platformBuildVersionCode='32' compileSdkVersion='32' compileSdkVersionCodename='12'"
version=${string#* versionName=\'} # Remove everything up to the version name.
version=${version%%\'*} # Remove everything after the first quote.
echo "$version"
You can use bash regex matching operator (=~):
[[ $string =~ versionName=\'([^\']*)\' ]] && echo "${BASH_REMATCH[1]}"
The initial string is RU="903B/100ms"
from which I wish to obtain B/100ms.
Currently, I have written:
#!/bin/bash
RU="903B/100ms"
RU=${RU#*[^0-9]}
echo $RU
which returns /100ms since the parameter expansion removes up to and including the first non-numeric character. I would like to keep the first non-numeric character in this case. How would I do this by amending the above text?
You can use BASH_REMATCH to extract the desired matching value:
$ RU="903B/100ms"
$ [[ $RU =~ ^([[:digit:]]+)(.*) ]] && echo ${BASH_REMATCH[2]}
B/100ms
Or just catch the desired part as:
$ [[ $RU =~ ^[[:digit:]]+(.*) ]] && echo ${BASH_REMATCH[1]}
B/100ms
Assuming shopt -s extglob:
RU="${RU##+([0-9])}"
echo "903B/100ms" | sed 's/^[0-9]*//g'
B/100ms
How can I find if a pattern has occurred in a variable in if condition.
Eg:
var1="DEFABCTY"
var2="EFGH"
How can I use if in shell script to find which of the two variables has "ABC" using if statement.
You can use pattern matching and variable indirection:
for var in var1 var2 ; do
if [[ ${!var} = *ABC* ]] ; then
echo ABC occurs in $var
fi
done
I want to extract substring till the point the last numeric ends.
for example:
In the string "abcd123z" , I want the output to be "abcd123"
In the string "abcdef123gh01yz" , I want the output to be "abcdef123gh01"
In the string "abcd123" , I want the output to be "abcd123"
How to do this in the unix shell?
Try this sed command,
sed 's/^\(.*[0-9]\).*$/\1/g' file
Example:
$ echo 'abcdef123gh01yz' | sed 's/^\(.*[0-9]\).*$/\1/g'
abcdef123gh01
You can do this in BASH regex:
str='abcdef123gh01yz'
[[ "$str" =~ ^(.*[[:digit:]]) ]] && echo "${BASH_REMATCH[1]}"
abcdef123gh01
tmp="${str##*[0-9]}" # cut off all up to last digit, keep intermediate
echo "${str%$tmp}" # remove intermediate from end of string