PhoenixJay PhoenixJay - 1 year ago 43
Perl Question

Perl to find character and add second line after it finds text

I am trying to get this script to work in regards to two find strings. I am having trouble getting the file to export the format I am looking for.

use strict;

my $find = '^(H0|HT)';

open (NEW, ">", "output.txt" ) or die "could not open:$!";
open (FILE, "<", "input.txt") or die "could not open:$!";

while (<FILE>) {
chomp;

if (/^\h{39}\XX187/){
print NEW join(",","$_\n");
}

if (/$find/){
print NEW join(",","$_")
}
}
close (FILE);
close (NEW);


Input File:

Txn Bch
Account Patient Name Date
--------------------------------------------------------------------------------------------

HT12345678 TEST,TESTNAME 01/01/16
XX187 CLAIM PROCESSED 01/01/16


Output File (Expectation):

HT12345678 TEST, TESTNAME 01/01/16 XX187 CLAIM PROCESSED 01/01/16

Answer Source

Your question is very imprecise, and I'm having trouble understanding exactly what it is that you need. However, this short program may help

use strict;
use warnings 'all';

while ( <DATA> ) {
    print if /^(?:H[0T]|\s+XX187\b)/;
}


__DATA__
Account        Patient Name                                                         Date 
--------------------------------------------------------------------------------------------

HT12345678    TEST,TESTNAME                                                         01/01/16
                                  XX187         CLAIM PROCESSED                 01/01/16

output

HT12345678    TEST,TESTNAME                                                         01/01/16
                                  XX187         CLAIM PROCESSED                 01/01/16