[LOADBALANCER]@ And need to extract from this string type of equipment" /> [LOADBALANCER]@ And need to extract from this string type of equipment" /> [LOADBALANCER]@ And need to extract from this string type of equipment"/>

Bash Cut text from line with different delimiters

757 Views Asked by At

I have a variable with value like:

@capability_ids type="list">[LOADBALANCER]</capability_ids>@

And need to extract from this string type of equipment ( LOADBALANCER ).

I've tried to use cut, but don't know how write cut command with different delimiters.

DeviceType=$( echo $DeviceTypeDirty | cut -d'[' -f1)

Can enywone help me with right solution on bash?

3

There are 3 best solutions below

0
Ronak Patel On BEST ANSWER

use awk with regular expression: awk -F '[\\[\\]]' '{print $2}'

$ echo '@capability_ids type="list">[L3SWITCH]/capability_ids>@'|awk -F '[\\[\\]]' '{print $2}'
$ L3SWITCH

$ DeviceType=$( echo "$DeviceTypeDirty" | awk -F '[\\[\\]]' '{print $2}')
1
anudeep On

I tried and got to extract "LOADBALANCER"

Administrators-MacBook-Pro:~$ echo "\"list\">[LOADBALANCER]   
</capability_ids>@"|awk -F '[][]' '{print $2}'
LOADBALANCER
Administrators-MacBook-Pro:~$ 

Hope that helps!

0
Jamil Said On

Using cut:

DeviceTypeDirty="@capability_ids type="list">[LOADBALANCER]</capability_ids>@"
DeviceType="$(echo "$DeviceTypeDirty" | cut -d'[' -f2 | cut -d']' -f1)"

Output:

echo "$DeviceType"
LOADBALANCER