Home: Perl Programming Help: Beginner:
basic search for elements: additional question



regex2012
User

Dec 14, 2015, 8:16 AM


Views: 3773
basic search for elements: additional question


Code
#!/usr/bin/perl 
use strict;

open my $fh9, '<', '/tmp/test2.txt' or die "unable to open file 'file' for writing: $!";
my @linesmon = <$fh9>;
close $fh9;


open my $fh8, '<', '/tmp/test1.txt' or die "unable to open file 'file' for writing: $!";
my @lines1 = <$fh8>;
close $fh8;

for my $policy (@lines1) {
my @newarray = grep {$policy} @linesmon;
print "@newarray\n";
}

I am trying to grep for the lines in test2.txt which contain the elements of test1.txt but it is not working. Not sure what I am doing wrong. I found that I get a printout of test2.txt but that the printout is the entire file, not a printout of the lines or data that contains the elements that are in test1.txt.


(This post was edited by regex2012 on Dec 15, 2015, 7:56 AM)


BillKSmith
Veteran

Dec 14, 2015, 8:38 AM


Views: 3764
Re: [regex2012] basic search for elements of array

Assuming that test1 does not contain any characters which have special meaning in REGEX and that you allow the possibility of the same line being printed more than once, Change your grep to use a REGEX.

Code
my @newarray = grep {/$policy/} @linesmon;

Good Luck,
Bill


regex2012
User

Dec 14, 2015, 10:09 AM


Views: 3759
Re: [regex2012] basic search for elements of array

This doesn't work but I tested it with another file that did not have 100s of lines in it and it did work. So I assume that there must be some special characters in the original file. Is there a way to work around this, or do I need to identify any special character that might be in the file and then rewrite the regex?


BillKSmith
Veteran

Dec 14, 2015, 11:01 AM


Views: 3755
Re: [regex2012] basic search for elements of array

My crystal ball "doesn't work". I doubt that you will get a better answer until you post code, data, and expected results so that we can duplicate your problem.
Good Luck,
Bill


regex2012
User

Dec 15, 2015, 7:20 AM


Views: 3743
Re: [BillKSmith] basic search for elements of array: thanks!

Thanks for everyone's help!
I found the issue, what I really wanted to use was the print if function, after further reading.
while (<$fh>) {
print if (/TEST-GROUP/);

or what ever data I need to "grep." I did learn a lot from examining the different ways of using grep and print if.


regex2012
User

Dec 15, 2015, 7:55 AM


Views: 3739
Re: additional question

I am trying to use print if logic to print out any line in test1.txt that match values of test2.txt. Is the following possible?
I don't get any errors, but no return either.

Code
open my $fh, '<', '/tmp/test1.txt' or die "unable to open file 'file' for writing: $!"; 
open my $fh2, '<', '/tmp/test2.txt' or die "unable to open file 'file' for writing: $!";
my @lines1 = <$fh2>;

while (<$fh>) {
foreach my $i (@lines1) {
print if (/$i/);
}
}

But I do get a return from this:

Code
while (<$fh>) { 
print if (/CORP/);

}


(This post was edited by regex2012 on Dec 15, 2015, 12:13 PM)


FishMonger
Veteran / Moderator

Dec 15, 2015, 8:33 AM


Views: 3734
Re: [regex2012] additional question

You need to post sample data from both files so that we can run a test to duplicate the problem.

Without that, it would be pure speculation.


regex2012
User

Dec 15, 2015, 12:39 PM


Views: 3723
Re: [FishMonger] additional question

Ok - here is what I'm seeing. I made these test files:


Code
cat /tmp/testa 
bug
car
soda
CORP-LEGAL_ENTERPRISE-SOLARISreviewbug page18341

cat /tmp/test2a
CORP-LEGAL_ENTERPRISE-SOLARIS
CORP-LEGAL_ENTERPRISE-SOLARISx


Code
Now if I do this: 
open my $fh9, '<', '/tmp/testa' or die "unable to open file 'file' for writing: $!";
open my $fh8, '<', '/tmp/test2a' or die "unable to open file 'file' for writing: $!";

while (<$fh9>) {
print if (/CORP-LEGAL_ENTERPRISE-SOLARIS/);
}

I get the returned data as:
CORP-LEGAL_ENTERPRISE-SOLARISreviewbug page18341
which is what I want.

But if I do this:

open my $fh9, '<', '/tmp/testa' or die "unable to open file 'file' for writing: $!";
open my $fh8, '<', '/tmp/test2a' or die "unable to open file 'file' for writing: $!";
my @lines1 = <$fh8>;

while (<$fh9>) {
foreach my $i (@lines1) {
print if (/$i/);
}
}

Code
I get no return -no error either.  however, If I remove the "reviewbug page18341" from the file, I do get the line CORP-LEGAL_ENTERPRISE-SOLARIS back.  How would I write this so that I can "grep" or extract a pattern so I get the entire line returned? 


(This post was edited by regex2012 on Dec 15, 2015, 12:41 PM)


FishMonger
Veteran / Moderator

Dec 15, 2015, 12:49 PM


Views: 3717
Re: [regex2012] additional question

You need to remove the line terminator.

Code
chomp(my @lines1 = <$fh8>);

or

Code
my @lines1 = <$fh8>; 
chomp @lines1;



BillKSmith
Veteran

Dec 15, 2015, 12:49 PM


Views: 3717
Re: [regex2012] additional question

If /$i/ and /CORP-GOLD-NETAPP_LEGAL1-NDMP/ produce different results, they must be different. You have shown that $i comes from one line of @line1 which you read from a file. Every line of a file (and therefore $i) ends with a newline. Your fixed regex does not. You must fix this. It would be wild speculation to say that this is the only difference. Fishmonger and myself have both said we need code and data. In order to debug your problem, we must be able to reproduce it on our own machines. We do not need much data and it does not have to be real, but it must demonstrate the problem.

Note: Your use of while and if rather than grep is probably a good idea, but it does nothing to solve the problem. You must get the regex right in either case.



UPDATE: See Cris's reply to http://perlguru.com/gforum.cgi?post=82439;. I think you could adapt it to your problem.
Good Luck,
Bill

(This post was edited by BillKSmith on Dec 16, 2015, 6:45 AM)


regex2012
User

Dec 18, 2015, 11:01 AM


Views: 3672
Re: [regex2012] basic search for elements: additional question

after a bit of investigation, I got it to work with the hash method you suggested in the link!! Thanks, all!


Laurent_R
Veteran / Moderator

Dec 19, 2015, 3:00 AM


Views: 3649
Re: [regex2012] basic search for elements: additional question

Hi,

just a brief additional comment on your code. You have:

Code
open my $fh, '<', '/tmp/test1.txt' or die "unable to open file 'file' for writing: $!";  
open my $fh2, '<', '/tmp/test2.txt' or die "unable to open file 'file' for writing: $!";


The "unable to open file 'file'..." is useless: your filename is not 'file', but test1.txt or test2.txt. For the error message to be useful you should specify which file you could not open:

Code
open my $fh, '<', '/tmp/test1.txt' or die "unable to open file /tmp/test1.txt for writing: $!";

or, slightly better, IMHO:

Code
my $file1 = '/tmp/test1.txt'; 
open my $fh, '<', $file1 or die "unable to open file $file1 for writing: $!";