all sed
-
EDIT - I removed the specificity of which topic needed to follow topic1
. Now any new [topic
will end the range, so order shouldn't matter.
sed -En '/^\[topic1\]/,/^\[topic/ { /^key2\s*=/ { s/^.*=\s*"([^"]+)".*/\1/; p; } }'
Breakout:
sed -En
says use extended matching but don't print anything unless requested.
/^\[topic1\]/,/^\[topic/ { ... }
says do the stuff between the braces only between the section headers for topic1 and topic2 (inclusive).
Within that,
/^key2\s*=/ { ... }
says do the stuff between these braces only on the key2 lines.
Then, once you've found the key2 between topic1 and the next topic,
s/^.*=\s*"([^"]+)".*/\1/;
says replace the whole line with just the value part between the quotes, and then p
rint the result.
Formatted:
sed -En '
/^\[topic1\]/,/^\[topic/ {
/^key2\s*=/ {
s/^.*=\s*"([^"]+)".*/\1/;
p;
}
}
'
Remove the ;
's and that will work for non-GNU sed
as well, I think.