So I have found this sed expression for getting the text between to keywords exclusive of the keywords:
cat example.txt | sed '/^KEYWORD1/,/^KEYWORD2/!d; //d'
However, I want to understand for sure what is going on with this expression. My understanding is that with a 'pattern range' (correct me if this is improper terminology), that a bool is set when you hit the first match and that the command(s) following the pattern range are only executed if that bool is true.
Then there is the
//d where the
// is supposed to mean the last expression/regex that was matched. So is it correct that in this case with a pattern range, the logic is as follows:
/^KEYWORD1/, set bool to true, proceed with !d command which does not delete this line, then since the last regex was
//d is effectively
/^KEYWORD1/d which deletes this line
- bool is true so it proceeds to not delete the 3 next lines and
/^KEYWORD1/ is not found on said lines so nothing is deleted
/^KEYWORD2/d, execute !d and then
/^KEYWORD2/d since this was the last regex used
So at this point I'm not sure how the lines before and after are not printed since it doesn't execute the commands (the
!d) unless the pattern range flag is set to true.
Or does sed at least look at the command for every line and since the first command is an inverse delete, it somehow changes the logic to delete all other lines where the pattern range bool is false?
Any clarification on how this sed expression works will be appreciated. I've read this great