Ok I used the code in the comments and tryed adding the scanning for X2 and X3 the same way as for X1 but for some reason the saveto vars are mixed up at the end
The wrong one I get is:
 X3: yes yes yes BEGIN_TAG X1 test test test tes test test X2 no no no no no  nono non no no  nononono no no  no X3 hi hi hi hi hi hi  hi hi hi hi  hi hi

The right one would be:
X1: test test test test test test tes test test X2: no no no no no no no no no nono non no no nononono no no no X3: yes yes yes hi hi hi hi hi hi hi hi hi hi hi hi



The code I have is:

#!/usr/bin/perl use strict; use warnings; open (FILE, '<', $ARGV[0]) or die "Could not open file: $!"; my $saveX1=0; my $saveX2=0; my $saveX3=0; my $savetoX1=''; my $savetoX2=''; my $savetoX3=''; while (my $line=<FILE>){ chomp $line; my @parts=split(' ',$line,2); if ( (!$saveX1) && $parts[0] eq 'X1') { $savetoX1=$parts[1]; $saveX1 +=1; } elsif ($parts[0] eq 'X2' ||($parts[0] eq 'BEGIN' && $parts[2] eq 'TA +G' ) ){ #print $savetoX1."\n" if ($savetoX1) ; $savetoX1=''; $saveX1=0; } elsif ($saveX1) { $savetoX1.=' '.$line; } if ( (!$saveX2) && $parts[0] eq 'X2') { $savetoX2=$parts[1]; $saveX2 +=1; } elsif ($parts[0] eq 'X3' ||($parts[0] eq 'BEGIN' && $parts[2] eq 'TA +G' ) ){ #print $savetoX2."\n" if ($savetoX2) ; $savetoX2=''; $saveX2=0; } elsif ($saveX2) { $savetoX2.=' '.$line; } if ( (!$saveX3) && $parts[0] eq 'X3') { $savetoX3=$parts[1]; $saveX3 +=1; } elsif ($parts[0] eq 'BEGIN' && $parts[2] eq 'TAG' ){ #print $savetoX3."\n" if ($savetoX3) ; $savetoX3=''; $saveX3=0; } elsif ($saveX3) { $savetoX3.=' '.$line; } } print "X1: ".$savetoX1."\n" if ($savetoX1) ; print "X2: ".$savetoX2."\n" if ($savetoX2) ; print "X3: ".$savetoX3."\n" if ($savetoX3) ;

In reply to Re: Scanning a file and extracting certains values within one or multiple lines by Arengin
in thread Scanning a file and extracting certains values within one or multiple lines by Arengin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.