Dears,

I have the file below in the following format

Success|Filter passed|[invalid field]|[invalid field]|Id-350a875b08796 +5e58cbe1f4a Accept: text/plain, text/plain, application/json, application/*+json, +*/*, */* Host: api2.tim.com.br User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.3 +6 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Via: 1.1 X-Forwarded-For: 144.22.98.123 X-Forwarded-Host: X-Forwarded-Server: Success|Success in calling policy shortcut|[invalid field]|[invalid fi +eld]|[invalid field]|Id-350a875b087965e58cbe1f4a|Call 'Set Request Me +ssage'|GET Accept: text/plain, text/plain, application/json, application/*+json, +*/*, */* Host: api2.tim.com.br User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.3 +6 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Via: 1.1 api2.tim.com.br X-Forwarded-For: 144.22.98.123 X-Forwarded-Host: X-Forwarded-Server: Content-Type: text/xml; charset="UTF-8"

I need to perform a search for the line that begins with the string "^ Success" and display all the items until there is a "^ Sucess" string again.

Here is an example of what I need to display:

Success|Filter passed|[invalid field]|[invalid field]|Id-350a875b08796 +5e58cbe1f4a Accept: text/plain, text/plain, application/json, application/*+json, +*/*, */* Host: api2.tim.com.br User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.3 +6 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Via: 1.1 X-Forwarded-For: 144.22.98.123 X-Forwarded-Host: X-Forwarded-Server:

What happens is that the amount of lines below after the match is very dynamic.

What happens is that the amount of lines below the match is very dynamic and in the same file there may be several lines of the same match and I would need to display them also when the file is run.

I do not know how to get the next line until the next occurrence. I can only catch the line of occurrence.

while ($line = <STDIN>) { chomp $line; if($line =~ /^Success.*Id-9952895b108957daeb296ba4/i) { print "$line\n"; } }
Could you guys help me?

In reply to Search after match in dynamic lines by leoberbert

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.