Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^4: How do I use grep in a script

by Corion (Patriarch)
on Dec 26, 2017 at 20:27 UTC ( [id://1206244]=note: print w/replies, xml ) Need Help??


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
    Thanks You Corion, Yes, I think I want to loop over @lines. The $outfile I am trying to create here is a separate file for each Account designated by Acct:1234 (the 1234 actually).

    To explain a bit there are SEVERAL lines of text following this line (including this line) that need to be written to that file name.

    I wish this to happen for each section where the Acct:#### occurs, but first I wanted to learn how to create the file in the form of 1234_2017.txt.

    Does this make sense ?

      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.

      #!/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; }
      poj
        Thank you poj - as one might expect there has been an additional request for this. Now I am being asked to include a second variable in the outfile name.

        from this code

        for my $acct (keys %data){ my $outfile = $acct.'_2017.txt';

        I need to add the last name (Name:LastName FirstName) which located on the same line in the $infile, but could also be in other places in the block of text being written to the $outfile. So now the $outfile name would look like this: LastName_1234_2017.txt.

        Any suggestions on how and where I should define this new variable?

        wow !!!

        That was PERFECT !!!!

        Thank you so MUCH !!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1206244]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found