The nested "while" loop inside your fourth "if" block is wrong. In fact, as written, it will be an infinite loop, because you're not doing anything to change the value of "$_" -- it remains true "forever".
Just reduce that fourth "if" block to one statement:
Then add two more "if" blocks:if ($_ =~ "HW/OS/Client") { $ClientServer = $_; }
The point is that your "$ClientServer" variable works just fine as a state variable. Whenever it has any non-empty value, you print the current line of input, otherwise, you don't print. You go over all the lines of input with just the one outer "while" loop, and the various "if" conditions will do the right thing.if ( $ClientServer ) { print; } if ( /^\s*$/ ) { # true when the current line is blank $ClientServer = ""; }
(As shown above, the blank line following the data block of interest will be included in the output. If you don't want the blank line printed, just put that "if" block above the one that does the "print".)
In reply to Re^6: How to print certain lines out of a text file.
by graff
in thread How to print certain lines out of a text file.
by sawdustar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |