CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
iterate through each line and send to a variable

 



regex2012
User

Dec 22, 2015, 11:20 AM

Post #1 of 2 (885 views)
iterate through each line and send to a variable Can't Post

Here is a giant list I am working on:
cat /tmp/pollst:
CORPORATE-ACCG_FILE-Linux
CORPORATE-MAIL_FILER-Solaris

cat /tmp/monitor.txt
1239,0,3,0,CORP-ACCG-FILE-Linux,MONTHLY-Full,FIXER,test-nbu,1450711808,0000001176,1450712984,test-nbu,1,,7185610,10770,,100,,root,1,19,0,71,root,test-nbu,3,9,0,,,1,/vol/root,1,0,testx,testx,1450711812,0000001172,1450712984,0,the requested operation was successfully completed,38,12/21/2015 07:30:08 - Info nbjm(pid=27801) starting backup job (jobid=1239) for client FROG\, policy CORP-ACCG-FILE-Linux\, schedule MONTHLY-Full,12/21/2015 07:30:26 - estimated 0 kbytes needed,12/21/2015 07:30:26 - Info nbjm(pid=27801) started backup (backupid=FIXER_1450711812) job for client Fixer


Code
open my $pollst_fh, '<', '/tmp/pollst.txt' or die $!; 
chomp(my @words = <$pollst_fh>);
print "@words\n";
close $pollst_fh;

my $pattern = join '|', @words;
my @lines;
open my $fh, '<', '/tmp/list.txt' or die "unable to open file '$file' for reading : $!";
open my $fh2, '>', '/tmp/list2x.txt' or die "unable to open file '$file1' for reading : $!";
while (<$fh>) {
push @lines, $_ if /$pattern/;
#print "these are pusched @lines\n";
}
print $fh2 "$_\n" foreach @lines;
close $fh2;

This works but not to my liking. What it does is it produces another file that contains the patterns:
CORPORATE-ACCG_FILE-Linux
CORPORATE-MAIL_FILER-Solaris

I want it to just search for CORPORATE-ACCG_FILE-Linux and then save it to a file (or even a variable) that can be used with a command, and then go to the next one CORPORATE-MAIL_FILER-Solaris, and save it to a file (or variable that will be reused) so I can run a command on it.

I have tried numerous ways, but all I can get is one big file with the patterns

CORPORATE-ACCG_FILE-Linux
CORPORATE-MAIL_FILER-Solaris

Is there any idea how to use foreach to do this?

I only can do it in bash but I want to use perl.


Chris Charley
User

Dec 22, 2015, 12:54 PM

Post #2 of 2 (878 views)
Re: [regex2012] iterate through each line and send to a variable [In reply to] Can't Post

The entry in 'monitor.txt', CORP-ACCG-FILE-Linux doesn't match the line in 'pollst', CORPORATE-ACCG_FILE-Linux. Differences are 'CORPORATE vs. 'CORP' and an underscore, '_' instead of a hyphen '-'.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives