Reputation: 33
I'm comparing below two files for duplicates
ac-list hostname permit tcp host 110.185.35.111 host 10.185.38.220 eq 1222
ac-list hostname permit tcp host 10.185.35.111 host 10.185.38.220 eq 1222
My script is as below
#!/usr/bin/perl
open( SOURCE, "</home/amp/surevy01/file1" );
open( DESTINATION, "</home/amp/surevy01/file2" );
while ( my $line = <SOURCE> ) {
while ( my $line1 = <DESTINATION> ) {
chomp( $line );
chomp( $line1 );
my @columns = split( ' ', $line );
my @gitcols = split( ' ', $line1 );
my $fld1 = $columns[4];
my $fld2 = $columns[5];
my $fld3 = $columns[6];
my $fld4 = $columns[7];
my $fld5 = $columns[9];
my $gitfld1 = $gitcols[4];
my $gitfld2 = $gitcols[5];
my $gitfld3 = $gitcols[6];
my $gitfld4 = $gitcols[7];
my $gitfld5 = $gitcols[9];
if ( $line == $line1
|| awk '/$fld1/ && /host/ && /$fld2/ && /$fld3/ && /$fld4/ && /$fld5/' $line1 ) {
print "\n All duplicate";
}
}
}
I'm getting the below error:
String found where operator expected at ./perltest line 25, near "awk '/$fld1/ && /host/ && /$fld2/ && /$fld3/ && /$fld4/ && /$fld5/'" (Do you need to predeclare awk?)
Scalar found where operator expected at ./perltest line 25, near "'/$fld1/ && /host/ && /$fld2/ && /$fld3/ && /$fld4/ && /$fld5/' $line1" (Missing operator before $line1?)
syntax error at ./perltest line 25, near "awk '/$fld1/ && /host/ && /$fld2/ && /$fld3/ && /$fld4/ && /$fld5/'"
syntax error at ./perltest line 30, near "}"
Execution of ./perltest aborted due to compilation errors.
However without using the awk
command my script is working perfectly fine.
Upvotes: 0
Views: 80
Reputation: 2154
Although I am not sure why this "comparison" is useful to you, the following code does what your code seems to be attempting to do:
#!/usr/bin/perl
use strict;
use warnings;
my $file1 = shift || '/home/amp/surevy01/file1';
my $file2 = shift || '/home/amp/surevy01/file2';
my @cols = (4, 5, 6, 7, 9);
open(my $fh1, "<$file1") or die "Cannot open source: $file1\n";
open(my $fh2, "<$file1") or die "Cannot open destination: $file2\n";
while( my $line1 = <$fh1> ) {
chomp $line1;
my @cols1 = split ' ', $line1;
while( my $line2 = <$fh2> ) {
chomp $line2;
my @cols2 = split ' ', $line2;
my $count = 0;
foreach my $i ( @cols ) {
if( $cols1[$i] eq $cols2[$i] ) {
$count++;
}
}
if( $count == scalar @cols ) {
print "All duplicate\n";
}
}
seek $fh2, 0, 0;
}
Regarding your code, please note:
awk
is not a Perl command. It is a different programming language. That explains the first error you get.eq
; ==
is used to compare numbers. So you should use $line eq $line1
.awk
within a Perl script by using system
and then get the return status using $?
, but that would be terribly inefficient.Also please note that the code as it stands will compare each line of the first file with all lines of the second file, giving no context to know when the All duplicate
string is printed. Maybe you mean to compare both files side by side. For that you may want to check diff
which combined with something that would choose the right columns like cut
, awk
or even perl
would make a side by side comparison relatively easy.
If you need something else done you may want to edit your question and state what exactly you are trying to achieve and give longer input files to illustrate along with an expected output.
Upvotes: 0