If I have a string:
Something (perhaps) Something (Whatever) Something (Fred)
I want to be able to extract the word Fred
.
In my mind, this means finding the last open bracket/parenthesis. Or, if I were able to work backwards from the right, I'd say let's find the first open bracket/parenthesis. I'm not sure which method would work more quickly, or which would be easier to code! On the whole, I'm inclined to ask for the 'working from the right' approach, because that seems to me something I'd have wider use for.
In summary: given a string with lots of open/close brackets, how do I best extract the text contained within the last pair of brackets, please?
Ideally, there'd be an incredibly simple Bash parameter expansion trick that would work the required magic, but if it has to be regexes, so be it!
Use (
and )
as field separators and output second last field:
awk -F '[()]' '{print $(NF-1)}' file
Output:
Fred