Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparison of lines in two separate files

Status
Not open for further replies.

sumncguy

IS-IT--Management
May 22, 2006
118
US

I want to make sure that file2 includes the mandatory lines
defined in FILE1.

#!/usr/local/bin/perl -w
#
open (FILE1, "/tftpboot/houseboat") or die;
open (file2, "/tftpboot/carboat") or die;
@xray=<file2>;
for $line (<FILE1>) {
#check to see if "$line" is resident in @xray. If so, do #nothing, else print "$line isnt in FILE1"
#ng print grep /"$line"/, @xray;
#ng print grep /'$line'/, @xray;
print grep /$line/, @xray;
}
close FILE1;
close FILE2;


.
.
.
.
snmp-server enable traps frame-relay subif
snmp-server host 16.10.10.7 XXX frame-relay snmp
snmp-server host 16.10.68.7 XXX frame-relay snmp
tacacs-server host 16.10.8.8
tacacs-server host 16.10.1.8
tacacs-server timeout 20
control-plane
banner exec
Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE *****************************************************************
/ at ./ssa line 9, <FILE1> line 144.

Soon as it hits the part of the banner that includes a line of nuttin but asterisks, it fails.

My question is, how do I force perl to take those asterisks literally. Or maybe theres a better way ??

Tks
Chris
 

Thanks .. great link too .. cut through alot of bull and give nice examples.

Will let you know how it works tomorrow when I return to the grind.

Thanks Again
Chris
 
An even simpler solution (though the one suggested is fine as well of course) would be just to escape the '$', or wouldn't it..??

So say something like:
Code:
print grep /\$line/, @xray;
 
No, that will match lines containing '$line', not the contents of $line.

A simpler version of what I had would be:
Code:
print grep /\Q$line\E/, @xray;

Something to bear in mind with the way the OP is matching is whether he wants to match lines that contain other lines, or whether he wants to match lines that are exact matches.

He is currently doing 'contains'. If he wants exact matches, then a regex isn't actually necessary:

Code:
print grep $line eq $_, @xray;
 
Ah, yes, I am sorry. I kind of misundertsood completely what the actual problem was... :-\

Maybe a quick glance at quotemeta() might help as well...
 
Ok thanks guys n gals.. definitely helped but got anudder question

Why does this work in unix solaris 5.8.2

#!/usr/local/bin/perl -w
open (FILE1, "/tftpboot/fidprow1") or die;
open (FILE2, "/tftpboot/fidprow") or die;
@xray=<FILE2>;
for $line (<FILE1>) {
if (!(grep $line eq $_, @xray)) {
print "$line";
}
}
close FILE1;
close FILE2;

But not in the windows environment wiff 5.8.2 ?
Doesnt like the knot ! thangy ...

this works though and is crappy cause I try to do more wiff less - content that is ..

#!c:\Perl\bin\perl.exe -wd
open (FILE1, "fidprow1.txt") or die;
open (FILE2, "fidprow.txt") or die;
@xray=<FILE2>;
for $line (<FILE1>) {
if (grep $line eq $_, @xray) {
next;
}else{
print "$line";
}
}
close FILE1;
close FILE2;

Thanks again
C
 
Can't say I really dug through all your code, so excuse me if I get it wrong again, but how about
Code:
if ( grep $line ne $_, @xray) {
      print "$line";
}

or, even slightly shorter, though completely the same
Code:
print $line if( grep $line ne $_, @xray);
 
But not in the windows environment wiff 5.8.2 ?
Doesnt like the knot ! thangy ...

I doubt the underlying cause is Windows not liking the ! operator. A possibility is that the files were not created on Windows or one of them was not, so the line endings are different than perl on Windows expects (\r\n) and the comparison fails because the "eq" operator used in that context means both strings have to be an exact match.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top