my $outfile = $_."_"."$nm" for @lines ;
This line will set/declare $outfile in a loop, which is unlikely to be what you want.
Did you want a loop over @lines instead? Or did you want to take the first element of @lines?
print sprintf "Have %d items\n", 0+@lines; my $item = $lines[0]; my $outfile = $item . '_' . $nm; print "$outfile\n";
I tried the following code with the input data in the __DATA__ section for convenience:
#! / usr/bin/perl -w use strict; use warnings; use POSIX 'strftime'; my $nm = strftime('%Y', localtime).".txt"; my @lines = map { /:([^\s]+)/ ? $1 : () } # take the stuff between th +e : and the first blank grep { /Acct:/ } <DATA>; # Read a file line by line and select the lines ma +tching Acct: print sprintf "Have %d items\n", 0+@lines; my $item = $lines[0]; my $outfile = $item . '_' . $nm; print "$outfile\n"; print $outfile; #open (OUTFILE, ">$outfile"); # do whatever with the values in @lines #print "$_\n" for @lines; __DATA__ foo Acct:123 Acct:456 bar
Note that you have a close($filename), which likely should be close($fh) in your code.
In reply to Re^4: How do I use grep in a script
by Corion
in thread How do I use grep in a script
by Flintlock
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |