I have example string like
Test "checkin_resumestorevisit - Online_V2.mt" Run
and i want to extract the text between the two quotes in bash. I tried using command like
SUBSTRING=${echo $SUBSTRING| cut -d'"' -f 1 }
but it fails with error: bad substitution
.
Using the cut Command Specifying the character index isn't the only way to extract a substring. You can also use the -d and -f flags to extract a string by specifying characters to split on. The -d flag lets you specify the delimiter to split on while -f lets you choose which substring of the split to choose.
' appearing in double quotes is escaped using a backslash. The backslash preceding the ' ! ' is not removed. The special parameters ' * ' and ' @ ' have special meaning when in double quotes (see Shell Parameter Expansion).
For example string="single quote: ' double quote: "'"'" end of string" . This works because quoted strings next to each other are considered a single string and you can switch between single and double quote strings within those parts.
If you want to extract content between the first "
and the last "
:
s='Test "checkin_resumestorevisit - Online_V2.mt" Run'
s=${s#*'"'}; s=${s%'"'*}
echo "$s"
In order to extract the substring between quotes you can use one of these alternatives:
Alternative 1:
SUBSTRING=`echo "$SUBSTRING" | cut -d'"' -f 2`
Alternative 2:
SUBSTRING=`echo "$SUBSTRING" | awk -F'"' '{print $2}'`
Alternative 3:
set -f;IFS='"'; SUBSTRING=($SUBSTRING); SUBSTRING=${SUBSTRING[1]};set +f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With