in reply to Re^3: How do I use grep in a script
in thread How do I use grep in a script
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: How do I use grep in a script
by Flintlock (Novice) on Dec 26, 2017 at 21:07 UTC | |
by poj (Abbot) on Dec 26, 2017 at 21:53 UTC | |
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 Flintlock (Novice) on Dec 26, 2017 at 22:05 UTC |