I need to extract the string after the :
in an example below:
package:project.abc.def
Where i would get project.abc.def
as a result.
I am attempting this in bash and i believe i have a regular expression that will work :([^:]*)$
.
In my bash script i have package:project.abc.def
as a variable called apk
. Now how do i assign the same variable the substring found with the regular expression?
Where the result from package:project.abc.def
would be in the apk
variable. And package:project.abc.def
is initially in the apk
variable?
Thanks!
There is no need for a regex here, just a simple prefix substitution:
$ apk="package:project.abc.def"
$ apk=${apk##package:}
project.abc.def
The ## syntax is one of bash's parameters expansions. Instead of #, % can be used to trim the end. See this section of the bash man page for the details.
Some alternatives:
$ apk=$(echo $apk | awk -F'package:' '{print $2}')
$ apk=$(echo $apk | sed 's/^package://')
$ apk=$(echo $apk | cut -d':' -f2)
$ string="package:project.abc.def"
$ apk=$(echo $string | sed 's/.*\://')
".*:" matches everything before and including ':' and then its removed from the string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With