Extract string from brackets

Try this:

echo $str | cut -d "[" -f2 | cut -d "]" -f1

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)

Tags:

Bash

Sed