Search code examples
stringbashextractcut

Extracting 2 parts of a String


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.


Solution

  • 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.