in reply to Re^15: How do I use grep in a script
in thread How do I use grep in a script
Presumably the name is always the same within the block for that account. Try
poj#!/usr/bin/perl use strict; my $acct; my %data = (); my %name = (); 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]+)/){ $name{$acct} = $1 unless defined $name{$acct}; } push @{$data{$acct}},$_ if ($acct); } close $fh; for my $acct (keys %data){ my $outfile = join '_',$name{$acct},$acct,'2017.txt'; print "Creating $outfile\n"; open my $fh,'>',$outfile or die "Could not open '$outfile' : $!"; for (@{$data{$acct}}){ print $fh $_; } close $fh; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^17: How do I use grep in a script
by Flintlock (Novice) on Dec 28, 2017 at 16:34 UTC | |
by Flintlock (Novice) on Dec 29, 2017 at 14:40 UTC | |
by poj (Abbot) on Dec 29, 2017 at 15:08 UTC | |
by Flintlock (Novice) on Dec 29, 2017 at 19:57 UTC |