开发者

using the sed command: match between 2 regular expressions

开发者 https://www.devze.com 2023-01-22 19:20 出处:网络
The syntax for this is: sed -n \'/Regex1/,开发者_开发知识库/Regex2/p\' But this includes the lines where Regex1 and Regex2 are found, how can I exclude them?

The syntax for this is:

sed -n '/Regex1/,开发者_开发知识库/Regex2/p'

But this includes the lines where Regex1 and Regex2 are found, how can I exclude them?

For example:

abcd-Regex1

BlaBlaBla

abcd-Regex2

Then I only want: BlaBlaBla


You can do this with a simple state machine in awk:

pax> echo 'abcd-Regex1
BlaBlaBla
abcd-Regex2' | awk '/Regex2/{e=0}{if(e){print}}/Regex1/{e=1}'

BlaBlaBla

It basically uses an echo flag e, with the commands below executed in sequence for each line:

  • /Regex2/{e=0} turns echo off when terminating line found.
  • {if(e){print}} prints line if echo is on.
  • /Regex1/{e=1} turns echo on when initialising line found.

If you must use only sed, there is a way you can do it by passing it through another sed to delete the start and end lines:

pax> echo 'asdf
abcd-Regex1
BlaBlaBla
abcd-Regex2' | sed -n '/Regex1/,/Regex2/p' | sed -e '/Regex1/d' -e '/Regex2/d'

BlaBlaBla
0

精彩评论

暂无评论...
验证码 换一张
取 消