Reputation: 5780
I have a string like this : 'MDP_44' => 'sdDSD4343khjkjhkjhjk',
I want to run a function which return only: MDP_44
.
I tried things like :
$ variable= 'MDP_44' => 'sdDSD4343khjkjhkjhjk',
$ echo ${var1//[^A-Z]} // MDPDSD
But the result is not good.
Can someone help me to get MDP_44
as the result ? Many thanks in advance.
Upvotes: 0
Views: 76
Reputation: 247210
using bash regular expressions to find the text between the first set of single quotes.
if [[ "$variable" =~ [\']([^\']+) ]]; then match=${BASH_REMATCH[1]}; fi
Upvotes: 0
Reputation: 7107
if your variable is:
variable="'MDP_44' => 'sdDSD4343khjkjhkjhjk',"
then you can use bash extended pattern matching:
echo ${variable//?(\'| =>*)}
Note you need to enable extglob by shopt -s extglob
, it's enabled by default in interactive mode.
Upvotes: 1
Reputation: 11087
This will extract the first thing in '
...'
echo "$variable" | cut -d\' -f2
Upvotes: 0
Reputation:
var="'MDP_44' => 'sdDSD4343khjkjhkjhjk',"
echo $var | cut -d= -f1 | sed "s/'//g"
EDIT
or even shorter
echo $var | cut -d\' -f2
Upvotes: 1