Reputation: 6269
Example strings:
app:mob-type1-subtype:1.0.2
app:mob-type2:1.0.1
Above are my example strings, I want to fetch "type" value from above string.
How can I do it using bash script?
I have tried the following
Code:-
res="${element#*-}"
echo $res| awk -F : '{ print $1 }'
Result (Incorrect one)
type1
type2-subtype
But I want to get type1 and type2 values only not subtypes. How can I do it in a better way?
Upvotes: 1
Views: 2518
Reputation: 101
Here is a solution, considering your text is in a.txt
cat a.txt | grep -Eo "type[0-9]+"
It will give the output as below:
type1
type2
Upvotes: 0
Reputation: 784958
cat file
app:mob-type1-subtype:1.0.2
app:mob-type2:1.0.1
Using awk
you can do:
awk -F '[:-]' '{print $3}' file
type1
type2
Here is one bash
solution if you're have input in a shell variable:
IFS='[:-]' read -ra arr <<< 'app:mob-type1-subtype:1.0.2'
echo "${arr[2]}"
type1
Upvotes: 3