in reply to Re^7: How do I use grep in a script
in thread How do I use grep in a script

You can add the name onto the existing $acct variable

while (<$fh>){ next unless /\S/; # skip blank lines if (/Acct:(\d+)/){ $acct = $1; if (/Name:([^\s]+)/){ $acct = $1.'_'.$acct; } } push @{$data{$acct}},$_ if ($acct); }
poj

Replies are listed 'Best First'.
Re^9: How do I use grep in a script
by Flintlock (Novice) on Dec 27, 2017 at 17:34 UTC
    Here is my code. The addition to $acct is not adding the LastName as expected. Ideas??

    #!/usr/bin/perl use strict; my $acct; my $nam; my %data = (); my $infile = 'Facs_Data.txt'; open my $fh,'<',$infile or die "Could not open '$infile' : $!"; while (<$fh>){ next unless /\S/; # skip blank lines if (/Acct:(\d+)/){ $acct = $1; if (/Name:([^\s]+)/){ $acct = $1."_".$acct; } } push @{$data{$acct}},$_ if ($acct); } close $fh; for my $acct (keys %data){ my $outfile = $acct.'_2017.txt'; print "Creating $outfile\n"; open my $fh,'>',$outfile or die "Could not open '$outfile' : $!"; for (@{$data{$acct}}){ print $fh $_; } close $fh; }

      Can you post an example of the line with Name in ? This is my test file

      a b Acct:1234 Name:LastName2 Firstname 1 2 3 4 Acct:2345 Name:LastName1 Firstname 2 3 4

      poj
        STATUS Acct:142424 Disposition:9000 CANCEL Wait: 04/11/17 DEBTOR Name:LastName FirstName Ssn:123456789 Cbr: Ph:555.555.1212

        All of the above is on the same line.