Re: Print to new file for each results



On 08/29/2006 06:32 PM, Ron McKeever wrote:
I am try to use part of someones elses code that creats the data file which prints out like this:

ip|result|deptA|data
ip|result|deptB|data
ip|result|deptC|data

My goal instead of having all the data in one big file is to loop this and create a file for each result. So I would have a deptA.out file with deptA info then a deptB.out file with dept b info and etc...
[...]

Although this is very different from your program, it should give you the general idea:

use strict;
use warnings;
use File::Slurp;
use File::Spec::Functions;

my @departments = q{
192.168.14.0|88|Sales|Richard Throop
192.168.16.0|90|Accounting|Juliet Rosenblatt
192.168.20.0|91|Logistics|Mark Mane
192.168.24.0|94|Security|Marlon Johnson
} =~ m/^\s*(.*)$/mg;

# print join "\n", @departments;
my $dir = catfile($ENV{HOME}, qw(tmp tmp));

foreach my $info (@departments) {
my $fname = (split /\|/, $info)[2];
$fname = catfile($dir, $fname);
print $fname, "\n";
write_file $fname, $info . "\n";
}



__HTH__
:-)

.



Relevant Pages

  • Re: reading file list, writing to text file
    ... fname %check it is reading all files ... The reason your loop is overwriting the previous text file is that you ... you should not open the file for reading multiple times - that is ... count = fprintf(fid, myfname); ...
    (comp.soft-sys.matlab)
  • Re: Variable value in while loop
    ... > declare -i size ... > while read size fname; ... > Inside a while loop all variable values are echoed OK, ... > because of the pipe agter awk. ...
    (comp.unix.shell)
  • Re: Problem using FileUtils to sort JPEG files
    ... of the for loop and only doing it once, ... Dir.glob.each do |fname| ... new scope. ... FileUtils.move(fname, "#{workdir}/#{filetype}") #workdir is ...
    (comp.lang.ruby)
  • Re: mv xrags and cp
    ... If you want do it with a loop use a while loop: ... find olddir -name '*.jpg | while read fname; ... do_something_with_file $fname ... breaks on any white space whatsoever. ...
    (Ubuntu)

Loading