?
Solved

Compare first 3 fields between two files

Posted on 2011-02-10
3
Medium Priority
?
377 Views
Last Modified: 2012-05-11
I need to compare just the first three fields between 2 different files.

the first file have lines with 3 fields
servername1 WINDOWS daily

The second file have lines with 5 fields
servername1 WINDOWS daily 24 remotehost
servername2 WINDOWS daily 24 remotehost

I want to print to a third file where the fiirst 3 field exist in the seonds file but do not exist in the first file, so in this instance the third file would contain the second line from the second file

servername2 WINDOWS daily 24 remotehost




0
Comment
Question by:MatthewF
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 1800 total points
ID: 34866132
This should do what you want...
#!/usr/local/bin/perl

use strict;
use warnings;

# get the two filenames from the command line
my $fil1 = shift || die "Usage: $0 file1 file2 output_file";
my $fil2 = shift || die "Usage: $0 file1 file2 output_file";
my $ofil = shift || die "Usage: $0 file1 file2 output_file";

my %seen;
open IN, $fil1 or die "could not open $fil1: $!";
while (<IN>) {
    chomp;
    my @arr = split /\s+/; # split the line on whitespace
    $seen{$arr[0]}{$arr[1]}{$arr[2]}++;
}
close IN;

open OUT, ">$ofil" or die "could not write $ofil: $!";

open IN, $fil2 or die "could not open $fil2: $!";
while (<IN>) {
    chomp;
    my @arr = split /\s+/; # split on whitespace again
    # write the line to the output file unless we saw it in file1
    print OUT $_, "\n" unless exists($seen{$arr[0]}{$arr[1]}{$arr[2]});
}
close IN;
close OUT;

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 34866249
perl -ane 'print unless $s{"@F[0..2]"}++ or @ARGV' first second > third
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 34866276
As always, ozo is king of the one-liners.

I'm an expert at perl but often learn something new (or at least make me think) from ozo's one-liners.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question