×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Perl: How to print unmatched data after comparison of two files?

Perl: How to print unmatched data after comparison of two files?

Perl: How to print unmatched data after comparison of two files?

(OP)
I have two files here (file 1 & file 2). I would like to match name in bold as shown below from both files. However I need to print those unmatched data in file 1 format. I have been trying the code below but it is not the result I want. How to print those unmatched data in file 1 format after matching?


file 1

ID alan135/xkr $work(b05bfn00un0c3)/b05bfn00un0c3 ; #<= b05bfn00un0d0 Size:5848.270996
ID John06/ext $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
ID lily099/poli $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
ID sam012/pp $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
ID lily099/poli $wwrk(b05bfn00ld0p8)/b05bfn00ld0p8 ; #<= b05bfn00ld0s0 Size:INFINITY
ID Steve9018 $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
..
..
.


file 2

Accept => John06/ext Max
Accept => vivian788/ppr Maxcap
Accept => suzan645/pp Min
Accept => lily099/poli Max
Accept => Nick5670/uu Max
Accept => Anne309/pej Min
..
..
.


code

my ($line1,$line2,@arr1,@arr2,@arr3,@emptyarr);

@arr1 = <FILE1>;

@arr2 = <FILE2>;

foreach $line2 (@arr2) {

if ($line2 =~ m/(.*)\s+(.*)\s+(.*)\s+(.*)/) {

@arr3 = @emptyarr;
my $cname2 = "$2";
push (@arr3, $cname2);

}
}

foreach $line2 (@arr3) {
foreach $line1 (@arr1) {

if ($line1 =~ m/(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)/) {

my $cname1 = "$2";

if ($cname1 ne $line3) {

print NL "$cname1\n";

}
}
}
}


expected result:

ID John06/ext $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
ID lily099/poli $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
ID lily099/poli $wwrk(b05bfn00ld0p8)/b05bfn00ld0p8 ; #<= b05bfn00ld0s0 Size:INFINITY


RE: Perl: How to print unmatched data after comparison of two files?

Hi

Something like this ?

CODE --> command-line

# found
master # perl -naF'\s' -e 'if("file2"eq$ARGV){push@a,$F[2];next}print if$F[1]~~@a' file2 file1
ID John06/ext $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
ID lily099/poli $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
ID lily099/poli $wwrk(b05bfn00ld0p8)/b05bfn00ld0p8 ; #<= b05bfn00ld0s0 Size:INFINITY

[gray]# not found[gray]
master # perl -naF'\s' -e 'if("file2"eq$ARGV){push@a,$F[2];next}print unless$F[1]~~@a' file2 file1
ID alan135/xkr $work(b05bfn00un0c3)/b05bfn00un0c3 ; #<= b05bfn00un0d0 Size:5848.270996
ID sam012/pp $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY
ID Steve9018 $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 Size:INFINITY 

Feherke.
feherke.github.io

RE: Perl: How to print unmatched data after comparison of two files?

(OP)
Thanks Feherke. =)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close