How can I extract exact string between brackets?
What I tried is:
echo "test [test1] test" | grep -Po "(?=\[).*?(?=\])"
But the output is:
[test1
It should be:
test1
Better to use grep.
Use a lookbehind:
echo "test [test1] test" | grep -Po "(?<=\[).*?(?=\])"
awk should do too:
echo "test [test1] test" | awk -F"[][]" '{print $2}'
test1
Or sed
echo "test [test1] test" | sed 's/[^[]*\[\|\].*//g'
test1
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