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 | |
|
Re: Copy Lines After Match
by NetWallah (Canon) on Dec 17, 2015 at 01:12 UTC | |
|
Re: Copy Lines After Match
by 2teez (Vicar) on Dec 17, 2015 at 01:07 UTC | |
|
Re: Copy Lines After Match
by vinoth.ree (Monsignor) on Dec 17, 2015 at 04:31 UTC | |
by 2teez (Vicar) on Dec 18, 2015 at 07:09 UTC | |
by GotToBTru (Prior) on Dec 18, 2015 at 16:02 UTC |