Extract string from brackets

后端 未结 9 1668
广开言路
广开言路 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:34

    Here's one way using awk:

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

    This sed option also works:

    echo "string1 [string2] string3 string4" | sed 's/.*\[\([^]]*\)\].*/\1/g'
    

    Here's a breakdown of the sed command:

    s/          <-- this means it should perform a substitution
    .*          <-- this means match zero or more characters
    \[          <-- this means match a literal [ character
    \(          <-- this starts saving the pattern for later use
    [^]]*       <-- this means match any character that is not a [ character
                    the outer [ and ] signify that this is a character class
                    having the ^ character as the first character in the class means "not"
    \)          <-- this closes the saving of the pattern match for later use
    \]          <-- this means match a literal ] character
    .*          <-- this means match zero or more characters
    /\1         <-- this means replace everything matched with the first saved pattern
                    (the match between "\(" and "\)" )
    /g          <-- this means the substitution is global (all occurrences on the line)
    
    0 讨论(0)
  • 2020-12-23 16:34

    Here is an awk example, but I'm matching on parenthesis which also makes it more obvious of how the -F works.

    echo 'test (lskdjf)' | awk -F'[()]' '{print $2}'

    0 讨论(0)
  • 2020-12-23 16:37

    Here's another one , but it takes care of multiple occurrences, eg

    $ echo "string1 [string2] string3 [string4 string5]" | awk -vRS="]" -vFS="[" '{print $2}'
    string2
    string4 string5
    

    The simple logic is this, you split on "]" and go through the split words finding a "[", then split on "[" to get the first field. In Python

    for item in "string1 [string2] string3 [string4 string5]".split("]"):
        if "[" in item:
           print item.split("]")[-1]
    
    0 讨论(0)
  • 2020-12-23 16:39

    Try this:

    echo $str | cut -d "[" -f2 | cut -d "]" -f1
    
    0 讨论(0)
  • 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}'
    
    0 讨论(0)
  • 2020-12-23 16:47
    Read file in which the delimiter is square brackets:
    $ cat file
    123;abc[202];124
    125;abc[203];124
    127;abc[204];124
    
    To print the value present within the brackets:
    $ awk -F '[][]' '{print $2}' file
    202
    203
    204
    

    At the first sight, the delimiter used in the above command might be confusing. Its simple. 2 delimiters are to be used in this case: One is [ and the other is ]. Since the delimiters itself is square brackets which is to be placed within the square brackets, it looks tricky at the first instance.

    Note: If square brackets are delimiters, it should be put in this way only, meaning first ] followed by [. Using the delimiter like -F '[[]]' will give a different interpretation altogether.

    Refer this link: http://www.theunixschool.com/2012/07/awk-10-examples-to-read-files-with.html

    0 讨论(0)
提交回复
热议问题