I have two files, want to compare file1 data with file2 second column and print line which are not matching. Need help in matching the pattern, file2 second column number can be leading 0 or 00 or 000.
Example:
file1
1
2
3
file2
a,0001
b,02
c,000
d,01
e,2
f,0005
Expected output:
c,000
f,0005
Awk statement I am using is :
awk -F ',' 'FNR==NR{a[$0];next}!($2 in a)' file1 file2
But this is not handling leading zero. Please advise. Thanks!
Don't know whats wrong with your input try perl also
#!/usr/bin/perl
use strict;
use warnings;
die "Usage: $0 File1 File2\n" if @ARGV != 2;
my $file2 = pop;
# Read first file
my %seen;
while (<>) { my @F = split; $seen{$F[0]+0} = 1;}
# Compare 2nd file with first file hash
local @ARGV = $file2;
while (<>) { my @F = split(",",$_); print if !$seen{$F[2]+0};}
awk 'FNR==1{++i}{print i==1 ? $1 : $3+0}' f1 FS=',' f2
Syntax Error The source line is 1.
The error context is
FNR==1{++i}{print >>> i== <<<
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
Yes! perl solution worked! thanks Please let me know, if i want to change column number of second file, where the change will be in in perl code? Sorry for the dummy question
[test@test]$ cat script.pl
#!/usr/bin/perl
use strict;
use warnings;
die "Usage: $0 File1 File2\n" if @ARGV != 2;
my $file2 = pop;
# Read first file
my %seen;
while (<>) { my @F = split; $seen{$F[0]+0} = 1;}
# Compare 2nd file with first file hash
local @ARGV = $file2;
while (<>) { my @F = split(",",$_); print if !$seen{$F[2]+0};}
[test@test]$
[test@test]$
[test@test]$ script.pl f1 f2
2,2,333
[test@test]$
[test@test]$ cat f1
971507520787
[test@test]$ cat f2
1,2,0971507520787
2,2,333
[test@test]$