Reputation: 55
I've got a String which contains the Output of a command which looks like this:
max. bit rate: ('2.5 MBit/s', '16.7 MBit/s')
Now I need to extract the "2.5 MBit/s" and the "16.7 MBit/s" in two seperate strings.
The language is bash.
Upvotes: 1
Views: 51
Reputation: 208107
Like this in bash
without starting any extra external processes:
yourString="max. bit rate: ('2.5 MBit/s', '16.7 MBit/s')"
IFS="'" read _ rate1 _ rate2 _ <<< "$yourString"
echo $rate1
2.5 MBit/s
echo $rate2
16.7 MBit/s
I am setting the IFS (Input Field Separator) to a single quote, then doing a read
with unwanted fields going into a dummy (unused) variable called _
.
Upvotes: 2
Reputation: 88999
With regex:
x="max. bit rate: ('2.5 MBit/s', '16.7 MBit/s')"
[[ $x =~ .*\'(.*)\'.*\'(.*)\'.* ]] && echo "${BASH_REMATCH[1]} ${BASH_REMATCH[2]}"
Output:
2.5 MBit/s 16.7 MBit/s
Upvotes: 1
Reputation: 50308
with awk:
string1=$(echo "max. bit rate: ('2.5 MBit/s', '16.7 MBit/s')" | awk -F"'" '{print $2}')
string2=$(echo "max. bit rate: ('2.5 MBit/s', '16.7 MBit/s')" | awk -F"'" '{print $4}')
with cut:
string1=$(echo "max. bit rate: ('2.5 MBit/s', '16.7 MBit/s')" | cut -d"'" -f2)
string2=$(echo "max. bit rate: ('2.5 MBit/s', '16.7 MBit/s')" | cut -d"'" -f4)
Either way we are just splitting the string by a single quote and grabbing the 2nd and 4th fields.
Upvotes: 2