1

I have this kind of file:

Analysis of its root cause:
Blablablablabla
blabablabkjhjk
kjbsqbdqbds

Details of the fix
blablabla

Analysis of its root cause:
fddsfsdfsdfdsfs
blnskdbbqbbb
xxxxggggggg

Details of the fix
blablabla

Analysis of its root cause is repeated x times in the file. I would like to get the block of text delimited by "Analysis of its root cause" and "Details of the fix".

Thanks a lot for your help.

2
  • I tried to edit my post to say Hello but it does not work, so Hello :) Commented Nov 28, 2017 at 17:04
  • What is your regex flavor? Commented Nov 28, 2017 at 17:10

2 Answers 2

2

I'm pretty sure there is some better way to do this, but that's what I could manage:

/(?(?<=Analysis of its root cause:\n)((.*\n)*)(?=Details of the fix\n))/gU

I'm using positive lookahead and lookbehind, and the following modifiers:

g - global - Don't return after first match

u - Ungreedy - Make quantifiers lazy

Try it online: https://regex101.com/r/xpz7pg/2

Sign up to request clarification or add additional context in comments.

Comments

1

Not a regex answer, but using perl

Put your lines into a single file.

 perl -e '$/="Analysis of its root cause:"; #Sets the record delimiter 

      while(<>){ #Iterates over the file, record by record

      chomp; #Removes the delimiter

      if ($_ =~ /\n(.*?)\nDetails of the fix\n(.*)\n/s){ #Matches strings between Details of the fix. . is allowed to match newline

      print "ONE:$1TWO:$2"} # $1 is the analysis, $2 is the details
      }'
      file.txt

Output

ONE:Blablablablabla
blabablabkjhjk
kjbsqbdqbds
TWO:blablabla
ONE:fddsfsdfsdfdsfs
blnskdbbqbbb
xxxxggggggg
TWO:blablabla

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.