Hi.
I have the following code


#!/usr/bin/perl open(FILE1, $ARGV[0]) || die "Error: $!\n"; @lines = <FILE1>; while ($line=<FILE1>){ if ($line=~/^BEGIN_TAG/){ <FILE1>; #get next line if ($line=~/^X1/ .. /^X2/){ # get all between X1 and X2 print $line; # print all of it } } }

I need to get the values between X1 and X2 even if that is over multiple line and print them out concatenated (if oder more than 1 line)
However I'm not getting anything from the above.... The inputfile (inputtext.txt) looks like:

BEGIN_TAG X1 test test test X2 no no no no 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

Any ideas? I get for you this is trivial but I'm stuck at this a weekend over already.
Thanks for any help you can provide.
Arengin

Original contents restored above by GrandFather

Hi.

This is an edited question since the first part of the problem is solved

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) ;

The values of the print at the end are all messed up here is what I have and what I sould have:

The wrong one (the one I have) I get is: <br/> <code> 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 inputfile (inputtext.txt) looks like:

BEGIN_TAG X1 test test test X2 no no no no 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

Any ideas? I get for you this is trivial but I'm stuck at this a weekend over already.
Thanks for any help you can provide.
Arengin


In reply to 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.