2015_newbie has asked for the wisdom of the Perl Monks concerning the following question:
/tmp/locationDATSUN_BLUE TOYOTA_MAROON ELANTRA-YELLOW
The second file has 100s of lines. I'd like to search for the patterns in the first file in the second file and save them into separate files. So the lines that have DATSUN_BLUE and the fields criteria would go to /tmp/Datsun.txt and the ones that contain TOYOTA_MAROON and criteria would go to /tmp/Toyota.txt, etc. I want the files to be created per line found in first file. So instead of one big file with the patterns, each time a pattern is found it would go to a separate file. I have tried, and I can create an $fh3 output file, but can't create separate files for each pattern in the first file. Does anyone have an idea?1250 1 engineer office DATSUN_BLUE office 67 page30 text 1500 1 billing office MERCEDES-TAN office 98 page 40 txt 1500 7 billing office JAGUAR-BLACK office 98 page 40 txt 1250 0 engineer office ELANTRA_YELLOW office 66 page50 txt 1250 1 engineer office DATSUN_BLUE office 67 page30 text
open my $fh2, '<', '/tmp/location' or die "unable to open file for rea +ding : $!"; open my $fh1, '<', '/tmp/carlist' or die "unable to open file for rea +ding : $!"; chomp(my @carlist = <$fh1>); print "these are the policies @carlist\n"; close $fh1; my $pattern = join '', @carlist; my @strings; while (<$fh2>) { my @fields = split(',', $_); local $" = ','; if ( $fields[1] eq '1' || $fields[1] eq '0') { push @strings, $_ if /$pattern/; print $fh3 "@fields[0,4]\n" if /engineer/ && !/,-,/; } } close $fh2; close $fh1; close $fh3;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to save patterns found in array to separate files
by Athanasius (Archbishop) on Dec 28, 2015 at 06:18 UTC | |
by 2015_newbie (Novice) on Dec 29, 2015 at 05:38 UTC | |
by Athanasius (Archbishop) on Dec 29, 2015 at 09:06 UTC | |
by 2015_newbie (Novice) on Dec 30, 2015 at 20:08 UTC |