Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Copy Lines After Match **UPDATE**

by jalopez453 (Acolyte)
on Dec 16, 2015 at 23:31 UTC ( [id://1150560]=perlquestion: print w/replies, xml ) Need Help??

jalopez453 has asked for the wisdom of the Perl Monks concerning the following question:

I was able to clean up my file as expected but there is one piece I am having trouble with. I want to capture a header in the file and include it into each line copied, (BAR.COMP) and when it gets to the next header if the name changes I want it to include the new header to the next copied lines. This is what I have so far but just confused on how to incorporate the second peice. Any help would be great! Thanks!

use strict; use warnings; open (NEW, ">", "output.txt" ) or die "could not open:$!"; open (FILE, "<", "Report.txt") or die "could not open:$!"; while (<FILE>) { print NEW if /^\h{3}\d/; } close (FILE); close (NEW);

Report.txt

BAR.COMP1 DATE: 12/14/15 G2,,,,,,,USER LOGS DEVICE EMULATOR FROM-THRU MINS ROUTINE G1,,,,,,,USER LOGS COMPUTER04.1 0726 0808 42 Process Account Account Name Master Patient Number Account Number 0727 JOHN DOE 1234567899 1234567899 0730 JOHN DOE 1234567899 1234567897 0732 JOHN DOE 1234567899 1234567899 0742 JOHN DOE 1234567899 1234567893 0744 JOHN DOE 1234567899 1234567893 BAR.COMP2 DATE: 12/14/15 G2,,,,,,,USER LOGS DEVICE EMULATOR FROM-THRU MINS ROUTINE G1,,,,,,,USER LOGS COMPUTER04.1 0726 0808 42 Process Account Account Name Master Patient Number Account Number 0727 JOHN DOE 1234567899 1234567899 0730 JOHN DOE 1234567899 1234567897 0732 JOHN DOE 1234567899 1234567899 0742 JOHN DOE 1234567899 1234567893 0744 JOHN DOE 1234567899 1234567893

output.txt

BAR.COMP1 0727 JOHN DOE 1234567899 1234567899 BAR.COMP1 0730 JOHN DOE 1234567899 1234567897 BAR.COMP1 0732 JOHN DOE 1234567899 1234567899 BAR.COMP1 0742 JOHN DOE 1234567899 1234567893 BAR.COMP1 0744 JOHN DOE 1234567899 1234567893 BAR.COMP2 0727 JOHN DOE 1234567899 1234567899 BAR.COMP2 0730 JOHN DOE 1234567899 1234567897 BAR.COMP2 0732 JOHN DOE 1234567899 1234567899 BAR.COMP2 0742 JOHN DOE 1234567899 1234567893 BAR.COMP2 0744 JOHN DOE 1234567899 1234567893

Replies are listed 'Best First'.
Re: Copy Lines After Match
by kcott (Archbishop) on Dec 17, 2015 at 03:28 UTC

    G'day jalopez453,

    Last month, in "copy line with character", you posted almost exactly what you have here.

    The advice given there (which you mostly, if not totally, appear to have ignored) would apply here. In fact, the only fundamental difference I can see is that there, you wanted to print lines that match, while here, you want to print lines that don't match.

    I'd also look closely at the description you've provided. You wrote "copy the lines after the character match", which would suggest the expected output should be:

    ACCT1 ACCT2 ACCT3 COMPUTER1 ACCT4 ACCT7 ACCT8 ACCT9

    However, looking at your posted expected output, the description should more closely resemble: copy the non-blank lines that don't match ..."

    I suggest you go back, read the advice previously given, and apply it to your current problem.

    See also: "perlintro -- a brief introduction and overview of Perl".

    — Ken

Re: Copy Lines After Match
by NetWallah (Canon) on Dec 17, 2015 at 01:12 UTC
    The standard way to approach this is :
    • Declare a variable and initialize to zero (my $tog=0) (This is OUTSIDE THE LOOP)
    • In the loop, Compare the input with $find, and take one of the following actions:
    • On MATCH, set $tog=1, and skip the rest of the loop
    • on NO MATCH, check $tog, if it is 1, print the line to the output
    Try to code this logic in, and if you have trouble with the code, post it, explain your issue(s), and we can help you along.

            "I can cast out either one of your demons, but not both of them." -- the XORcist

Re: Copy Lines After Match
by 2teez (Vicar) on Dec 17, 2015 at 01:07 UTC

    Hi jalopez453,
    Nice try! You are almost there. What are you using the lines you are getting from the filehandle for in the while loop?

    The While loop is empty!!

    Don't forget to always use warnings; too

    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
Re: Copy Lines After Match
by vinoth.ree (Monsignor) on Dec 17, 2015 at 04:31 UTC

    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...
      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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1150560]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-23 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found