I'm not sure how to do this with sed (not too familiar with it), but you could use GNU grep with Perl-compatible regular expressions (see this answer for another example).
Here's a quick regex I've put together for your test input (assuming your data is in a file named 'foo'):
cat foo | grep -Po '(?<=\[A )[^\]]+'
This outputs:
hi
how
why
update - How this works:
The first portion of the regex (?<=\[A )
uses a negative-lookbehind, which basically means you ensure this think you are looking for is preceded by something (in this case \[A
). This helps give context to what you are looking for. This can also be accomplished with capture groups, but since I've not done this sort of thing before with grep, I wasn't sure how to use them here. The syntax for one of the lookbehinds is (?<=THING_TO_PRECEDE_YOUR_MATCH_WITH)
.
The second chunk [^\]]+
just says "find one or more characters that are not \]
. Note that we have to escape the square-brackets because they mean something in regular expressions. [^CHARSET]
just says anything but some given character set or character-class. The +
just says find one or more of what we just mentioned.
Depending on your experience with regular expressions this may or may not have been helpful, let me know if there are any points that I could better explain. I'm not sure of the best place to learn these. Having used python a lot, I find their syntax reference quite handy. Also, google tends to point to http://www.regular-expressions.info/ a lot, but I can't say from experience how useful it is.