Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Copy Lines After Match

by vinoth.ree (Monsignor)
on Dec 17, 2015 at 04:31 UTC ( #1150578=note: print w/replies, xml ) Need Help??


in reply to Copy Lines After Match **UPDATE**

use strict; use warnings; open (NEW, ">", "output.txt" ) or die "could not open:$!"; my $find = 'COMPUTER1'; my $inside = 0; my $data = ''; while (<DATA>) { next if (/^$/); $inside = 1 if /COMPUTER1/; if ($inside){ $inside=0; next; } else{ $data .= $_; print NEW $_ } } print '[' . $data . ']'; close(NEW) __DATA__ COMPUTER1 ACCT1 ACCT2 ACCT3 COMPUTER1 ACCT4 ACCT7 ACCT8 ACCT9

All is well. I learn by answering your questions...

Replies are listed 'Best First'.
Re^2: Copy Lines After Match
by 2teez (Vicar) on Dec 18, 2015 at 07:09 UTC
    Hi vinoth.ree

    Am sure you know one can achieve the OP intended output without so much elaborate solution. like so:
    use warnings; use strict; while (<DATA>){ print unless /computer.|^\s+$/i; } __DATA__ COMPUTER1 ACCT1 ACCT2 ACCT3
    One can even take out the while loop and use grep like so:
    print grep{!/computer.|^\s+$/i}<DATA>;
    These gives the output:
    ACCT1 ACCT2 ACCT3 ACCT4 ACCT7 ACCT8 ACCT9
    @jalopez453,
    Please also note that the filehandle is written as __DATA__, double "_" not a single dash as did.
    Cheers!
    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      This assumes the data is really as simple as indicated, so that the question posed by the OP title is not really relevant. For the benefit of others who might find this thread by searching, the more detailed program is necessary.

      Dum Spiro Spero

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1150578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2020-06-03 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (22 votes). Check out past polls.

    Notices?