Let's suppose that the file data you're using as input are consistent, and always have the form you've shown -- at least, let's assume they consiste of one or more "records" where each record contains multiple lines, and exactly three of these lines that begin with the labels "Equipment:", "solved:" and "Lab:".

Based on your question, the strings to capture is on the lines with "Equipment" and "solved" labels; they may be fixed length, and/or match a particular pattern expressable by a regex. So maybe something like this (not tested):

... while (<FILE>) { if ( /^Equipment:.*?(\d+)-(\S+)/ ) { ( $str1, $str2 ) = ( $1, $2 ); } elsif ( /^solved:\s+(\S+)\s+"([^"]+)/ ) { ( $str3, $str4 ) = ( $1, $2 ); print "Found: str1= $str1 str2= $str2 str3= $str3 str4= $str4\ +n"; } }
If you don't get what that's doing, read up on perl regular expressions (man pages "perlretut", "perlre", and numerous other reference works). Basically, the "if" is looking for the line that starts with "Equipment:" and contains a string of digits followed immediately by a dash and a set of non-whitespace characters; these latter two things are retained for later use (as $1 and $2) via the paren operaters. Likewise, the "elsif" is looking for the "solved" line, and capturing the next non-whitespace string from that line, along with anything enclosed in double-quotes.

In reply to Re: How do I extract some records from a textfile by graff
in thread How do I extract some records from a textfile by bory

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.