Through the command line I draw one or several of the patterns found text.xml file:

perl -ln0e 'while(/<PMResult.*?<\/PMResult>/gs) {
  $x=$&;print $x if $x=~/BCCEL-[1-3]/}' text.xml

I need to create a new file with this pattern found; the new file will have the first three lines of Text.xml file before of pattern and have the last two lines of Text.xml file after the pattern.

closed as unclear what you're asking by Anthon, cuonglm, mdpc, Michael Homer, slm Apr 28 '15 at 22:55

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Hi Guys, this is the complete command line: perl -ln0e 'while(/<PMResult.*?<\/PMResult>/gs) {$x=$&;print $x if $x=~/BCCEL-[1-3]/}' text.xml – Cesar May 22 '14 at 19:52
  • 4
    It seems your file is XML. Use a proper parser. – choroba May 22 '14 at 19:54
  • 2
    ... as in one of the many available at CPAN. For the love of God don't write another parser yourself. – Kyle Jones May 22 '14 at 20:00
  • @choroba, I notice that people who give this kind of advise never actually propose a solution. How do you do this with a proper parser? – Stéphane Chazelas May 22 '14 at 20:00
  • Do you want the 3/2 first/last lines of the file or the 3/2 lines before/after each match (as in some context around the match)? – Stéphane Chazelas May 22 '14 at 20:10

Browse other questions tagged or ask your own question.