Extract string from brackets

后端 未结 9 1710
广开言路
广开言路 2020-12-23 16:26

I\'m pretty new at bash so this is a pretty noob question..

Suppose I have a string:

string1 [string2] string3 string4

I would like

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 16:44

    Specify awk multiple delimiters with -F '[delimiters]'

    If the delimiters are square brackets, put them back to back like this ][

    awk -F '[][]' '{print $2}'
    

    otherwise you will have to escape them

    awk -F '[\\[\\]]' '{print $2}'
    

    Other examples to get the value between the brackets:

    echo "string1 (string2) string3" | awk -F '[()]' '{print $2}'
    echo "string1 {string2} string3" | awk -F '[{}]' '{print $2}'
    

提交回复
热议问题