If all you want to do is add the header (meta info) to every hit, this can probably be done in two very simple lines of code: always store the current (latest) header and add it to the results.
If you do the main data lookup line by line with a while loop or similar construct and supposing that no line has more than one <text> tag pairs (i.e. new texts are on new lines) and the opening and closing text tags are always on the same line (i.e. there are no line breaks within the header), something like this should work:
while (<FILE>){ if (/<text>(.*)<\/text>/) {$currentheader = $1} # if the current line +has a header, save it, otherwise, keep last saved header # your data search code goes here if (there was a hit) {print $currentheader along with the hit} }

This way you only go through the file once. Of course this may not be doable if you use some exotic solution for your search word matching instead of a while loop.

Edit: I just noticed that chrestomanci already proposed a more elaborate execution of the same idea.

In reply to Re: Retrieving meta information from txt by elef
in thread Retrieving meta information from txt by fanticla

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.