in reply to Re^5: How do I use grep in a script
in thread How do I use grep in a script
Assuming your data is like this (numeric account ids)
Acct:1234 1 2 3 Acct:2345 2 3 4
then you could use a hash of arrays (HOA) (see perldsc) to split the file into parts.
poj#!/usr/bin/perl use strict; my $acct; 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; } 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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: How do I use grep in a script
by Flintlock (Novice) on Dec 27, 2017 at 14:19 UTC | |
by poj (Abbot) on Dec 27, 2017 at 15:05 UTC | |
by Flintlock (Novice) on Dec 27, 2017 at 17:34 UTC | |
by poj (Abbot) on Dec 27, 2017 at 17:37 UTC | |
by Flintlock (Novice) on Dec 27, 2017 at 17:49 UTC | |
| |
|
Re^7: How do I use grep in a script
by Flintlock (Novice) on Dec 26, 2017 at 22:05 UTC |